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?".
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

How to get color information

Post by VanGog »

Could you please help me with this? I need to do two simple batch scripts to test color information in image. So the output of the result should contain some information for the CMD.

1) (Solved) I want to test if file contains some pixels of white and to print some result. Possibly yes or no, or just print nothing if he has not the information. So I would ask for certain position in the image like: 0,0; 100,100; 500,500; 1000,1000 ... four points are enough to ensure that the image is not just white.

and second thing
2) (unsolved) I look for 4 groups of blue color: 1800ff. Every group has few of blue pixels, like if you made a stroke with a brush. So there will be at least 15px diameter of the pixel group. Is it possible to print coordinates for it? If not possible to find the group of pixels of given color, never mind, I would do it without it, just to print all the blue color pixels and I try to calculate with it. (Edit: Task has changed a bit. Now I need only to find pixel of the upper left corner from the blue brush stroke, and the lower right corner from the blue brush stroke. I think I could find the coordinates more simple in the output of the txt: command, if I could separate first line with blue pixels and last blue line of blue pixels. So to get image 256px x2 px.
Last edited by VanGog on 2012-04-10T02:59:01-07:00, edited 3 times in total.
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 »

convert your image to txt: format and filter for a color by piping to grep.

convert yourimage: txt:- | grep "somecolor"

somecolor can be coloname, hex value such as #1800ff or rgb(r,g,b)

Example:

convert rose: txt:- | grep "white"

61,11: (255,255,255) #FFFFFF white
62,15: (255,255,255) #FFFFFF white
62,16: (255,255,255) #FFFFFF white
63,16: (255,255,255) #FFFFFF white
14,37: (255,255,255) #FFFFFF white
12,38: (255,255,255) #FFFFFF white
13,38: (255,255,255) #FFFFFF white
23,39: (255,255,255) #FFFFFF white
12,40: (255,255,255) #FFFFFF white
24,40: (255,255,255) #FFFFFF white
25,40: (255,255,255) #FFFFFF white
11,41: (255,255,255) #FFFFFF white
12,41: (255,255,255) #FFFFFF white
21,41: (255,255,255) #FFFFFF white
26,41: (255,255,255) #FFFFFF white
20,42: (255,255,255) #FFFFFF white
28,42: (255,255,255) #FFFFFF white
29,43: (255,255,255) #FFFFFF white
18,44: (255,255,255) #FFFFFF white
30,44: (255,255,255) #FFFFFF white
31,44: (255,255,255) #FFFFFF white
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

One thing I did not realize. In the point 2) I forgot, that jpeg images have not exact color, but color in some range. So I need to find other way how to get the range of colors. When I click on the blue color from the saved image, so there is Y:0 and K:. Maybe this would be way how to filter it?
Last edited by VanGog on 2012-04-07T14:35:22-07:00, edited 1 time in total.
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 »

Probably easier is just to list out the histogram. see http://www.imagemagick.org/Usage/files/#histogram
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 »

To get near colors use -fuzz xx% -fill black +opaque somecolor -fill somecolor -opaque somecolor . That will turn all the near colors into that color and the rest will become black.

convert image -fuzz xx% -fill black +opaque somecolor -fill somecolor -opaque somecolor txt:- | grep "somecolor"


see http://www.imagemagick.org/Usage/color_basics/#replace
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

The histogram is interesting function. It can generate "image" of the colors like PS levels. This could be solution to my previous question "color analyzer" viewtopic.php?f=1&t=20695&p=83054#p83054...
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

Is it possible to combine this two lines to one line?

Code: Select all

convert test.jpg -define histogram:unique-colors=false histogram:histogram.gif
convert histogram.gif -strip -resize 50% -separate  histogram-%d.gif
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:Is it possible to combine this two lines to one line?

Code: Select all

convert test.jpg -define histogram:unique-colors=false histogram:histogram.gif
convert histogram.gif -strip -resize 50% -separate  histogram-%d.gif

Only way I know is via a pipe.


convert rose: -define histogram:unique-colors=false histogram:- | convert - -strip -resize 50% -separate histogram-%d.gif

I don't believe you need to add -strip.
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 you don't include the -resize, you can do this

convert rose: -separate -define histogram:unique-colors=false histogram:histogram-%d.gif
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

Now I read about mpr but it does not work for me:

Code: Select all

convert test.jpg -define histogram:unique-colors=false +delete -write histogram:mpr:histogr mpr:histogr -strip -resize 50% -separate  histogram_buffer-%d.gif
Error:

Code: Select all

convert test.jpg -define histogram:unique
-colors=false +delete -write histogram:mpr:histogram_buffer mpr:histogram_buffer
 -strip -resize 50% -separate  mpr:histogram_buffer-%d.gif
convert.exe: missing an image filename `mpr:histogram_buffer-%d.gif' @ error/con
vert.c/ConvertImageCommand/3016.
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 »

Yes I had forgotten about mpr. You just have your +delete too early. You are deleting the input image before you create the mpr: image. try

convert test.jpg -define histogram:unique-colors=false -write histogram:mpr:histogr +delete mpr:histogr -strip -resize 50% -separate histogram_buffer-%d.gif
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to get color information

Post by VanGog »

The MPR function is awesome!

Code: Select all

convert test.jpg -fuzz 20% -fill black +opaque #1700ff -fill  #1700ff -opaque #1700ff testik.jpg
Image

I forgot I need only upper left corner and lower right corner of the blue areas. But could you get only first line of the blue and last line of the blue? The image could be two lines of pixels only. So it would be much more simple to get the information about minimum and maximum (by batch), to get and calculate four coordinates for points.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

crop image borders based on color

Post by VanGog »

Maybe it would be better to do it in this way:
"crop image borders based on color"
viewtopic.php?f=1&t=12066

convert <image> -trim +repage <result>
convert <image> -fuzz xx% -trim +repage <result>

I just don't understand where should I insert the color information (#1700ff):

Code: Select all

convert test.jpg -trim +repage result.jpg
convert test.jpg -fuzz 20% -trim +repage result2.jpg
To crop the image in the way that the blue will stay in image.
But the final goal is

either to print the coordinates for the corners, so is it really necessary to crop it? (Or is there any other function, that can simulate cropping around the blue border and just get the information about the corners position? I would like to crop the image, get information for corners and than I can delete this image and crop the original image.

Or to fill the image after it is cropped or generate empty image in this size and this use as mask to crop original? But still I need to print the coordinates for the batch because I need to write/save coordinates to .txt file.

What solution to choose?
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 »

trim does not take a color value. it finds the colors from the four corners of 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 »

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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply