0
votes

I'm using imagemagick to extract a pixel color @ some x,y coordinate in a jpeg image.

The output most of the time is srgb(R,G,B) but in some cases it returns grey##.

Since I'll need the plain RGB numbers for a calculation, grey## is not a good value.

Is it possible to always get RGB values from imagemagick?

The command:

magick image.jpg -format "%[pixel:p{110,246}]" info:-

1

1 Answers

1
votes

What you want to do is compute each color channel separately in ImageMagick fx: computation.

magick image.jpg[1x1+110+245] -format "%[fx:round(255*u.r)],%[fx:round(255*u.g)],%[fx:round(255*u.b)]" info:


That will produce for example:

enter image description here

magick lena.jpg[1x1+110+245] -format "%[fx:round(255*u.r)],%[fx:round(255*u.g)],%[fx:round(255*u.b)]" info:


208,90,88

If you want to include rgb(), then do

magick image.jpg[1x1+110+245] -format "rgb(%[fx:round(255*u.r)],%[fx:round(255*u.g)],%[fx:round(255*u.b)])" info:


magick lena.jpg[1x1+110+245] -format "rgb(%[fx:round(255*u.r)],%[fx:round(255*u.g)],%[fx:round(255*u.b)])" info:


rgb(208,90,88)

ADDITION:

If you want multiple pixels returned, then you can do the following:

magick lena.jpg -format "rgb(%[fx:round(255*u.p{10,10}.r)],%[fx:round(255*u.p{10,10}.g)],%[fx:round(255*u.p{10,10}.b)]) rgb(%[fx:round(255*u.p{100,100}.r)],%[fx:round(255*u.p{100,100}.g)],%[fx:round(255*u.p{100,100}.b)])\n" info:


rgb(226,130,106) rgb(131,46,85)