Get value after SetNewValue

Started by Archive, May 12, 2010, 08:54:08 AM

Previous topic - Next topic

Archive

[Originally posted by reinicpan on 2007-08-26 13:17:25-07]

Hi!

How can I get the new value of a variable changed with SetNewValue?

What I want to do is, shifting the time of an image an use the new date/time to rename the image.

When I shift the time with
Code:
$exiftool->SetNewValue('AllDates', "$timeshift", Shift => +1);
and get the value afterwards with
Code:
$image_info->{'CreateDate'}
I get the old value.

How can I get the new value.

Code:

Code:
   my $file = "a.jpg";
    my $timeshift = "+00:01:02";
    my $exiftool = new Image::ExifTool;
    my $image_info = $exiftool->ImageInfo($file, {DateFormat => "%Y-%m-%d-%H-%M-%S"});

    # shift time
    if ($timeshift) {
      my ($direction) = ($timeshift =~ /^([+-])/);
      $timeshift =~ s/^[+-]//;
      if ($direction eq "+") {
        $exiftool->SetNewValue('AllDates', "$timeshift", Shift => +1);
      }
      elsif ($direction eq "-") {
        $exiftool->SetNewValue('AllDates', "$timeshift", Shift => -1);
      }
      else {
        $exiftool->SetNewValue('AllDates', "$timeshift");
      }
      print "CreationDate:   $image_info->{'CreationDate'}\n";
      print "CreationDate:   $exiftool->GetValue('CreationDate')\n";
    }

Thanx a lot.

Reini

Archive

[Originally posted by exiftool on 2007-08-27 12:32:45-07]

Hi Reini,

You have to write the file with WriteInfo() first, then read back the information
with ImageInfo() to get the new date/time.  If the files aren't huge, this can be
done efficiently by writing the file to memory:

Code:
my $file = "a.jpg";
    my $timeshift = "+00:01:02";
    my $exiftool = new Image::ExifTool;

    # shift time
    if ($timeshift) {
      my ($direction) = ($timeshift =~ /^([+-])/);
      $timeshift =~ s/^[+-]//;
      if ($direction eq "+") {
        $exiftool->SetNewValue('AllDates', "$timeshift", Shift => +1);
      }
      elsif ($direction eq "-") {
        $exiftool->SetNewValue('AllDates', "$timeshift", Shift => -1);
      }
      else {
        $exiftool->SetNewValue('AllDates', "$timeshift");
      }
      print "CreationDate:   $image_info->{'CreationDate'}\n";
      print "CreationDate:   $exiftool->GetValue('CreationDate')\n";
      $exifTool->WriteInfo($file); # <<<< you forgot this
    }
    my $image_info = $exiftool->ImageInfo($file, {DateFormat => "%Y-%m-%d-%H-%M-%S"});

- Phil

Archive

[Originally posted by reinicpan on 2007-08-29 18:08:54-07]

Hi Phil,

can't it be done without writing the file first?

How can I write the file to memory?

Thanx a lot.

Reini

Archive

[Originally posted by exiftool on 2007-08-30 11:45:21-07]

No.  You can't know if a value is going to be written until the
file has actually been written (there are many reasons why
the value may not be written as expected).  So you must
write the file first.

Sorry.  I meant to show how to write in memory but forgot
when I cut and pasted your example.  Here the image is rewritten
in memory (and never updated on disk!):

Also, I didn't read the details of your script, but CreationDate
is not a valid EXIF tag name.  Maybe you meant CreateDate.
Plus I fixed some problems with the printouts in my example.

Code:
   my $file = "a.jpg";
    my $timeshift = "+00:01:02";
    my $exiftool = new Image::ExifTool;
    my $memory_image;

    # shift time
    if ($timeshift) {
      my ($direction) = ($timeshift =~ /^([+-])/);
      $timeshift =~ s/^[+-]//;
      if ($direction eq "+") {
        $exiftool->SetNewValue('AllDates', "$timeshift", Shift => +1);
      }
      elsif ($direction eq "-") {
        $exiftool->SetNewValue('AllDates', "$timeshift", Shift => -1);
      }
      else {
        $exiftool->SetNewValue('AllDates', "$timeshift");
      }
      $exifTool->WriteInfo($file, \$memory_image);
    }
    my $image_info = $exiftool->ImageInfo(\$memory_image, {DateFormat => "%Y-%m-%d-%H-%M-%S"});
    print "CreationDate:   $image_info->{'CreateDate'}\n";

- Phil

Archive

[Originally posted by reinicpan on 2007-08-30 21:04:29-07]

Hi Phil!

Now it works fine.

Thank you very much for your fast response.

Reini