[magick-users] Reading a single pixel from the command line

Anthony Thyssen anthony at griffith.edu.au
Sun Oct 28 04:34:54 PDT 2007


Nathan Wilson on  wrote...
| Given an x,y location is there an efficient way to get the RGB values
| from a JPEG using the standard ImageMagick command line tools?
|
| I'm looking for something equivalent to:
|
| % color_picker foo.jpg 46 47
| 206 159 129
|
IM has two ways to do this.

On is using -format with either -identify  or info:

The former allows you to specify a pixel in the middle of a image
process, and either output anither pixel or do other work on the image
afterward.

See http://imagemagick.org/Usage/transform/#fx_escapes
However it is not very nice, and not recommended


The better way is to output a txt: image...
  http://imagemagick.org/Usage/files/#txt
either of all the pixel, or a -crop of just the one pixel
For example...
   convert foo.jpg -crop 1x1+46+47 txt:-

Which will return something like....
   # ImageMagick pixel enumeration: 1,1,65535,rgb
   0,0: (20560,15163,11051)  #50503B3B2B2B  rgb(80,59,43)

You can set the output quality using -depth  for example
   convert foo.jpg -crop 1x1+46+47 -depth 8 txt:-

Which returns something like...
   # ImageMagick pixel enumeration: 1,1,255,rgb
   0,0: ( 80, 59, 43)  #503B2B  rgb(80,59,43)

The number in parenthesis is the numbers you want.
And four numbers will be returned for images with semi-transparency.

NOTE the 'rgb(...)' part is not guranteed and may return.
a color name.  It and the '#' method represents a ways of requesting
that pixel color on the IM command line.

For example...
   0,0: (255,255,255)  #FFFFFF  white


| In case you're curious this is for a color picker within a web page.
| The site already uses ImageMagick command line tools to resize
| images, so I'm hoping there's an easy way to do this without creating
| a new dependency.  For the moment I have a hand written C program
| that is working, but that complicates the portability of the website.
|
That is very good, and follows my general Scripting advise..
   http://imagemagick.org/Usage/api/#scripts


  Anthony Thyssen ( System Programmer )    <A.Thyssen at griffith.edu.au>
 -----------------------------------------------------------------------------
       Rincewind was firmly of the opinion that in stone-aged days,
     people were divided into two groups. Those that, when confronted
     by a sabre-toothed tiger, ran away (and thereby survived) and
     those who just stood there saying 'my, what a magnificent brute'
     or 'here, kitty, kitty'.         - Terry Prachett, "The Color of Magic"
 -----------------------------------------------------------------------------
     Anthony's Home is his Castle     http://www.cit.gu.edu.au/~anthony/


More information about the Magick-users mailing list