What does return the %[mean] option?

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?".
Post Reply
GavinL

What does return the %[mean] option?

Post by GavinL »

Hello to everyone,

after searching the whole web and esp. this forum up and down for several days I find no other chance to post a new question here. Sorry to bother you.

Ok, this is the task: I do have tiff images in CMYK format 32bit color depth. What I do need is the information, how much of the pixels are cyan, magenta and yellow. A percentage value is my my first choice.

After reading the IM doc and lots of topics here in the forum and all over the web, it looks as if the following command line would return me exactly what I'm looking for:

Code: Select all

identify -format "%[fx:mean.c]" image.tif
It seems as if this command does return a percentage value of the all cyan pixels related to the total amount of all pixels in the given tiff file. Is this correct? Or what else does the %[mean] option (do you call it option? Or what is it called in terms of IM?) return when I use it? I have learned, that it returns a "mean value", but what does it mean, this "mean value"?

Btw.: perhaps someone can tell me a better way how to solve my task. Again, this is what I need: Let's say there is a tiff image with 300*300 pixels, where 500 of these pixels do have a cyan color part (no matter, how intense the cyan part is, only it is not 0). So I'd need a result, telling me, that the tiff does contain a cyan percentage of 100*500/(300*300) = 0.556%. And if the image does contain 10000 pixels with magenta, so I'd get a result of 100*10000/(300*300) = 11.1%. Thats all I need.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: What does return the %[mean] option?

Post by fmw42 »

identify -format "%[fx:mean.c]" image.tif
'


This just tells you the average value of the cyan channel in the range 0 to 1. If you multiply by 100 you get percent.

However your problem is not solvable, every pixel has a C, M, Y, and K value to it that ranges from 0 to 1 (or from 0 to Quantumrange, ie 255 for Q8 or 65535 for Q16). So there is no way to really say the some pixels are only Cyan.

You can also generate a historgram that will show the distribution of each channel, but that still does not tell you how many Cyan colors there are as there will be a Cyan value for every pixel.


Perhaps I misunderstand your problem. Perhaps you can explain it in some other manner or rephrase it differently.


It looks like you want to know how many pixels of each channel are not zero. To do that separate each channel, threshold at 0 to get all values above 0 as white and value 0 as black. Then for each channel find the mean value and multiply by 100.

cyanpct=`convert image -threshold 0 -format "%[fx:100*mean.c]" info:`
magpct=`convert image -threshold 0 -format "%[fx:100*mean.m]" info:`
yelpct=`convert image -threshold 0 -format "%[fx:100*mean.y]" info:`
echo "cyan=$cyanpct; magenta=$magpct; yellow=$yelpct"


I am assuming that IM -fx will honor cmyk channels, but don't know if that is the case. If not, you can do

cyanpct=`convert image -channel cyan -threshold 0 -format "%[fx:100*mean]" info:`
magpct=`convert image -channel magenta -threshold 0 -format "%[fx:100*mean]" info:`
yelpct=`convert image -channel yellow -threshold 0 -format "%[fx:100*mean]" info:`
echo "cyan=$cyanpct; magenta=$magpct; yellow=$yelpct"


But just because the cyan at some pixel is not 0, does not mean that at the same pixel the magenta and yellow are not zero also.

Perhaps you want to know which pixels are pure cyan. That would be cyan != 0, magenta=0, yellow=0. For that you would need some conditional in the fx statement or to combine three appropriately generated (threshold) masks. If this is more the case, let us know and we can specify how to do this.
GavinL

Re: What does return the %[mean] option?

Post by GavinL »

Hi there,

thanks very much for your reply. I see I've not really made clear, what I need. I'll retry to describe.

Thats what I have: A TIFF file in the CMYK color model (for printing).
Thats what I need: To know, how much color is used for printing this file. I.e. all together the number of pixels which will be printed with C, M and Y altogether.

I don't need the percentage of only the C, M or Y pixels, it only is important, how much color toner will be used to print this page, expressed as a percentage value of e.g. "When printing this image (which is in fact an image sized A4) on a color printer, then 4.2% of the whole page will be covered by color toner".

So it looks as if the solution described by fmw42 with the threshold setting should return the answer to my question. I'll try this:

cyan percentage = 'convert image -threshold 0 -format "%[fx:100*mean.c]" info:'

Etc. for the two other colors and then I can calculate:

Whole color percentage = cyan percentage + magenta percentage + yellow percentage


If I'm wrong, then could you please correct me?

Thanks for your help!
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: What does return the %[mean] option?

Post by GreenKoopa »

I am not sure I understand why you are counting all-or-nothing for color values. Maybe you are trying to get an estimate of the maximum possible ink used. Maybe to charge a customer half for only printing a page that is half color and half blank? Or charge half for a page that contains only black and cyan? Or neared to the b&W charge than full color charge for a page with very little color? In any case, fmw42 recommended using -threshold on each channel to make your image all-or-nothing if that is really what you want.

A good estimate would be difficult. First your printer will convert your CMYK to the printer's own CMYK colorspace adjusting all values somewhat. Then the printer will dither or halftone or something to produce its best approximation of these values. Printer settings and paper & ink choice probably matter. All this may be more complex than you are needing. I don't really understand your goal.

Once you come up with a solution, you will need to test it. A few contrived, simple examples would be needed. If your proposed solution works on these, great. If not, maybe sharing those examples and expected test results would help us understand.
Post Reply