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?".

Re: How to get color information

Postby fmw42 » 2012-04-08T17:51:55+00:00

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
User avatar
fmw42
 
Posts: 14813
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get color information

Postby VanGog » 2012-04-09T04:53:47+00:00

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.
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby fmw42 » 2012-04-09T11:38:34+00:00

It means that you get the mean or average of all the pixels in the image.
User avatar
fmw42
 
Posts: 14813
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get color information

Postby anthony » 2012-04-09T18:33:31+00:00

mean or average of all pixel VALUES :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8729
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

RE: 1) Test if image contains white

Postby VanGog » 2012-04-10T02:27:50+00:00

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?
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby fmw42 » 2012-04-10T10:19:57+00:00

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.
User avatar
fmw42
 
Posts: 14813
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get color information

Postby VanGog » 2012-04-10T11:33:22+00:00

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.
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby fmw42 » 2012-04-10T11:48:41+00:00

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.
User avatar
fmw42
 
Posts: 14813
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get color information

Postby VanGog » 2012-04-10T13:13:41+00:00

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+00:00, edited 3 times in total.
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby VanGog » 2012-04-10T14:04:42+00:00

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?
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby fmw42 » 2012-04-10T16:13:48+00:00

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
User avatar
fmw42
 
Posts: 14813
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get color information

Postby VanGog » 2012-04-11T02:08:14+00:00

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?
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby fmw42 » 2012-04-11T10:22:17+00:00

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.
User avatar
fmw42
 
Posts: 14813
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: How to get color information

Postby VanGog » 2012-04-11T11:31:17+00:00

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?
VanGog
 
Posts: 278
Joined: 2012-02-05T02:46:58+00:00

Re: How to get color information

Postby anthony » 2012-04-11T20:31:31+00:00

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
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8729
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

PreviousNext

Return to Users

Who is online

Users browsing this forum: Google [Bot] and 8 guests