Page 1 of 1

How to get the color of a pixel?

Posted: 2010-03-31T18:26:53-07:00
by stumpy
How do I get IM to tell me what the color is at pixel x,y?

And, how do I get IM to set -fill (or whatever) to that color without me having to type it in?

Thanks for a great free software.

Re: How to get the color of a pixel?

Posted: 2010-03-31T18:48:58-07:00
by fmw42
unfortunately you need two steps. in unix it would be (to get pixel 0,0)

# create a constant color image
color=`convert image.png -format "%[pixel: u.p{0,0}]" info:`
convert -size 100x100 xc:"$color" swatch.png

or

# replace that color with white
color=`convert image.png -format "%[pixel: u.p{0,0}]" info:`
convert image.png -fill white -opaque "$color" image2.png

see http://www.imagemagick.org/Usage/transform/#fx_escapes

for windows users see http://www.imagemagick.org/Usage/windows/

Re: How to get the color of a pixel?

Posted: 2010-04-01T12:16:20-07:00
by stumpy
I see. So in windows it would be:

for /f %%i in ('convert image.png -format "%[pixel: u.p{0,0}]" info:') do set color=%%i
convert image.png -fill white -opaque "%color%" image2.png

Thanks. One more thing. Can you get it to say the color in the "#rrggbb" format?

Re: How to get the color of a pixel?

Posted: 2010-04-01T16:57:44-07:00
by fmw42
One more thing. Can you get it to say the color in the "#rrggbb" format?
In unix, you would do to get pixel 0,0

color=`convert image.png[1x1+0+0] -depth 8 txt: | tail -n +2 | sed -n 's/^.*\(#[^ ]*\).*$/\1/p'`


see http://www.imagemagick.org/Usage/files/#txt