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

In reply to Change Image File Timestamp to Match EXIF Data by shoness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.