Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

(Solved) Change "Date Picture Taken" with Image::ExifTool

by Limbic~Region (Chancellor)
on Jul 25, 2008 at 00:49 UTC ( #700009=perlquestion: print w/replies, xml ) Need Help??

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

Before returning home from vacation, I downloaded the pictures my sister had taken on to my laptop. I just realized her camera has the wrong date so the the meta data in all the pictures is wrong. Thanks to Khisanth, I am pretty sure the module I want to use is Image::ExifTool. I am also fairly certain that the tag I need to modify is one of the following: CreateDate ModifyDate DateTimeOriginal

What I can't figure out is how to use the SetNewValue() correctly such that the WriteInfo() doesn't fail. Does anyone have a complete working example? Admittedly, I have only been RTFMing in between commercials but what I have looks something like this (which I know is wrong):

#!/usr/bin/perl use strict; use warnings; use Image::ExifTool qw(:Public); my $jpg = $ARGV[0] or die "Usage: $0 <file>"; my $tool = Image::ExifTool->new(); $tool->ExtractInfo($jpg); $tool->SetNewValue('Make'); # Deleting this tag magically makes it wor +k my $success = $tool->SetNewValue("DateTimeOriginal", '2008:01:02 03:04 +:05'); if ($success) { $success = $tool->WriteInfo($jpg); print "Update failed", $tool->GetValue('Error'), "\n" if ! $succes +s; } __END__ Update failed[minor] Bad MakerNotes directory pointer for tag 0x203

Update: Added error I am getting.

Update 2: It appears to be a corrupt file (though it views fine) because the same code worked correctly on another picture. Does anyone know how to fix that?

Update 3: I noticed $exifTool->SetNewValue('*');  # delete all non-EXIF tags in TFM. I tried it, and what do you know, it fixed the problem. Of course, I didn't want to delete all the tags so then I restored the original. I then looped over all the tags, deleting each one in turn, and then trying the write. It turns out that 'Make' was the key though I still don't know why. The code above has been modified above to reflect the now working code.

Cheers - L~R

Replies are listed 'Best First'.
Re: (Solved) Change "Date Picture Taken" with Image::ExifTool
by b10m (Vicar) on Jul 25, 2008 at 08:16 UTC

    Although you did already seem to solve this, you might have gone for `exiftool` that comes with the module. Especially the Date/Time Shift Feature section of the documentation seems helpful ;)

Re: (Solved) Change "Date Picture Taken" with Image::ExifTool
by polettix (Vicar) on Jul 27, 2008 at 02:48 UTC
    AFAIK, and according to some emails I exchanged with the maintainer, doing the right thing with image metadata is a PITA. There are many formats, each one slightly incompatible with the other... and it's easy to get trapped.

    Moreover, the MakerNotes has additional issues, and it's clearly stated in the docs that it might be read as a whole block instead of single items. Try to see the MakerNotes option (that defaults to 0, i.e. "Do not extract writable subdirectories"). Even worse things can happen if you play with the preview, because this seems not to be guaranteed to be small enough to make all the image formats happy, and tends to be left out when copying metadata from one file to another (e.g. to restore metadata on some photo you have worked on). You have to go through some loops to get the data back safely.

    The bottom line is that Image::ExifTool is a wonderful module, but it has quite a few glitches when it comes to writing metadata, especially MakerNotes ones.

    Last, but not least, take into account that exiftool (the program) generates "fake" metadata as well, taking as input stuff like file size, file date, etc. These tend to be read-only, so don't lose too much time trying to modify them.

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Io ho capito... ma tu che hai detto?
      I am not sure I quite understand the point of your post. It seems like what you are saying is that doing this is hard but the module does the best job it can. I agree completely but if I am missing something else, please point it out.

      In this case, it turns out that the Kodak Z812 is writing invalid entries that appear to be place holders. Phil Harvey, the module author, is looking into this further but has suggested that a better solution than deleting the 'Make' tag is to use the 'IgnoreMinorErrors' option. This worked and no tags needed to be deleted.

      Cheers - L~R

        Sorry for not making myself clear. It was lucky that you understood correctly, anyway: I like the module very much, and I think that it tries its best to get the job done. Due to the quirks of each different format, anyway, the "best job" isn't always what you would like it to do :)

        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Io ho capito... ma tu che hai detto?
Re: (Solved) Change "Date Picture Taken" with Image::ExifTool
by hbm (Hermit) on Aug 31, 2010 at 14:32 UTC

    I needed to coordinate photos from three cameras with three different clock settings - one was correct, one an hour ahead, and one 2 hours ahead. I used similar code, but many pictures failed to update with a "Maker offsets" warning. The FixBase => '' option fixed that, and below is the code:

    use strict; use warnings; use Image::ExifTool; chdir("C:/Users/hbm/pictures/trip") or die("Unable to chdir: $!"); my $tool = Image::ExifTool->new(); $tool -> Options(FixBase => ''); # "best guess" for fixing Make +r offsets for my $jpg (glob("*.JPG")) { $tool -> ExtractInfo($jpg); my $camera = $tool->GetValue("Model"); next if $camera =~ /KODAK/i; my $shift = ($camera =~ /SD1200/i) ? "2:00" : "1:00"; $tool -> SetNewValue(DateTimeOriginal => $shift, Shift => -1); my $success = $tool->WriteInfo($jpg); print "$jpg Update failed: ", $tool->GetValue('Error'), "\n" if +!$success; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700009]
Approved by GrandFather
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2023-09-22 22:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found