如何使用ImageMagick获取图像全像素的RGB颜色? [英] How to get image whole pixel's RGB color with ImageMagick?

查看:988
本文介绍了如何使用ImageMagick获取图像全像素的RGB颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ImageMagick的新手。

I'm new to ImageMagick.

我想知道整个像素的图像RGB颜色。

I want to know image's RGB color of whole pixel.

我已经知道如何获得一个像素的RGB颜色。

I already know how to get one pixel's RGB color.

但我不知道如何用一个命令获取所有像素的数据。

But I don't know how to get all pixel's data with one command.

对每个像素执行每个命令都很重,无法使用它。

Executing each command on every pixel is so heavy that can't use it.

最好的命令结果是这样的......

The best command result is like this...

#x,y={R,G,B}
1,1={100,230,105},
1,2={100,230,105},
1,3={100,230,135},
...
...


推荐答案

更多选项

如果你想在很多图像上分摊运行识别的成本以获得更好的性能,你可以做这样的事情 - 其中%k 为您提供颜色数量,%n 为您提供文件名:

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

<强> 0输出

290972:7T6Dj.jpg
3641:a.jpg
8349:b.jpg
3019:back.jpg
3122:background.jpg
83155:blion.jpg
35136:brDaP.jpg
37106:cartesian.jpg

必须有 system() shell_exec() popen()在Java中可以运行,以便获得输出。

There must be a system() or shell_exec() or popen() in Java that could run that so you could get the output.

更新后的答案

如果您只是想检查图片是否只包含一种颜色,您可以请ImageMagick计算颜色,如下所示(使用与下面相同的图像):

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

我不知道你是怎么用Java做的,但在Ruby中你这样做:

I am not sure how you do that with Java, but in Ruby you do:

image["%[k]"]

在Perl中你可以:

my $image = Image::Magick->new;
$image->ReadImage("c.png");

print $image->Get("%[k]");

原始答案

您没有提供有关环境,编程语言,应用程序或其他任何内容的详细信息,但是,这可能会帮助您入门。

You provided no details of your environment, programming language, application or anything much, however, this may get you started.

让我们从命令创建一个小图像线,有3个正方形,每个4x4像素,一个红色,一个绿色和一个蓝色都在水平行中:

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

但是你说要获得1个像素需要太长时间,所以你可以将图像转换为纯RGB值的文件并读取那:

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

如果我们查看文件,我们可以看到它是144像素长,16个红色像素,16个绿色像素, 16个蓝色像素 - 因此总共48个像素 - 并且每个像素具有单个字节R,G an d B.(48x3 = 144)

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使用文件扩展名来确定格式, rgb 表示 RGB !。如果你想使用不同于 .rgb 的扩展名,你可以像这样告诉ImageMagick:

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  ............

希望你能看到第一行是4个红色像素,第二行是4个绿色像素......

Hopefully you can see the first line is 4 red pixels, the second line is 4 green ones...

因此,简而言之,如果您只想将图像中的纯二进制数据读入C程序,您可以这样做:

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

这篇关于如何使用ImageMagick获取图像全像素的RGB颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆