I catalog images by the date each was taken. My camera timestamps its images with the date/time. Perfect!
However, when I later "modify" an image, or when I transfer it over BlueTooth, etc. the "Date Modified" for the file changes.
I use this little script to change the atime/mtime for the image file BACK to where it should be.
All the action is inside Image::ExifTool, nevertheless, I welcome your feedback!
UPDATE: Added error checking.
#!/usr/bin/perl
use warnings;
use strict;
use Time::Local;
use Image::ExifTool qw(:Public);
my $exifTool = new Image::ExifTool;
foreach my $file (@ARGV) {
# Read only the "DateTimeOriginal" tag from the file.
my $info = $exifTool->ImageInfo($file, 'DateTimeOriginal');
# Error Handling
if (defined $exifTool->GetValue('Error')) {
print "ERROR: Skipping '$file': " . $exifTool->GetValue('Error') .
+ "\n";
next;
}
if (not exists $info->{'DateTimeOriginal'}) {
warn "File '$file' has no EXIF 'DateTimeOriginal' tag. Skipping it
+.\n";
next;
}
if (defined $exifTool->GetValue('Warning')) { # Can there be a warn
+ing?
print "Warn: Processing '$file': " . $exifTool->GetValue('Warning'
+) . "\n";
}
# I suppose we could still check to make sure the data is "reasonabl
+e".
# There could be garbage data in the DateTimeOriginal field and we'd
+ be
# in trouble...
# Our data comes in the form "YEAR:MON:DAY HOUR:MIN:SEC".
# utime() wants data in the exact opposite order, so we reverse().
my @date = reverse(split(/[: ]/, $info->{'DateTimeOriginal'}));
# Note that the month numbers must be shifted: Jan = 0, Feb = 1
--$date[4];
# Convert to epoch time format
my $time = timelocal(@date);
# Make the change to the mtime and atime
my $status = utime($time, $time, $file);
if ($status != 1) {
print "Warn: utime() on '$file' returned $status instead of 1.\n";
}
}
__END__
=head1 SYNOPSYS
Reads the "DateTimeOriginal" EXIF field out of an image file
and changes the "last accessed time" and "last modified time"
of the file to match it.
=head1 USAGE
On Unix:
thisfile.pl *.jpg another/dir/*.jpg
On Windows:
perl thisfile.pl bunch.jpg of.jpg files.jpg
Everything you give to this file is expected to be a filename.
It accepts no other switches or arguments.
=cut
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|