How to get the color of a pixel?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
stumpy

How to get the color of a pixel?

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get the color of a pixel?

Post 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/
stumpy

Re: How to get the color of a pixel?

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get the color of a pixel?

Post 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
Post Reply