How to get color information

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

Re: How to get color information

Post by fmw42 »

Alternate:

convert image.jpg -fuzz 5% -fill white -opaque white -fill black +opaque white -format '%[fx:100*mean]' info:

gives the actual percent


convert image.jpg -fuzz 5% -fill white -opaque white -fill black +opaque white -format '%[fx:w*h*mean]' info:

gives the actual number of pixels


see
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

anthony wrote:Take the image and mask for white!

Code: Select all

  convert image.jpg  -fuzz 5% -fill white -opaque white -fill black +opaque white -format '%[mean]' info:
I've just found IM escapes page, but no explanation for %[mean] ... What does it mean?
http://www.imagemagick.org/script/escape.php

Edit note:
As usually, I have overlooked your second post on new page.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get color information

Post by fmw42 »

It means that you get the mean or average of all the pixels in the image.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to get color information

Post by anthony »

mean or average of all pixel VALUES :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

RE: 1) Test if image contains white

Post by VanGog »

RE: 1)
anthony wrote:Take the image and mask for white!

Code: Select all

  convert image.jpg  -fuzz 5% -fill white -opaque white -fill black +opaque white -format '%[mean]' info:
if you get a number that is not 0 (all black) you have some colors within 5% of white!
The value is the fraction of white times 2^16.

See IM Examples, Quantization, Extracting colors (in various ways)
http://www.imagemagick.org/Usage/quantize/#handling
Sorry, but you mixed both points 1) and 2) together. But they are separate request, with different conditions.

In the point 1)
I ask how to find color for exact coordinates. I cannot use your command, simple because every image contains some traces of white. But I want to be sure, that the image is whole in white. Well, maybe we can do it in different way. What about to make a test, if image contains 4096*4096 px of white? I can print the number of pixels and I will make test in batch. If yes, then I will generate warning sound and make pause. So this point is solved.

And the point 2)
I realized that I need not to use 4 blue brush strokes. I need only to mark upper left and bottom right corner of the selection (I do it in raster editor). And then...

Here in IM, I need to use fuzz but to get the blue color. I would like to get the coordinates of the most far blue pixel of bottom top from the group of blue pixels and to get the most far blue pixel of the bottom right corner from the next group of blue pixels.

I have this code:

Code: Select all

convert test.jpg  -fuzz 30% -fill black +opaque #1700ff -fill  blue -opaque #1700ff txt:- | grep blue
Could we change so that txt: will generate only blue pixels (blue channel)? So no need of grep. I would be more glad if the command would be indepentend on linux utilities.

I tried this command:

Code: Select all

convert test.jpg  -fuzz 30% -fill black +
opaque #1700ff -fill  blue -opaque #1700ff -channel B  testik.gif txt:-
But the black is still there. Maybe you have some command to erase pixels?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get color information

Post by fmw42 »

If the %[fx:mean] is 1 (as fraction) or %[fx:100*mean] is 100 (as percent) or %[mean] is 65535 (Q16) or 256 (Q8), then the image is pure white. As I said above you can find out how many pixels are white using %[fx:w*h*mean]. The rest are not white.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

OK. Your solution is also interesting. So point one is solved.
Please let's close the point one and move to point two. I would like to limit count of pixels in IM, if possible. Is it possible to find first row of blue pixels, and last row of blue pixels and generate new image with this rows? If yes, then it would be simple to work with it in batch.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get color information

Post by fmw42 »

VanGog wrote:I would like to limit count of pixels in IM, if possible. Is it possible to find first row of blue pixels, and last row of blue pixels and generate new image with this rows? If yes, then it would be simple to work with it in batch.

I am not sure I understand. Do you want only two rows of data? Do the rows have to be completely blue or only partially blue, that is some pixels not blue, but at least one pixel blue? Or do you want all data from the first row of some blue to the last row of some blue.

If the latter, change all pixels that are not blue to some other color, say white or black. The use -trim +repage. That will crop the image to the minimum bounding box that contains any blue.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

I have added the -trim +repage command to my code. It looks like I have what I wanted but I have one more problem - the trim command also cropped the canvas. But I need the original canvas to be kept, similar like crop command does. I need the information about how much the image was cropped (shifted away of top and left edges of the canvas).

Here just few photos from what I wanted. I wanted to obtain the information from the corner pixels of the blue brush strokes:

The blue color strokes in image:
Image

The blue rows needed to be extracted:
Image

The blue pixels which are in corners I am interested about:
Image

And this is the result:
Image

I thought about to extract first and last row. If I would create new document 256x2 px and place there the first and last row so the batch command and process would be much more simple. I actually need to extract txt values from the first and last line. But is not useless to proceed all rows into for loop?
Last edited by VanGog on 2012-04-10T15:46:19-07:00, edited 3 times in total.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

BTW: Why is it necessary always to use white or black for background? It must take space of the image. Why the image cannot contain just the blue pixels to get less image size?

What if I would decide instead of manually drawing the blue brush strokes, to use xml file to get coordinates for dots? I would draw them automatically. Then I would trim the image, to get correct rectangular area.

Code: Select all

convert -size 250x250 xc:white -draw "point 20,15 point 150,25 point 200,140 point 15,125" -trim +repage -bordercolor black -border 7 dots.gif
Also I added a border around the trimmed area because I would like to be sure that I did not cropped it too much. But I found one more problem. The image was cropped so that the canvas was cropped and that is not the goal. I need to keep information of original canvas size.

Code: Select all

convert  dots.gif histogram:- | identify -
-=>C:/DOCUME~1/HOST~1/LOCALS~1/Temp/magick-pDwfYQVO MIFF 256x200 256x200+0+0 8-bit Grayscale DirectClass 3.47KB 0.000u 0:00.000
So how to do the trim without cropping of the canvas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get color information

Post by fmw42 »

This gives you the trimmed size and offset from your original image. From there you can compute the top left x,y and bottom right x,y



convert s0vhalx20vn4txls8tv.jpg -trim -format "%wx%h%O" info:
160x192+48+32

See string format at
http://www.imagemagick.org/script/escape.php
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

So must I to output the information about canvas and offset just after cropping of the image? I thought that the information is saved into the gif image. If the first is true then how can I trim, save file and output info: in one command. Is it possible? Or must I call the convert twice?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get color information

Post by fmw42 »

try

convert s0vhalx20vn4txls8tv.jpg -fuzz 10% -trim -format "%P%O" -write info: +repage s0vhalx20vn4txls8tv_trim.png


The -fuzz 10% is needed because your input is jpg and the white and blue are not solid colors. There are slightly different near white and blue values (esp white), so you need to use the fuzz to allow the trim to get rid of all near white values.

The +repage removes the virtual canvas after you have written it to the terminal.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

I think it is great. Thanks.

One more idea. Imagine situation. I have xml file, where I have the dots that I want to draw. It is similar to the previous, but I will draw the dots instead drawing brush strokes manually:

Image

These dots are not exactly in rectangular orientation, therefor I want to trim them to get rectangular crop.

Code: Select all

convert -size 250x250 xc:white -draw "point 20,15 point 150,25 point 200,140 point 15,125" -bordercolor black -border 7  -trim +repage -format "%P%O" -write info: output.gif
The border is save area around the pixels, otherwise the image could be cropped too much (there is some inaccuracy in the coordinates saved in xml file). But in this case I have problem, that I do not see correct %P and %O displayed.

Output is 0x0+0+0

And this command without the repage:

Code: Select all

convert -size 250x250 xc:white -draw "point 20,15 point 150,25 point 200,140 point 15,125" -bordercolor black -border 7 -trim  -format "%P%O" -write info: output.gif
264x264+7+7
Even returns greater image size then the original canvas is! Is it because I added the border?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to get color information

Post by anthony »

Make sure you add +repage after you retrieve the trim information. That removes the virtual canvas and offset.

You may like to look over, Drawing Symbols
http://www.imagemagick.org/Usage/draw/#symbols

It shows a number of methods of drawing more than just a single 'dot' :-)

Note drawing a larger 'dot' is particulalry easy using the 'strokewidth' technique showing in the 'circles' section.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply