Let
Image::ExifTool do the work. No need using a regex to extract the resolution. I've written a script to extract metadata from images (on OS X). It is located in one of
my GitHub repos along with other related scripts. By default, it only outputs the subset of exif data that I was interested in, but you can make the appropriate changes to extract what you'd like (or use the
--all option to output all exif data).
The pertinent code + output:
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Data::Printer;
use Image::ExifTool qw(:Public);
my $image = $ARGV[0];
my $exif = ImageInfo( $image, 'XResolution', 'YResolution' );
p $exif;
my $xrez = $$exif{'XResolution'};
my $yrez = $$exif{'YResolution'};
say "Resolution is $xrez x $yrez.";
__END__
\ {
XResolution 180,
YResolution 180
}
Resolution is 180 x 180.
The full script:
#!/usr/bin/env perl
# exif_extractor.pl
use strict;
use warnings;
use Data::Printer;
use Image::ExifTool qw(:Public);
use Getopt::Long;
my ( $image, $all, $help );
my $options = GetOptions(
"image=s" => \$image,
"all" => \$all,
"help" => \$help,
);
my $usage = <<USAGE_END;
USAGE:
$0
--image Image name
--all Extract all Metadata
by default, extracts:
* CreateDate
* Directory
* FileName
* FileNumber
* MeasuredEV
* OwnerName
--help
USAGE_END
die "**NO IMAGE SPECIFIED**\n" . $usage unless $image;
die $usage if $help;
if ($all) {
p ImageInfo($image);
}
else {
p ImageInfo(
$image, 'CreateDate', 'Directory', 'FileName',
'FileNumber', 'MeasuredEV', 'OwnerName'
);
}
exit;