Page 1 of 1

Image histogram options

Posted: 2008-09-10T17:06:49-07:00
by dizwell
I use the command

Code: Select all

convert penguinsonice.bmp -format %c histogram:info:- > penguins2.txt
to output an image's histogram to a text file.

Is it possible to (a) only output the (say) 10 top lines of the historgram (i.e., only document the 10 most common colours in an image); and (b) to get the image filename included in the output, somewhere?

I'm running the latest Imagemagick on Windows XP.

Re: Image histogram options

Posted: 2008-09-10T18:20:20-07:00
by fmw42
dizwell wrote:I use the command

Code: Select all

convert penguinsonice.bmp -format %c histogram:info:- > penguins2.txt
to output an image's histogram to a text file.

Is it possible to (a) only output the (say) 10 top lines of the historgram (i.e., only document the 10 most common colours in an image); and (b) to get the image filename included in the output, somewhere?

I'm running the latest Imagemagick on Windows XP.

I do not know exactly what you need to do to change this for Windows, but this works in Unix. Anthony can give you a more compact method, I am sure.

convert rose: -colors 256 -depth 8 -verbose info: | \
sed -n '1p; /Histogram:/,/Colormap:/p' | \
head -n 12 > rose_hist.txt

If you just want 10 lines from the histogram without the image name (or the title Histogram), then you can use:

convert rose: -colors 256 -depth 8 -format "%c" histogram:info: | \
head -n 10 > rose_hist.txt

Re: Image histogram options

Posted: 2008-09-10T21:26:19-07:00
by dizwell
Thank you so much for taking the time to reply.

I did try your solution using Cygwin (I didn't have a lot of choice given the call to sed in there!), but without joy:

Code: Select all

hrogers@PLPC358 ~/Pictures
$ convert penguinsonice.bmp: -colours 256 -depth 8 -verbose info: | \
> sed -n '1p; /Histogram:/,/Colormap:/p' | \
> head -n 12 > penguins2.txt
convert: unable to open image `penguinsonice.bmp:': No such file or directory.
convert: unrecognized option `-colours'.

hrogers@PLPC358 ~/Pictures
$ ls penguinsonice.bmp
penguinsonice.bmp
I wasn't entirely certain whether the character you used after "sed -n" was a normal single quote (') or one of those weird ones on the key next to 1 ("`"). I tried it with both. Both blew up!

I'm not wedded to the DOS-like command line: happy to use Bash, Sed and the rest in their Cygwin form, but Windows is, unfortunately, not negotiable at this time! :-(

Anyway, if you can see where I've gone wrong, or can come up with an alternative suggestion, I would be very grateful.

Re: Image histogram options

Posted: 2008-09-10T21:47:26-07:00
by fmw42
I am no expert on Windows (use Mac OSX brand Unix mostly).

The quote was a standard single quote, but I think you can also use double quotes also. It is not a slant quote.

Windows has some issues with needing different escapes and other things. So you might be best looking at

http://www.imagemagick.org/Usage/api/#windows


I think the following are your two problems per the error messages:

1) You don't want a colon after bmp in your filename? The colon is used only for IM special images such as rose:
(see http://www.imagemagick.org/script/forma ... tin-images)

2) Also you need to use -colors not -colours! (US English not British/Australian/Canadian English)

Hope this helps.

P.S. If you don't want to use sed, you can also do it like this:

imagefile="rose.jpg"
topname=$(convert "$imagefile" -format "%t" info:)
echo "$imagefile Histogram:" > ${topname}_hist.txt
convert "$imagefile" -colors 256 -depth 8 -format "%c" histogram:info: | head -n 10 >> ${topname}_hist.txt

Note:
topname=$(convert "$imagefile" -format "%t" info:)

can also be written as

topname=`convert "$imagefile" -format "%t" info:`

using slant quotes

Result will be rose_hist.txt

rose.jpg Histogram:
48: (105, 99, 83) #696353 rgb(105,99,83)
48: (248,250,247) #F8FAF7 rgb(248,250,247)
43: ( 44, 43, 42) #2C2B2A rgb(44,43,42)
39: (216, 69, 60) #D8453C rgb(216,69,60)
34: ( 55, 44, 39) #372C27 rgb(55,44,39)
34: ( 57, 70, 55) #394637 rgb(57,70,55)
32: ( 40, 54, 40) #283628 rgb(40,54,40)
32: (119, 56, 42) #77382A rgb(119,56,42)
31: (106,146, 74) #6A924A rgb(106,146,74)
29: ( 54, 44, 51) #362C33 rgb(54,44,51)

Note indents of histogram lines will not show above, but will be in the file

Or you can do:

imagefile="rose.jpg"
topname=$(convert "$imagefile" -format "%t" info:)
echo "$imagefile" > ${topname}_hist.txt
echo "Histogram:" >> ${topname}_hist.txt
convert "$imagefile" -colors 256 -depth 8 -format "%c" histogram:info: | head -n 10 >> ${topname}_hist.txt

results will be:

rose.jpg
Histogram:
48: (105, 99, 83) #696353 rgb(105,99,83)
48: (248,250,247) #F8FAF7 rgb(248,250,247)
43: ( 44, 43, 42) #2C2B2A rgb(44,43,42)
39: (216, 69, 60) #D8453C rgb(216,69,60)
34: ( 55, 44, 39) #372C27 rgb(55,44,39)
34: ( 57, 70, 55) #394637 rgb(57,70,55)
32: ( 40, 54, 40) #283628 rgb(40,54,40)
32: (119, 56, 42) #77382A rgb(119,56,42)
31: (106,146, 74) #6A924A rgb(106,146,74)
29: ( 54, 44, 51) #362C33 rgb(54,44,51)

where %t is the top of the filename (suffix removed)
see http://www.imagemagick.org/script/escape.php


This puts it all in one long (awkward?) command:

echo "rose.jpg"$'\n'"Histogram"$'\n'"`convert rose.jpg -colors 256 -depth 8 -format "%c" histogram:info:`" | head -n 12

Re: Image histogram options

Posted: 2008-09-11T15:41:45-07:00
by dizwell
Well, first off: my apologies for a silly typo! With the removal of the colon and the u in 'colours', it all worked just as you said it would:

Code: Select all

$ convert penguinsonice.bmp -colors 256 -depth 8 -verbose info: | sed -n '1p; /
Histogram:/,/Colormap:/p' | head -n 12 > mypenguins.txt
...which got me this:

Image: penguinsonice.bmp
Histogram:
83455: ( 35, 56, 78) #23384E rgb(35,56,78)
75029: ( 34, 54, 75) #22364B rgb(34,54,75)
62965: ( 58, 85,107) #3A556B rgb(58,85,107)
46406: (209,214,218) #D1D6DA rgb(209,214,218)
39141: ( 60, 89,110) #3C596E rgb(60,89,110)
39072: ( 31, 58, 82) #1F3A52 rgb(31,58,82)
33244: ( 53, 81,102) #355166 rgb(53,81,102)
32626: ( 54, 82,105) #365269 rgb(54,82,105)
32566: ( 51, 77, 99) #334D63 rgb(51,77,99)
32511: (203,210,214) #CBD2D6 rgb(203,210,214)

...which is just exactly what I was after. So thank you very much indeed.

Inevitably, however, there are a couple of follow-ups.

1. Your syntax says 'colors 256, depth 8'. The image being analysed here is 24-bit depth, which means lord knows how many colours. Does that difference affect the analysis? I tried setting 'depth' to 24, but the resulting output was just an empty text file.

2. Is there any way for Imagemagick to be less than precise about the colours it describes? What I mean is that if you were to look up the colours (53, 81, 102) and (54, 82, 105), you'd be hard-pressed (I think) to notice the difference. If I am trying to work out the general 'tone' of an image, or the top ten most common 'colour ranges', small differences in specific RGB values are of no real interest to me. Can Imagemagick be commanded to 'allow a tolerance of 2%' or something similar, and then output the ten remaining 'fudged colours'?

I am already grateful for your help to date, though, and don't want to push my luck! But any pointers on those two issues would be much appreciated, too.

Re: Image histogram options

Posted: 2008-09-11T17:12:10-07:00
by fmw42
dizwell wrote:Well, first off: my apologies for a silly typo! With the removal of the colon and the u in 'colours', it all worked just as
Inevitably, however, there are a couple of follow-ups.

1. Your syntax says 'colors 256, depth 8'. The image being analysed here is 24-bit depth, which means lord knows how many colours. Does that difference affect the analysis? I tried setting 'depth' to 24, but the resulting output was just an empty text file.

2. Is there any way for Imagemagick to be less than precise about the colours it describes? What I mean is that if you were to look up the colours (53, 81, 102) and (54, 82, 105), you'd be hard-pressed (I think) to notice the difference. If I am trying to work out the general 'tone' of an image, or the top ten most common 'colour ranges', small differences in specific RGB values are of no real interest to me. Can Imagemagick be commanded to 'allow a tolerance of 2%' or something similar, and then output the ten remaining 'fudged colours'?

I am already grateful for your help to date, though, and don't want to push my luck! But any pointers on those two issues would be much appreciated, too.
1) IM can only create histograms for up to 1024 colors. So the best you could do would be:

convert penguinsonice.bmp -colors 1025 -verbose info: | sed -n '1p; /
Histogram:/,/Colormap:/p' | head -n 12 > mypenguins.txt

If you are on Q16 IM, then -depth 16 would not be needed.

2) To be less precise, the best you can do is set the colors to the number of colors you want. For example if you wanted the image to be represented by only 10 colors, you could do

convert penguinsonice.bmp -colors 10 -verbose info: | sed -n '1p; /
Histogram:/,/Colormap:/p' | head -n 12 > mypenguins.txt

That clusters the colors into 10 groups and the histogram should only have 10 bins in it.

I don't believe that you can use -fuzz in generating a histogram. But will defer to Anthony or Magick on that issue. They may be able to advise you better with regard to this question.

You may want to look at:
http://www.imagemagick.org/Usage/quantize/