Jouve has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have written a Perl script that need to be executed on Mac OS X (version: 10.6.8) to read the image resolution using the below module.

Image::ExifTool qw(ImageInfo)

But I am not able to read the image Meta information. Kindly advise how to read the resolution of the images using Perl in Mac. Below is the code that I have used:

foreach my $eachImage (@Images) { my ($xrez,$yrez); $xrez=0; next if($eachImage !~ /\.(PNG|JPG|JPEG|TIFF|TIF|EPS|PSD)$/i); $info = ImageInfo("$Directory_Path\\$eachImage"); foreach (keys %$info) { my $val = $$info{$_}; my $imgres=$_."\t".$val."\n"; if ($imgres=~m/XResolution\t([0-9]+)/gi) { $xrez=$1; } } $xrez=sprintf("%.1f",$xrez); $sno++; if ($xrez < 300 or $xrez > 1200) { $result="Fail"; } else { $result="Pass"; } push @Report, $sno.'---'.$eachImage.'---'.$xrez.'---'.$result; undef $image; }

Note: The above script is working fine in Windows XP.

Please help to resolve this problem.

Thanks in Advance,

Sankar

Replies are listed 'Best First'.
Re: How to read Image Resolution on Mac OS X
by MidLifeXis (Monsignor) on Oct 08, 2013 at 15:50 UTC

    Is ImageInfo returning any error messages or state? I am specifically wondering what the path in the line $info = ImageInfo("$Directory_Path\\$eachImage"); does.

    Also, how have you tried to debug this? Have you printed the value of $eachImage within the loop? Do you make it past the next? Do you receive any output, errors, or other information -- But I am not able to read the image Meta information. is very similar to "it doesn't work", which is almost useless from a debugging or assistance point of view.

    Update: this is an example of what an error looks like from ImageInfo:

    perl -MImage::ExifTool=ImageInfo -MData::Dumper -e "$x=ImageInfo('frob +nitz.jpg'); warn Dumper($x)" $VAR1 = { 'Error' => 'Error opening file', 'Directory' => '.', 'FileName' => 'frobnitz.jpg', 'ExifToolVersion' => '9.27' };
    You need to learn to break stuff like this down piece by piece, and think about what is or may be happening.

    --MidLifeXis

      Hi MidLifeXis,

      Your guidance is very helpful to me, Thank you so much for your advice

Re: How to read Image Resolution on Mac OS X
by frozenwithjoy (Priest) on Oct 08, 2013 at 15:51 UTC
    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:

      Hi frozenwithjoy, Thanks for your alternative solution. Thanks Sankar B
Re: How to read Image Resolution on Mac OS X
by Laurent_R (Canon) on Oct 08, 2013 at 17:04 UTC

    I have never used Perl on a Mac, but maybe you should try to change the relevant line to:

    $info = ImageInfo("$Directory_Path/$eachImage");