How to delete all tags except a few

Started by Martin B., September 08, 2024, 01:33:49 PM

Previous topic - Next topic

Martin B.

Hi,

I want to delete all tags from a set of JPGs, except for a few (mainly the basic exposure parameters, such as ISO, FNumber, etc.). I tried the following, but the ISO tag doesn't appear in the resulting file:

    $exifTool->ExtractInfo($src);
    $exifTool->SetNewValue('*');  # delete all...
    my @result = $exifTool->SetNewValue('EXIF:ISO', undef, Replace => 2); # ...but keep this one
    $success = $exifTool->WriteInfo($dst);
I checked the value of @result; first element is 2, second element is empty, which I understand means that 2 tags were written with no error messages.

If I do this (as indicated here https://metacpan.org/dist/Image-ExifTool/view/lib/Image/ExifTool.pod#SetNewValue), then I get the entire EXIF group, including the ISO tag, but that's not exactly what I need.

    $exifTool->ExtractInfo($src);
    $exifTool->SetNewValue('*');  # delete all...
    my @result = $exifTool->SetNewValue('EXIF:*', undef, Replace => 2); # ...but keep this one
    $success = $exifTool->WriteInfo($dst);

The photo does have an ISO tag:
exiftool -G1 -a -s -iso 20001.jpg
[ExifIFD]       ISO                             : 400

Is there a way to delete all tags except for a few?

By the way, some files have no tags, so my code needs not to break in those cases.

Thanks!

Martin

StarGeek

Not my area of expertise, but from the note at the bottom of SetNewValue
QuoteWhen deleting groups of tags, the Replace option may be used to exclude specific groups from a mass delete. However, this technique may not be used to exclude individual tags from a group delete (unless a family 2 group was specified in the delete). Instead, use SetNewValuesFromFile to recover the values of individual tags after deleting a group.

It looks like you have to switch to SetNewValuesFromFile to recover the ISO.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Martin B.

Thanks StarGeek; I hadn't noticed that paragraph.

Using SetNewValuesFromFile did the trick; the following worked nicely to keep only the exposure parameters and the color profile information:
$exifTool->ExtractInfo($src);
my @tags_to_keep = qw(EXIF:ISO
                      EXIF:ExposureTime EXIF:ShutterSpeedValue
                      EXIF:FNumber EXIF:ApertureValue
                      EXIF:ExposureCompensation EXIF:Flash EXIF:MeteringMode
                      EXIF:FocalLength
                      ICC_Profile ColorSpaceTags
                     );
$exifTool->SetNewValue('*');  # delete all...
$exifTool->SetNewValuesFromFile($src, @tags_to_keep); # ...but keep these ones
# Write the changes back to the file
$success = $exifTool->WriteInfo($dst);

-Martin

StarGeek

One thing that has taken me too long to figure out is to always check the documentation first, as it's so extensive. Even Phil occasionally forgets that he's already documented something. The hardest part is figuring out which part to search in.

I keep the PDF versions of some of the docs locally, especially the tag names page, so that it's easier to search through.  Hmmm... It might be worth merging all of them into a single document to make it even easier to search.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype