简体   繁体   English

如何使用PerlMagick提取EXIF数据?

[英]How can I extract EXIF data using PerlMagick?

I'm currently using Perl Magick http://www.imagemagick.org/script/perl-magick.php , the perl interface to Image Magick http://www.imagemagick.org , to process & convert photos that our site users upload. 我目前使用Perl Magick http://www.imagemagick.org/script/perl-magick.php ,perl的界面难懂的http://www.imagemagick.org ,处理和转换的照片,我们的网站用户上传。 I'd like to be able to also capture some of the EXIF data attached to these images and I have been able to figure out how to do this using the command line interface to Image Magick with the following command: 我希望能够捕获附加到这些图像的一些EXIF数据,并且我已经能够使用以下命令使用Image Magick的命令行界面来弄清楚如何执行此操作:

/usr/bin/identify -format "%[EXIF:*]" image.jpg

Which returns the following EXIF information for a particular photo: 返回特定照片的以下EXIF信息:

exif:ApertureValue=29/8
exif:ColorSpace=1
exif:CompressedBitsPerPixel=3/1
exif:CustomRendered=0
exif:DateTime=2002:10:08 19:49:52
exif:DateTimeDigitized=2002:09:29 14:03:55
exif:DateTimeOriginal=2002:09:29 14:03:55
exif:DigitalZoomRatio=1/1
exif:ExifImageLength=307
exif:ExifImageWidth=410
exif:ExifOffset=192
exif:ExifVersion=48, 50, 50, 48
exif:ExposureBiasValue=0/1
exif:ExposureMode=0
exif:ExposureTime=1/1000
exif:Flash=24
exif:FlashPixVersion=48, 49, 48, 48
exif:FNumber=7/2
exif:FocalLength=227/32
exif:FocalPlaneResolutionUnit=2
exif:FocalPlaneXResolution=235741/32
exif:FocalPlaneYResolution=286622/39
exif:Make=Canon
exif:MaxApertureValue=12742/4289
exif:MeteringMode=5
exif:Model=Canon PowerShot S30
exif:ResolutionUnit=2
exif:SceneCaptureType=0
exif:SensingMethod=2
exif:ShutterSpeedValue=319/32
exif:Software=Adobe Photoshop 7.0
exif:WhiteBalance=0
exif:XResolution=180/1
exif:YResolution=180/1

I've tried about 100 ways to get this same result from Perl Magick but can't figure out how pass the same parameters I'm using on the command line to make it work properly. 我已经尝试了大约100种方法来从Perl Magick获得相同的结果但是无法弄清楚如何通过我在命令行上使用的相同参数来使其正常工作。 Here are a couple of variations I've tried none of which seems to have worked: 以下是我尝试过的几种变体,其中没有一种似乎有效:

use Image::Magick;
my $image = Image::Magick->new;
my $exif = $image->Identify('image.jpg');
print $exif;

$image->Read('image.jpg');
$exif = $image->Get('format "%[EXIF:*]"');
print $exif;

I know there are other ways to extract EXIF data from an image file in perl but since we already have the Perl Magick module loaded I don't want to waste any more memory by having to load an additional module. 我知道还有其他方法可以从perl中的图像文件中提取EXIF数据,但由于我们已经加载了Perl Magick模块,所以我不想因为必须加载额外的模块而浪费更多的内存。 I'm hoping someone out there already has this working on their site and can share the solution. 我希望有人在他们的网站上已经有这个工作,并可以分享解决方案。 Thanks in advance for your help! 在此先感谢您的帮助!

> cat im.pl
use Image::Magick;
my $image = Image::Magick->new();
$image->Read('/home/rjp/2009-02-18/DSC00343.JPG');
my $a = $image->Get('format', '%[EXIF:*]'); # two arguments
my @exif = split(/[\r\n]/, $a);
print join("\n", @exif);
> perl im.pl
exif:ColorSpace=1
exif:ComponentsConfiguration=...
exif:Compression=6
exif:CustomRendered=0
exif:DateTime=2009:02:13 16:18:15
exif:DateTimeDigitized=2009:02:13 16:18:15
...

That seems to work. 这似乎有效。

Version: ImageMagick 6.3.7 06/04/09 Q16 http://www.imagemagick.org 版本:ImageMagick 6.3.7 06/04/09 Q16 http://www.imagemagick.org

I strongly recommend you to use Phil Harvey's ExifTool . 我强烈建议你使用Phil Harvey的ExifTool It's comprehensive and well documented. 它是全面的,有据可查的。 Also, it doesn't read the entire image into memory, and according to the documentation you can get the Exif information from the image just by passing it a file handle to an open image file. 此外,它不会将整个图像读入内存,根据文档,您只需将文件句柄传递给打开的图像文件即可从图像中获取Exif信息。 So it shouldn't waste a lot of memory. 所以它不应该浪费大量的内存。

Edit: @rjp showed how to access all of the information rather than individual tags. 编辑: @rjp显示了如何访问所有信息而不是单个标签。 Here is how to put the data in a hash: 以下是如何将数据放入哈希:

#!/usr/bin/perl

use strict;
use warnings;

use Image::Magick;

my $image = Image::Magick->new;
$image->read('test.jpg');

my %exif = map { s/\s+\z//; $_ }
           map { split /=/, $_  }
           split /exif:/, $image->Get('format', '%[EXIF:*]');

use Data::Dumper;
print Dumper \%exif;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM