Apparent failure to write

Started by Tom Cunningham, May 22, 2025, 06:20:03 PM

Previous topic - Next topic

Tom Cunningham

This has me completely flummoxed. It harkens back to this thread but with a twist. I have this code:

# clear all current fields in region
for my $i (0 .. $#oldRegionName)
{
    ($success, $errStr) = $exifTool->SetNewValue('XMP-mwg-rs:RegionList' => {
        'Name' => $oldRegionName[$i],
        },
        DelValue => 1) || errExit($file, __LINE__, $errStr);
}
# now repopulate region fields
for my $i (0 .. $#regionName)
{
    ($success, $errStr) = $exifTool->SetNewValue('XMP-mwg-rs:RegionList' => {
                                                    'Name' => $regionName[$i],
                                                        'Type' => $regionType[$i],
                                                        'Area' => {
                                                            'H'    => $regionAreaH[$i],
                                                            'Unit' => "normalized",
                                                            'W'    => $regionAreaW[$i],
                                                            'X'    => $regionAreaX[$i],
                                                            'Y'    => $regionAreaY[$i]
                                                        }
                                                    },
                                                    AddValue => 1) ||
                                    errExit($file, __LINE__, $errStr);
    }
}

In previous code I've removed one element from the region arrays (a duplicate face tag). So with the first for loop I (presumably) clear all the existing fields in the region arrays (4 elements). Then in the second for loop I repopulate the region arrays, this time with only 3 elements. After some subsequent operations not involving the regions, I perform an $exifTool->WriteInfo($fn), where $fn is the filename. After that I check the file with exiftool and there are still 4 elements in the region arrays. It's as if the write didn't happen. What the heck am I missing here? Thanks.


Phil Harvey

Aside from a problem with the balancing of braces in your script, I don't see anything wrong.  I had to modify things somewhat to be able to run this, but this seems to work as expected:

Here is the script I used (called "ttt"):

#!/usr/bin/perl
use strict;
use warnings;

use Image::ExifTool;

my $exifTool = Image::ExifTool->new;

my @oldRegionName = qw(test1 test2);
my @regionName = ('new');

# clear all current fields in region
for my $i (0 .. $#oldRegionName)
{
    my ($success, $errStr) = $exifTool->SetNewValue('XMP-mwg-rs:RegionList' => {
        'Name' => $oldRegionName[$i],
        },
        DelValue => 1);
}
# now repopulate region fields
for my $i (0 .. $#regionName)
{
    my ($success, $errStr) = $exifTool->SetNewValue('XMP-mwg-rs:RegionList' => {
                                                    'Name' => $regionName[$i],
                                                        'Type' => 'Face',
                                                        'Area' => {
                                                            'H'    => 1,
                                                            'Unit' => "normalized",
                                                            'W'    => 2,
                                                            'X'    => 3,
                                                            'Y'    => 4,
                                                        }
                                                    },
                                                    AddValue => 1);
}
$exifTool->Options(Verbose => 3);
$exifTool->WriteInfo('a.xmp');

And here is the result:

> rm a.xmp
> exiftool a.xmp -regionlist='{name=test1,type=pet}' -regionlist='{name=test2,type=pet}' -regionlist="{name=test3,type=pet}"
    1 image files created
> exiftool a.xmp -struct -xmp:all
XMP Toolkit                     : Image::ExifTool 13.30
Region Info                     : {RegionList=[{Name=test1,Type=Pet},{Name=test2,Type=Pet},{Name=test3,Type=Pet}]}
> ./ttt
Rewriting a.xmp...
  FileType = XMP
  FileTypeExtension = XMP
  MIMEType = application/rdf+xml
    - XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Name = 'test1'
    - XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Type = 'Pet'
    - XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 11/mwg-rs:Name = 'test2'
    - XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 11/mwg-rs:Type = 'Pet'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Area/stArea:h = '1'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Area/stArea:unit = 'normalized'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Area/stArea:w = '2'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Area/stArea:x = '3'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Area/stArea:y = '4'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Name = 'new'
    + XMP-mwg-rs:Regions/mwg-rs:RegionList/rdf:Bag/rdf:li 10/mwg-rs:Type = 'Face'
> exiftool a.xmp -struct -xmp:all
XMP Toolkit                     : Image::ExifTool 13.30
Region Info                     : {RegionList=[{Area={H=1,Unit=normalized,W=2,X=3,Y=4},Name=new,Type=Face},{Name=test3,Type=Pet}]}

This is the result I expected (the "test1" and "test2" regions in the list were replaced by the new region.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Tom Cunningham

Quote from: Phil Harvey on May 22, 2025, 09:44:23 PMThis is the result I expected (the "test1" and "test2" regions in the list were replaced by the new region.
- Phil

Thanks for looking into this, Phil. I was breaking up some rather large subroutines and managed to insert a rogue $exifTool->SetNewValue() into one of them. That's why it looked like the write wasn't happening. :P Now it's smooth sailing.  ;D