Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Magick / Detect colors contained on a image

What would be the simple process that would gives an array of the colors contained in an image ?

like image 315
Ben Avatar asked Apr 19 '13 12:04

Ben


People also ask

How does the Magick identify command work?

The magick identify command recognizes these options. Click on an option to get more details about how that option works. analyze image features (e.g. contract, correlations, etc.). display image moments and perceptual hash. by default, efficiently determine certain image characteristics.

What does Magick identify output look like?

By default, magick identify provides the following output: Filename [frame #] image-format widthxheight page-widthxpage-height+x-offset+y-offset colorspace user-time elapsed-time Next, we look at the same image in greater detail:

What is the Magick identify program?

The magick identify program describes the format and characteristics of one or more image files. It also reports if an image is incomplete or corrupt.

How to set image format in IMagick from command line?

setFormat doesn't replicate the command line option -format - the one in Imagick tries to set the image format, which should be png, jpg etc. The one in the command line is setting the format for info - the closest match for which in Imagick is calling $imagick->identifyImage (true) and parsing the results of that.


1 Answers

ImageMagick's "convert" command can generate a histogram.

$ convert image.png -define histogram:unique-colors=true -format %c histogram:info:-

19557: (  0,  0,  0) #000000 gray(0,0,0)
 1727: (  1,  1,  1) #010101 gray(1,1,1)
 2868: (  2,  2,  2) #020202 gray(2,2,2)
 2066: (  3,  3,  3) #030303 gray(3,3,3)
 1525: (  4,  4,  4) #040404 gray(4,4,4)
   .
   .
   .

Depending on your language of choice and how you want the colors represented, you could go lots of directions from here. Here's a quick Ruby example though:

out = `convert /tmp/lbp_desert1.png \
               -define histogram:unique-colors=true \
               -format %c histogram:info:- \
       | sed -e 's/.*: (//' \
       | sed -e 's/).*//'`

out.split("\n")
    .map{ |row| row.split(",").map(&:to_i) }


# => [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] .....
like image 126
Jim Lindstrom Avatar answered Sep 22 '22 00:09

Jim Lindstrom