I\'m new to ImageMagick.
I want to know image\'s RGB color of whole pixel.
I already know how to get one pixel\'s RGB color.
But I don\'t know how to
A Further Option
If you want to amortize the cost of running identify
across lots of images for better performance, you can do something like this - where %k
gives you the number of colours and %n
gives you the filename:
identify -format "%k:%f\n" *.jpg
Output
290972:7T6Dj.jpg
3641:a.jpg
8349:b.jpg
3019:back.jpg
3122:background.jpg
83155:blion.jpg
35136:brDaP.jpg
37106:cartesian.jpg
There must be a system()
or shell_exec()
or popen()
in Java that could run that so you could get the output.
Updated Answer
If you simply want to check whether the image consists of only a single colour, you can ask ImageMagick to count the colours, like this (using the same image as below):
identify -format "%k" a.gif
3
I am not sure how you do that with Java, but in Ruby you do:
image["%[k]"]
and in Perl you do:
my $image = Image::Magick->new;
$image->ReadImage("c.png");
print $image->Get("%[k]");
Original Answer
You provided no details of your environment, programming language, application or anything much, however, this may get you started.
Let's create a small image from the command line, with 3 squares, each 4x4 pixels, one red, one green and one blue all in a horizontal row:
convert -size 4x4 xc:red xc:green xc:blue +append a.gif
I'll zoom it in so you can see it:
Now, we can look at it in text format:
convert -size 4x4 xc:red xc:green xc:blue +append -depth 8 txt:
# ImageMagick pixel enumeration: 12,4,255,srgb
0,0: (255,0,0) #FF0000 red
1,0: (255,0,0) #FF0000 red
2,0: (255,0,0) #FF0000 red
3,0: (255,0,0) #FF0000 red
4,0: (0,128,0) #008000 green
5,0: (0,128,0) #008000 green
6,0: (0,128,0) #008000 green
7,0: (0,128,0) #008000 green
8,0: (0,0,255) #0000FF blue
9,0: (0,0,255) #0000FF blue
10,0: (0,0,255) #0000FF blue
11,0: (0,0,255) #0000FF blue
0,1: (255,0,0) #FF0000 red
1,1: (255,0,0) #FF0000 red
2,1: (255,0,0) #FF0000 red
3,1: (255,0,0) #FF0000 red
4,1: (0,128,0) #008000 green
5,1: (0,128,0) #008000 green
6,1: (0,128,0) #008000 green
7,1: (0,128,0) #008000 green
8,1: (0,0,255) #0000FF blue
9,1: (0,0,255) #0000FF blue
10,1: (0,0,255) #0000FF blue
11,1: (0,0,255) #0000FF blue
0,2: (255,0,0) #FF0000 red
1,2: (255,0,0) #FF0000 red
2,2: (255,0,0) #FF0000 red
3,2: (255,0,0) #FF0000 red
4,2: (0,128,0) #008000 green
5,2: (0,128,0) #008000 green
6,2: (0,128,0) #008000 green
7,2: (0,128,0) #008000 green
8,2: (0,0,255) #0000FF blue
9,2: (0,0,255) #0000FF blue
10,2: (0,0,255) #0000FF blue
11,2: (0,0,255) #0000FF blue
0,3: (255,0,0) #FF0000 red
1,3: (255,0,0) #FF0000 red
2,3: (255,0,0) #FF0000 red
3,3: (255,0,0) #FF0000 red
4,3: (0,128,0) #008000 green
5,3: (0,128,0) #008000 green
6,3: (0,128,0) #008000 green
7,3: (0,128,0) #008000 green
8,3: (0,0,255) #0000FF blue
9,3: (0,0,255) #0000FF blue
10,3: (0,0,255) #0000FF blue
11,3: (0,0,255) #0000FF blue
But you say that it takes too long to get one 1 pixel, so you can convert the image to a file that is just pure RGB values and read that:
convert -size 4x4 xc:red xc:green xc:blue +append -depth 8 x.rgb
If we look at the file, we can see it is 144 pixels long, 16 red pixels, 16 green pixels, 16 blue pixels - therefore 48 pixels altogether - and each one with a single byte of R, G and B. (48x3=144)
ls -l x.rgb
-rw-r--r-- 1 mark staff 144 29 Oct 11:06 x.rgb
ImageMagick uses the file extension to determine the format, and rgb
means RGB
!. If you want to use an extension different from .rgb
, you can tell ImageMagick like this:
convert -size 4x4 xc:red xc:green xc:blue +append -depth 8 RGB:x.raw
Now let's look at the file in hex:
xxd -g3 -c12 x.rgb
0000000: ff0000 ff0000 ff0000 ff0000 ............
000000c: 008000 008000 008000 008000 ............
0000018: 0000ff 0000ff 0000ff 0000ff ............
0000024: ff0000 ff0000 ff0000 ff0000 ............
0000030: 008000 008000 008000 008000 ............
000003c: 0000ff 0000ff 0000ff 0000ff ............
0000048: ff0000 ff0000 ff0000 ff0000 ............
0000054: 008000 008000 008000 008000 ............
0000060: 0000ff 0000ff 0000ff 0000ff ............
000006c: ff0000 ff0000 ff0000 ff0000 ............
0000078: 008000 008000 008000 008000 ............
0000084: 0000ff 0000ff 0000ff 0000ff ............
Hopefully you can see the first line is 4 red pixels, the second line is 4 green ones...
So, in short, if you want to just read pure binary data from an image into a C program, you can do this:
convert YourImage.jpg -depth 8 RGB:- | YourProgram