Import image with Perl 用Perl导入图像

huangapple go评论95阅读模式
英文:

Import image with Perl

问题

我知道这听起来像是一个已经被提出许多次的问题,但我找不到确切我需要的东西。

我想要的是逐像素提取图像数据。
导入类型应该是 .jpg(或者如果有一个不适用于 jpeg 的函数,可以是另一种格式)。
导出类型应该是一个包含像素值的整数数组或列表。

我正在寻找的函数类似于 Matlab 中的 "imread"。
我尝试过 Perl 中的 "open" 函数,但我不知道提取的图像是什么类型。

英文:

I know it sounds like a question that would have been already posted many times, but I can't find exactly what I need.

What I want is to extract the data of the image pixel by pixel.
The import type would be .jpg (or another format if there is a function that doesn't work on jpeg).
The export type would be an array or a list of integers with the values of the pixels.

The function I am searching for is similar to "imread" for those who know Matlab.
I have tried the function "open" in Perl but then I don't know what type is the extracted image.

Thanks.

答案1

得分: 2

你可以使用Image::Magick来执行这个操作,就像clamp建议的那样,但我更喜欢更现代的Imager包。

use strict;
use warnings;
use Imager;

my $img = Imager->new( file => 'scratch/so.jpg' ) or die Imager->errstr;
print "X\tY\tR\tG\tB\n";
foreach my $x ( 0 .. $img->getwidth - 1 ) {
    foreach my $y ( 0 .. $img->getheight - 1 ) {
        my $color = $img->getpixel( x => $x, y => $y );
        my @rgb = $color->rgba;
        printf("%d\t%d\t%d\t%d\t%d\n", $x, $y, @rgb[0,1,2]);
    }
}

这将输出:

X	Y	R	G	B
0	0	31	0	30
0	1	107	54	106
0	2	240	95	214
1	0	191	138	190
1	1	82	29	81
1	2	177	32	151
2	0	179	255	197
2	1	77	203	95
2	2	255	232	126

我使用了这张图片:Import image with Perl
用Perl导入图像

虽然代码看起来有点复杂,但主要是为了生成这个输出。思路很简单。你遍历每一行,然后遍历每一列,并获取像素的值。在这种情况下,RGBA代表红色、绿色、蓝色和alpha通道,但JPEG没有alpha通道(通常用于透明度,如PNG或GIF)。

你需要安装Imager和Imager::File::JPEG,这需要libjpeg(例如,在Ubuntu上是libjpeg-dev)。

英文:

You can use Image::Magick to do this like clamp suggests, but I like the more modern Imager package more.

use strict;
use warnings;
use Imager;

my $img = Imager->new( file => 'scratch/so.jpg' ) or die Imager->errstr;
print "X\tY\tR\tG\tB\n";
foreach my $x ( 0 .. $img->getwidth - 1 ) {
    foreach my $y ( 0 .. $img->getheight - 1 ) {
        my $color = $img->getpixel( x => $x, y => $y );
        my @rgb = $color->rgba;
        printf("%d\t%d\t%d\t%d\t%d\n", $x, $y, @rgb[0,1,2]);
    }
}

This will output

X	Y	R	G	B
0	0	31	0	30
0	1	107	54	106
0	2	240	95	214
1	0	191	138	190
1	1	82	29	81
1	2	177	32	151
2	0	179	255	197
2	1	77	203	95
2	2	255	232	126

I've used this image: Import image with Perl
用Perl导入图像

While the code looks a bit complicated, that's mostly to produce this output. The idea is simple. You iterate over each row, then over each column, and get the values for the pixels. RGBA in this case stands for red, green blue and the alpha channel, but a JPEG doesn't have an alpha channel (which is typically used for transparency, like in PNG or GIFs).

You will have to install Imager and Imager::File::JPEG, which requires libjpeg (libjpeg-dev on Ubuntu, for example).

答案2

得分: 0

我会使用GraphickMagic而不是ImageMagick,因为与GM相比,它慢得要命。

英文:

I would use GraphickMagic rather than ImageMagick as it is slow as hell compared to GM.

huangapple
  • 本文由 发表于 2020年1月3日 23:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定