English 中文(简体)
如何在使用Perl的平面阵列中读一个图像?
原标题:How to read an image in as a byte array using Perl?

我经验很少。

我需要读到双亲形象,然后通过<代码>。 图像:ExifTool模块。

我的守则是:

use Image::ExifTool;

my $exifTool = new Image::ExifTool; 

open(IMAGE, $file) || die "Can t Open $file
";
binmode(IMAGE);

my ($buf, $data, $n);
while (($n = read FILE, $data, 4) != 0) {
  $buf .= $data;
}

# .=  is concat
print $file .= " test";

$infob = $exifTool->ImageInfo($buf);

foreach ( keys %$infob ) {
    print "$_ => $$infob{$_}
";
}

close(IMAGE);

就我所知,我的上述代码在参考文档和逐级附录中读到<代码>$buf的双亲数据。

根据ExifTool文件,你可以把档案作为“图像信息”方法的缩略语——如上。

图像:ExifTool模块在安装时发现:

Error => Unknown file type
最佳回答
use Image::ExifTool;

my $exifTool = new Image::ExifTool; 

open( my $IMAGE, $filename ) || die "Can t Open $filename
";
binmode($IMAGE);

$infob = $exifTool->ImageInfo($IMAGE);

foreach ( keys %$infob ) {
    print "$_ => $$infob{$_}
";
}

close($IMAGE);
问题回答

开放使用file, 处理, 而不是标记。 http://www.ohchr.org。

This will cause you trouble later should you try to do a read. I.e., you want

$n = read IMAGE, $data, 4;

to actually get data from the file. Expanding on the previous code:

use Image::ExifTool;
my $exifTool = new Image::ExifTool;
$filename = $ARGV[0];
open( my $IMAGE, $filename ) || die "Can t Open $filename
";
binmode($IMAGE);

$infob = $exifTool->ImageInfo($IMAGE);
foreach ( keys %$infob ) {
    print "$_ => $$infob{$_}
";
}

$n = read $IMAGE, $data, 2;
printf ("read %d bytes: [%s]
", $n, $data);

$ret = close($IMAGE);
print "close returned $ret
";

为此:

GreenMask => 0x0000ff00
BMPVersion => Windows V4
NumColors => 2
PixelsPerMeterX => 3938
RedMask => 0x00ff0000
Planes => 1
FileType => BMP
<snip>

read 2 bytes: [^@^@]
close returned 1

请注意,该读本没有适当发挥作用,因此已经将第一份标书的两根标本退回了建立信任措施。 理由是,公开呼吁没有花cal,而是处理:

use Image::ExifTool;

my $exifTool = new Image::ExifTool;

$filename = $ARGV[0];

open(  IMAGE, $filename ) || die "Can t Open $filename
";
binmode(IMAGE);

$infob = $exifTool->ImageInfo($filename);

foreach ( keys %$infob ) {
    print "$_ => $$infob{$_}
";
}

$n = read IMAGE, $data, 2;
printf ("read %d bytes: [%s]
", $n, $data);

$ret = close(IMAGE);
print "close returned $ret
";

因此:

Megapixels => 0.994
Directory => .
ImageWidth => 850
ImageSize => 850x1169
BitDepth => 1
<snip> 
FilePermissions => rw-r--r--
Compression => None
NumColors => 2
FileName => staves.bmp
BlueMask => 0x000000ff
read 2 bytes: [BM]
close returned 1

“ExifTool”这一读物是正常运转的。

(Hoping the formatting is okay. I m new to this....)





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签