Page 1 of 1
Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-10T06:53:34-07:00
by codeswitch
I can do nothing about it, my hoster only provides the version 6.2.4.
What I need to do is extracting the ten most used colors in a jpg.
I appreciate your help very much!
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-10T10:39:59-07:00
by fmw42
generate the histogram to txt format and sort it and then list the top 10 counts to see what colors they are.
for example for the internal IM image, logo:
convert logo: -format %c histogram:info:- | sort -k 1 -r | head -n 10
253350: (255,255,255) #FFFFFF white
10877: ( 2, 90,164) #025AA4 rgb(2,90,164)
2244: ( 49, 51,125) #31337D rgb(49,51,125)
2063: (251,246,245) #FBF6F5 rgb(251,246,245)
1500: (234, 43, 44) #EA2B2C rgb(234,43,44)
1070: ( 34, 28, 27) #221C1B rgb(34,28,27)
1065: (241,165,125) #F1A57D rgb(241,165,125)
898: ( 36, 32, 33) #242021 rgb(36,32,33)
862: ( 28, 25, 27) #1C191B rgb(28,25,27)
715: (249,234, 77) #F9EA4D rgb(249,234,77)
Best that I can do as the IM function -unique-colors only came out at version 6.2.8-8
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-11T06:30:19-07:00
by codeswitch
Thanks, but it doesn't put out exactly what I need. By using your code I get this:
Code: Select all
id=ImageMagick version=1.0
class=DirectClass colors=0 matte=False
columns=256 rows=200 depth=8
colorspace=RGB
compression=JPEG quality=75
resolution=72x72
Comment={ 15044: ( 0, 0, 0) black
43: ( 1, 1, 1) #010101
325: ( 1, 0, 0) #010000
105: ( 0, 1, 0) #000100
14: ( 1, 1, 0) #010100
59: ( 1, 0, 2) #010002
127: ( 0, 0, 2) #000002
24: ( 0, 1, 2) #000102
6: ( 1, 1, 3) #010103
4: ( 0, 1, 3) #000103
25: ( 0, 2, 0) #000200
9: ( 0, 3, 0) #000300
7: ( 1, 2, 0) #010200
5: ( 0, 2, 1) #000201
1: ( 1, 3, 0) #010300
2: ( 0, 2, 2) #000202
... and many many more rows
As I understand it, the histogram isn't sorted. Please help me out, I only need this one more hint
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-11T11:09:28-07:00
by fmw42
First you did not show your exact command. If you used what I provided then the pipe | to sort will do the sort (-k 1 filters on the first field and -r reverses the order) and the pipe to head will report only the first 10 lines. If you are on Unix, did you not use that exactly as I specified?
convert logo: -format %c histogram:info:- | sort -k 1 -r | head -n 10
If you are on Windows, then there should be some unix equivalent s/w that can provide the equivalent (of sort at the least) or perhaps there is a sort command built into Windows/DOS. Unfortunately, I know little about Windows. Perhaps a Windows user can fill you in further.
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-11T12:09:10-07:00
by codeswitch
I used exactly your posted command on the server itself via SSH.
Committing your command, the server writes the file named info:- . When I open it (after renaming it to info.txt) I see what I stated before. I tried it also with a real image on the server, with the same result.
Mean no offense, but did you try it yourself? Maybe there's a wrong spelling in the command.
What does :- mean behind histogram:info?
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-11T15:05:52-07:00
by fmw42
it means to send to standard output so that the pipe command will take the textual result and pass it to the sort command.
if you modified my command to write to txt, then it won't process the pipe to the sort, which is likely why you are not getting it sorted.
Running the command again:
convert logo: -format %c histogram:info:- | sort -k 1 -r | head -n 10
253350: (255,255,255) #FFFFFF white
10877: ( 2, 90,164) #025AA4 rgb(2,90,164)
2244: ( 49, 51,125) #31337D rgb(49,51,125)
2063: (251,246,245) #FBF6F5 rgb(251,246,245)
1500: (234, 43, 44) #EA2B2C rgb(234,43,44)
1070: ( 34, 28, 27) #221C1B rgb(34,28,27)
1065: (241,165,125) #F1A57D rgb(241,165,125)
898: ( 36, 32, 33) #242021 rgb(36,32,33)
862: ( 28, 25, 27) #1C191B rgb(28,25,27)
715: (249,234, 77) #F9EA4D rgb(249,234,77)
Gives the sorted output. The output from histogram:info:- is piped to the input of sort and sorted and then piped to the head command to tell it to list only the first 10 lines or 10 top most used colors. The minus after info:- often can be left out on some systems and it will still work. But best to keep it.
You can also do it in two steps:
convert logo: -format %c histogram:info:hist.txt
cat hist.txt | sort -k 1 -r | head -n 10
253350: (255,255,255) #FFFFFF white
10877: ( 2, 90,164) #025AA4 rgb(2,90,164)
2244: ( 49, 51,125) #31337D rgb(49,51,125)
2063: (251,246,245) #FBF6F5 rgb(251,246,245)
1500: (234, 43, 44) #EA2B2C rgb(234,43,44)
1070: ( 34, 28, 27) #221C1B rgb(34,28,27)
1065: (241,165,125) #F1A57D rgb(241,165,125)
898: ( 36, 32, 33) #242021 rgb(36,32,33)
862: ( 28, 25, 27) #1C191B rgb(28,25,27)
715: (249,234, 77) #F9EA4D rgb(249,234,77)
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-13T18:02:00-07:00
by anthony
Useless use of cat....
Code: Select all
sort -k 1 -r hist.txt | head -n 10
However you have SSH access. If the host provided compiler support, you could generate a personal version of IM...
See
http://www.imagemagick.org/Usage/api/#personal
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-13T18:33:30-07:00
by fmw42
Presumably you have access to IM on your server/computer to which you are SSH. So perhaps you can use PHP and exec via your browser to process the image with IM to get the histogram into a text file and then if PHP has a sort command, you can then further sort it and keep only the first 10 lines. Might be worth looking into. I am not a PHP expert on these matters, so perhaps another can comment if this has any merit.
Other IM API exists also. See
viewforum.php?f=15
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-23T04:29:19-07:00
by codeswitch
Thank you so far.
But now I have a different idea.
Is it possible to convert a jpg into a png, and telling imagemagick to reduce the png to 10 colors? You know, just like in photoshop.
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-09-23T09:46:54-07:00
by fmw42
try (assuming your input is RGB and not CMYK)
convert image.jpg +dither -colors 10 PNG8:image.png
see
http://www.imagemagick.org/Usage/quantize/
http://www.imagemagick.org/Usage/formats/#png
example:
convert rose: +dither -colors 10 PNG8:rose_c10.png
identify -verbose rose_c10.png
Image: rose_c10.png
Format: PNG (Portable Network Graphics)
Class: PseudoClass
Geometry: 70x46+0+0
Resolution: 72x72
Print size: 0.972222x0.638889
Units: Undefined
Type: Palette
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
red:
min: 51 (0.2)
max: 232 (0.909804)
mean: 141.419 (0.554584)
standard deviation: 66.5689 (0.261054)
kurtosis: -1.57701
skewness: 0.062993
green:
min: 51 (0.2)
max: 211 (0.827451)
mean: 86.2292 (0.338154)
standard deviation: 46.3566 (0.181791)
kurtosis: 1.64809
skewness: 1.6316
blue:
min: 45 (0.176471)
max: 217 (0.85098)
mean: 77.1522 (0.302558)
standard deviation: 48.4711 (0.190083)
kurtosis: 2.95612
skewness: 1.99881
Image statistics:
Overall:
min: 45 (0.176471)
max: 232 (0.909804)
mean: 76.2001 (0.298824)
standard deviation: 69.0855 (0.270924)
kurtosis: -0.138444
skewness: 0.897025
Histogram:
552: ( 51, 51, 45) #33332D rgb(51,51,45)
274: ( 83, 76, 56) #534C38 rgb(83,76,56)
334: ( 94, 93, 79) #5E5D4F rgb(94,93,79)
237: ( 98, 57, 47) #62392F rgb(98,57,47)
208: (110,147, 86) #6E9356 rgb(110,147,86)
176: (140,133,146) #8C8592 rgb(140,133,146)
356: (175, 66, 57) #AF4239 rgb(175,66,57)
263: (213,211,217) #D5D3D9 rgb(213,211,217)
566: (221, 54, 48) #DD3630 rgb(221,54,48)
254: (232, 81, 89) #E85159 rgb(232,81,89)
...
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-10-06T09:10:48-07:00
by codeswitch
Thank you very much, fmw42!
That's exactly what I needed.
So as a brief overview for other PHP-programmers:
To extract the most dominant colours out of an image, I use imagemagick to convert that image into a png-file with exactly the amount of colours I need. Then I take this generated image to pull out the histogram, which tells me the wanted colours. With so old a version as I have (see the first post) you have to use PHP-string functions in order to get the colour values.
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-10-07T21:13:13-07:00
by fmw42
You don't have to create a new PNG image. You can do it as:
convert logo: +dither -colors 10 -format %c histogram:info:
15159: ( 7, 88,162) #0758A2 rgb(7,88,162)
8098: ( 32, 28, 30) #201C1E rgb(32,28,30)
5560: ( 49, 59,135) #313B87 rgb(49,59,135)
2842: ( 68, 69,101) #444565 rgb(68,69,101)
2432: (105,140,135) #698C87 rgb(105,140,135)
2868: (178,164,159) #B2A49F rgb(178,164,159)
3317: (213,210,211) #D5D2D3 rgb(213,210,211)
4068: (223, 56, 52) #DF3834 rgb(223,56,52)
6402: (232,191, 94) #E8BF5E rgb(232,191,94)
256454: (255,255,255) #FFFFFF rgb(255,255,255)
or if you want it sorted
convert logo: +dither -colors 10 -format %c histogram:info:- | sort -r -k 1
256454: (255,255,255) #FFFFFF rgb(255,255,255)
15159: ( 7, 88,162) #0758A2 rgb(7,88,162)
8098: ( 32, 28, 30) #201C1E rgb(32,28,30)
6402: (232,191, 94) #E8BF5E rgb(232,191,94)
5560: ( 49, 59,135) #313B87 rgb(49,59,135)
4068: (223, 56, 52) #DF3834 rgb(223,56,52)
3317: (213,210,211) #D5D2D3 rgb(213,210,211)
2868: (178,164,159) #B2A49F rgb(178,164,159)
2842: ( 68, 69,101) #444565 rgb(68,69,101)
2432: (105,140,135) #698C87 rgb(105,140,135)
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-10-08T00:39:59-07:00
by codeswitch
Thanks, but the sort option doesn't work with my old version (which the hoster doesn't want to update).
Re: Help for old version 6.2.4: how to count colors in a jpg
Posted: 2009-10-08T10:58:12-07:00
by fmw42
the sort is a Unix command and not part of IM. Get them to install some of the unix commands like sort, sed, bc, etc. or find windows equivalent commands