Set and Get RGB values for a specific pixel in an image
-
- Posts: 12
- Joined: 2015-04-04T19:06:28-07:00
- Authentication code: 6789
Set and Get RGB values for a specific pixel in an image
I am new to IM - from the command line, under Windows 7, how would I set the RGB values for a specific pixel?
How would I read the RGB values for a specific pixel?
How do you specify a specific pixel's location for the above operations, if you know the dimensions (in pixels) of the image?
I want to use these operations to get, and then adjust, the RGB values for any single pixel I choose in a PNG or other image.
How would I read the RGB values for a specific pixel?
How do you specify a specific pixel's location for the above operations, if you know the dimensions (in pixels) of the image?
I want to use these operations to get, and then adjust, the RGB values for any single pixel I choose in a PNG or other image.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set and Get RGB values for a specific pixel in an image
Method 1:
create a single pixel image of a specific color, then composite over the image at the location desired.
See
http://www.imagemagick.org/Usage/canvas/#solid
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/basics/#parenthesis
Method 2: (easier)
simply draw a pixel of a given color onto the background image
see
http://www.imagemagick.org/Usage/draw/#primitives
Note I am a unix person, so one of the windows users may have to correct my windows syntax commands above.
See also http://www.imagemagick.org/Usage/windows/
create a single pixel image of a specific color, then composite over the image at the location desired.
Code: Select all
convert backgroundimage ( -size 1x1 xc:"rgb(rr,gg,bb)" ) -gravity northwest -geometry +X+Y -compose over -composite resultimage
http://www.imagemagick.org/Usage/canvas/#solid
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/basics/#parenthesis
Method 2: (easier)
simply draw a pixel of a given color onto the background image
Code: Select all
convert backgroundimage -fill "rgb(rr,gg,bb)" -draw "color x,y point" resultimage
http://www.imagemagick.org/Usage/draw/#primitives
Note I am a unix person, so one of the windows users may have to correct my windows syntax commands above.
See also http://www.imagemagick.org/Usage/windows/
-
- Posts: 12
- Joined: 2015-04-04T19:06:28-07:00
- Authentication code: 6789
Re: Set and Get RGB values for a specific pixel in an image
Thanks for the quick reply, Fred!
Yes, if there is a variation in the commands for Windows, I'd love to have someone post those.
Also - how would I read the current RGB values for a specific pixel so as to know what they are before I set them to something else?
Yes, if there is a variation in the commands for Windows, I'd love to have someone post those.
Also - how would I read the current RGB values for a specific pixel so as to know what they are before I set them to something else?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Set and Get RGB values for a specific pixel in an image
A BAT file (called onePixel.bat) with some methods of reading a single pixel.
For use in "xc:" or "-fill", as shown by Fred, the first of these methods is fine. But it is no good if you want to do some processing on the colour values.
For use in "xc:" or "-fill", as shown by Fred, the first of these methods is fine. But it is no good if you want to do some processing on the colour values.
Code: Select all
rem Returns colour of file %1, pixel coordinate %2,%3.
rem ----------------------------------------------------------------
rem Simple version.
FOR /F "usebackq" %%C ^
IN (`%IM%convert %1 -format "%%[pixel:p{%2,%3}]" info:`) ^
DO set ONE_PIXEL=%%C
echo ONE_PIXEL=%ONE_PIXEL%
rem Sample outputs from %[pixel:..]:
rem red
rem cmyk(1,2,3,5)
rem srgb(100%,94.7402%,0%)
rem Having a percentage in an environment variable won't normally cause a problem,
rem provided only one command translation is performed.
rem ----------------------------------------------------------------
rem Intensity.
FOR /F "usebackq" %%C ^
IN (`%IM%convert %1 -format "%%[pixel:p{%2,%3}.r]" info:`) ^
DO set ONE_PIXEL=%%C
echo ONE_PIXEL=%ONE_PIXEL%
rem ----------------------------------------------------------------
rem txt:
%IM%convert xc:%ONE_PIXEL% txt:
rem ----------------------------------------------------------------
rem Three components, range 0.0 to 1.0.
FOR /F "usebackq" %%C ^
IN (`%IM%convert %1 ^
-precision 9 ^
-format "ONE_PIXEL.R=%%[fx:p{%2,%3}.r]\nONE_PIXEL.G=%%[fx:p{%2,%3}.g]\nONE_PIXEL.B=%%[fx:p{%2,%3}.b]" ^
info:`) ^
DO set %%C
echo ONE_PIXEL.R=%ONE_PIXEL.R% ONE_PIXEL.G=%ONE_PIXEL.G% ONE_PIXEL.B=%ONE_PIXEL.B%
rem ----------------------------------------------------------------
rem Three components, range 0.0 to 100.0.
FOR /F "usebackq" %%C ^
IN (`%IM%convert %1 ^
-precision 9 ^
-format "ONE_PIXEL.R=%%[fx:100*p{%2,%3}.r]\nONE_PIXEL.G=%%[fx:100*p{%2,%3}.g]\nONE_PIXEL.B=%%[fx:100*p{%2,%3}.b]" ^
info:`) ^
DO set %%C
echo ONE_PIXEL.R=%ONE_PIXEL.R% ONE_PIXEL.G=%ONE_PIXEL.G% ONE_PIXEL.B=%ONE_PIXEL.B%
rem ...Do any arithmetic here...
set ONE_PIXEL=sRGB(%ONE_PIXEL.R%%%,%ONE_PIXEL.G%%%,%ONE_PIXEL.B%%%)
echo ONE_PIXEL=%ONE_PIXEL%
rem ----------------------------------------------------------------
rem Single colour spec eg "srgb(45%,67.123%,0%)", range 0.0 to 100.0.
FOR /F "usebackq" %%C ^
IN (`%IM%convert %1 ^
-precision 9 ^
-format "ONE_PIXEL=srgb(%%[fx:100*p{%2,%3}.r]\%%,%%[fx:100*p{%2,%3}.g]\%%,%%[fx:100*p{%2,%3}.b]\%%)" ^
info:`) ^
DO set %%C
echo ONE_PIXEL=%ONE_PIXEL%
rem ----------------------------------------------------------------
rem Single colour spec eg "srgb(4545,37867,0)", range 0.0 to QuantumRange eg 65535.
FOR /F "usebackq" %%C ^
IN (`%IM%convert %1 ^
-format "ONE_PIXEL=srgb(%%[fx:int(QuantumRange*p{%2,%3}.r+0.5)],%%[fx:int(QuantumRange*p{%2,%3}.g+0.5)],%%[fx:int(QuantumRange*p{%2,%3}.b+0.5)])" ^
info:`) ^
DO set %%C
echo ONE_PIXEL=%ONE_PIXEL%
rem ----------------------------------------------------------------
rem Hash-format, from crop and txt:. HDRI clips this.
%IM%convert %1 ^
-crop 1x1+%2+%3 +repage ^
txt:
FOR /F "usebackq skip=1 tokens=3" %%C ^
IN (`%IM%convert %1 ^
-crop 1x1+%2+%3 ^
txt:`) ^
DO set ONE_PIXEL=%%C
rem The above doesn't work for older versions of IM that had more spaces.
FOR /F "usebackq skip=1 tokens=6 delims=():, " %%C ^
IN (`%IM%convert %1 ^
-alpha off ^
-crop 1x1+%2+%3 ^
-depth 16 ^
txt:`) ^
DO set ONE_PIXEL=%%C
snibgo's IM pages: im.snibgo.com
-
- Posts: 12
- Joined: 2015-04-04T19:06:28-07:00
- Authentication code: 6789
Re: Set and Get RGB values for a specific pixel in an image
Thanks snibgo! This helps a lot.
One last request, please - how would I define a region, say a vertical line of pixels starting at origin (x=0,y=0) and ending at x=1, y=240, thus encompassing 240 pixels all at once, and set all their RGB color values to something specific, like "#5500AC" ?
Thanks everyone again for your help - this assists me in getting the feel of the ImageMagick command line.
One last request, please - how would I define a region, say a vertical line of pixels starting at origin (x=0,y=0) and ending at x=1, y=240, thus encompassing 240 pixels all at once, and set all their RGB color values to something specific, like "#5500AC" ?
Thanks everyone again for your help - this assists me in getting the feel of the ImageMagick command line.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Set and Get RGB values for a specific pixel in an image
Code: Select all
convert wizard: -fill #5500ac -draw "rectangle 0,0 0,239" r.png
My pages contain loads of Windows BAT examples.
snibgo's IM pages: im.snibgo.com
-
- Posts: 12
- Joined: 2015-04-04T19:06:28-07:00
- Authentication code: 6789
Re: Set and Get RGB values for a specific pixel in an image
Many thanks snibgo - very helpful - I'll study your pages carefully!
-
- Posts: 12
- Joined: 2015-04-04T19:06:28-07:00
- Authentication code: 6789
Re: Set and Get RGB values for a specific pixel in an image
I used Gimp to create a PNG image that is all white, 240 px. by 320 px., named "white-background.png". I set the image mode to the RGB color space before I exported it to a PNG image. When I right-click on the image in Windows Explorer and check properties, it says "Bit depth 24", so I've got RGB here.
Then I used this command to try to change the pixel at 0,0 to red:
convert white-background.png -fill "rgb(ff,00,00)" -draw "color 0,0 point" white-background.png
I get no errors, but when I look at the image, the pixel I changed with IM is black, and Properties now says the image has a bit depth of 1, not 24. So I succeeded in changing the PNG to a grayscale - not what I wanted, of course.
Do I need to specify bit depth or mode in my IM command line to get the correct results? What am I doing wrong here?
Thanks for your help!
Then I used this command to try to change the pixel at 0,0 to red:
convert white-background.png -fill "rgb(ff,00,00)" -draw "color 0,0 point" white-background.png
I get no errors, but when I look at the image, the pixel I changed with IM is black, and Properties now says the image has a bit depth of 1, not 24. So I succeeded in changing the PNG to a grayscale - not what I wanted, of course.
Do I need to specify bit depth or mode in my IM command line to get the correct results? What am I doing wrong here?
Thanks for your help!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set and Get RGB values for a specific pixel in an image
Code: Select all
convert white-background.png -fill "rgb(ff,00,00)" -draw "color 0,0 point" white-background.png
see http://www.imagemagick.org/script/color-management.php
-
- Posts: 12
- Joined: 2015-04-04T19:06:28-07:00
- Authentication code: 6789
Re: Set and Get RGB values for a specific pixel in an image
It worked! Thx so much, and sorry for my newbie mistake!