Page 1 of 1
Outputting average color characteristics
Posted: 2008-03-15T20:56:24-07:00
by highlandink
Hi I'm pretty new to imagemagick and I hope what I'm trying to do is relatively simple. I want to generate the average red green blue values for a given image. This seems like it should be as simple as scaling the image to 1x1 pixels and then just outputting the r(0-255) g(0-255) and b(0-255). I'm using ImageMagick through PHP though because I couldn't get IMagick or MagicWand setup right. So for now I'm just having the php execute the ImageMagick commands as strings. I have it working for other functions but this problem is really giving me trouble.
found this fragment:
convert rose: -scale 1x1\! -format '%[pixel:u]' info:-
somewhere and have been trying to work with it, but I'm not great with Unix command line syntax and am having trouble getting it to A) rescale B) output just the RGB values alone.
Any help would be much appreciated!
-Mike
Re: Outputting average color characteristics
Posted: 2008-03-16T14:44:36-07:00
by fmw42
If you are using IM 6.3.9.1+ you can now use string formats to get the average or mean value. See
http://www.imagemagick.org/script/comma ... hp?#format
redave=`convert <image> -channel Red -separate -format "%[mean]" info:`
echo $redave
etc for Green and Blue
Alternately, you need to use -filter box -resize in place of -scale
ave=`convert rose: -filter box -resize 1x1! -format "%[pixel:u]" info:`
echo $ave
The problem with this method is sometimes it gives values as rgb(xxx%,yyy%,zzz%) and sometimes it gives color names
If you want the average value over all 3 channels, you can do:
ave=`convert rose: -filter box -resize 1x1! -format "%[fx:u]" info:`
or under IM 6.3.9.1+
redave=`convert <image> -type Grayscale -format "%[mean]" info:`
Re: Outputting average color characteristics
Posted: 2008-03-16T16:26:30-07:00
by highlandink
thanks for the reply - i was playing around with it again today and it seems like the %[pixel:u] notation to get the pixel information isn't working, I can get -format to output basic information (%s %m) but not anything using the pixel or fx notation. Maybe my version of image magick is old. Do you know of anyway to get pixel data without using that notation.
Re: Outputting average color characteristics
Posted: 2008-03-16T22:09:20-07:00
by anthony
See IM examples, Extracting Colors from images
http://imagemagick.org/Usage/quantize/#extract
The best solution may be to do your -scale to a single pixel then output the image as a txt: or ppm: image That output should be easy to parse.
NOTE: the 'pixel' rgb() format expression, and the comment color strings from txt: or histogram: outputs, should all be directly usable in IM color settings like -fill -background etc. If it is not usable then you have found a bug!
See
-fill for details of the various color specification options.