Trouble writing to PNG

Started by outdoormagic, June 25, 2020, 10:57:04 AM

Previous topic - Next topic

outdoormagic

Hi,

I have a PNG and want to update the keywords and hierarchicalsubject. Getting tags from the command line with exiftool -g1 -a -u, I get:


---- XMP-lr ----
Hierarchical Subject            : 2 - LOCATION|North America|Canada|British Columbia|Whistler|Stonesedge Restaurant, CAN, Canada|British Columbia|Whistler, Events|Honeymoon, Locations|British Columbia, Locations|Canada, Objects|Menus, Places|Stonesedge, Places|Whistler, Places|Whistler Village, Restaurants, documentation, food|Poutine


In PERL, I have this:

                if ($filePath =~ /PNG/i){
                    print "Writing PNG to $filePath\n";
                    print Dumper ($exifTool->GetNewValue('XMP-lr:HierarchicalSubject'));
                }
                $exifTool->WriteInfo($filePath);


which outputs:

Writing PNG to /Volumes/Data Vault/Media/PHOTOS/Capture/North America/Canada/British Columbia/Whistler/Stonesedge Restaurant/Decadent Poutine/CAN_Honeymoon_Stonesedge Restaurant_20150711__Decadent Poutine_5966.PNG
'1 - TIME|event|Unique Personal Events|Anniversary|Honeymoon'
'1 - TIME|event|events'
'2 - LOCATION|country code|CAN'
'2 - LOCATION|geography|North America|Canada|British Columbia|Whistler'
'2 - LOCATION|geography|North America|Canada|British Columbia|Whistler|Whistler Village|restaurant|Stonesedge|Stonesedge Restaurant'
'2 - LOCATION|location'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|Documentation and Ideas|menu'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|World Cuisine|North American food|Canadian food|Poutine'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|Other Common and Everyday Items|object'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|information|Content|documentation'
'5 - HOW - VERBS & ADJECTIVES|characteristic|food'
'6 - PHOTOGRAPHY & TECHNICAL|photo type|food'
'8 - CONCEPT - STORY|concept|food'
'1 - TIME|event|Unique Personal Events|Anniversary|Honeymoon'
'1 - TIME|event|events'
'2 - LOCATION|country code|CAN'
'2 - LOCATION|geography|North America|Canada|British Columbia|Whistler'
'2 - LOCATION|geography|North America|Canada|British Columbia|Whistler|Whistler Village|restaurant|Stonesedge|Stonesedge Restaurant'
'2 - LOCATION|location'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|Documentation and Ideas|menu'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|World Cuisine|North American food|Canadian food|Poutine'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|Other Common and Everyday Items|object'
'4 - WHAT - OBJECTS - PLANTS - ANIMALS|information|Content|documentation'
'5 - HOW - VERBS & ADJECTIVES|characteristic|food'
'6 - PHOTOGRAPHY & TECHNICAL|photo type|food'
'8 - CONCEPT - STORY|concept|food'

Updated 1 files.
Skipped 0 files.



So the new values were loaded, but when I look at the file again, I see they are not written. My code works fine with other file formats, so I'm stuck.

Help!

Thanks.

StarGeek

Quote from: outdoormagic on June 25, 2020, 10:57:04 AM
So the new values were loaded, but when I look at the file again, I see they are not written.

What are you using to look at the file?  If it's something other than exiftool then you need to know that most software doesn't support metadata in PNG files.  For example, Windows won't read any metadata except for PNG:CreationTime.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

outdoormagic

#2
QuoteWhat are you using to look at the file?  If it's something other than exiftool then you need to know that most software doesn't support metadata in PNG files.  For example, Windows won't read any metadata except for PNG:CreationTime.

I look at the file either by typing exiftool -g1 -a -u file name.png, or in PERL.

In PERL, the full script opens the file (sets up an instance of exiftool) and loads the tags with GetValue. I then process the tags and check them with GetNewValue, as described above, before writing changes with WriteInfo.

So after I open the file, I can print the "pre-processing" or original tags for inspection. That's where I see it in PERL. But, generally, is use the command line version outside of debugging PERL scripts.

The reason I'm trying to clean things up is that Lightroom and PhotoMechanic both seem to read nearly all common metadata from PNG (keywords, dates, make, caption, headline, ...).


EDIT:
I have since found this: https://exiftool.org/config.html

If PNG tags are not standard, then that would explain why I can read them, but not write to them: they are not defined in exiftool.

Does anyone know where I can find documentation to define a hierarchicalSubject structure? For other fields such as Keywords or Headline, can I assume it is String[0,255]?


Phil Harvey

I read your post 3 times and still can't glean enough information to know exactly what you are doing.  I need to be able to reproduce the effect you are seeing to be able to help.

- 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 ($).

outdoormagic

Hi Phil,

Based on what StarGeek said and what I can read in config file, I think what I need to do is to make a tag writeable.

Lightroom, for example, will write keywords to a PNG fileā€”not fully standard, but it does. Why I care is that I have is a small PERL script that ensures that keywords are in my controlled vocabulary and then writes the appropriate branch back to HierarchicalSubject (which Lightroom both writes and reads). It helps me keep my keywording tidy.

Exiftool reads the IPTC:Keywords, XMP-dc:Subject, and XMP-lr:HierarchicalSubject tags in a PNG file, but I suspect that as they are not defined for PNG, Image:ExifTool's WriteInfo() will not write updated tags back to the file after I make a change.

I tried to add an .ExifTool_Config file with the following:


%Image::ExifTool::UserDefined = (
    # new PNG tags are added to the PNG::TextualData table:
    'Image::ExifTool::PNG::TextualData' => {
        'XMP-lr:HierarchicalSubject' => { },
        'IPTC:Keywords' => { },
        'XMP-dc:Subject' => { },
    },
);

#------------------------------------------------------------------------------
1;  #end


and while I don't get an error, I clearly don't have the correct syntax.

Does this make sense?

Thanks.

StarGeek

Quote from: outdoormagic on June 25, 2020, 05:20:33 PM
Exiftool reads the IPTC:Keywords, XMP-dc:Subject, and XMP-lr:HierarchicalSubject tags in a PNG file, but I suspect that as they are not defined for PNG

Exiftool will write all three of these to a PNG file without any extra definitions/config file.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

That config file is definitely not the way to do it. 

Can you attach a small sample image where Lightroom has written the keywords so we an take a look?

- 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 ($).

outdoormagic

#7
Quote from: Phil Harvey on June 25, 2020, 06:21:22 PM
Can you attach a small sample image where Lightroom has written the keywords so we an take a look?


Quote from: Phil Harvey on June 25, 2020, 06:21:22 PM
Can you attach a small sample image where Lightroom has written the keywords so we an take a look?

Here you go. I now think it might be a corrupted file issue.

The large file (testfile_original.PNG) is the original. The small one (testfile.PNG) is resized (EDIT: cropped) and made all blue. Same keywords, but with an intermediate save in Photoshop 2020.

The large file doesn't update. The small one does and yields testfile_updated.PNG.

All 3 are attached. I had to zip them because the site wouldn't allow me to upload (security check).


Phil Harvey

The Subject and HierarchicalSubject are written to the standard international-text XMP.

The Keywords are written to the compressed-text IPTC profile.

These are exactly what ExifTool writes for PNG.

Copying the HierarchicalKeywords from the updated file works as expected:

> exiftool ~/Desktop/Archive -hierarchicalsubject -G5
======== /Users/phil/Desktop/Archive/testfile_updated.PNG
[PNG-InternationalText-XMP] Hierarchical Subject: 1 - TIME|event|Unique Personal Events|Anniversary|Honeymoon, 1 - TIME|event|events, 1 - TIME|time of day|night, 2 - LOCATION|country code|CAN, 2 - LOCATION|geography|North America|Canada|British Columbia|Vancouver|restaurant, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler|Whistler Village|restaurant|Stonesedge|Stonesedge Restaurant, 2 - LOCATION|geography|North America|United States|Utah|Moab|restaurant, 2 - LOCATION|location, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|Documentation and Ideas|menu, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|World Cuisine|North American food|Canadian food|Poutine, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Industry and Business|business|industry|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Other Common and Everyday Items|object, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Structures and Buildings|building|hospitality establishment|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Travel and Transit|travel|hotel|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|information|Content|documentation, 5 - HOW - VERBS & ADJECTIVES|characteristic|food, 6 - PHOTOGRAPHY & TECHNICAL|photo type|food, 8 - CONCEPT - STORY|concept|food, 1 - TIME|event|Unique Personal Events|Anniversary|Honeymoon, 1 - TIME|event|events, 1 - TIME|time of day|night, 2 - LOCATION|country code|CAN, 2 - LOCATION|geography|North America|Canada|British Columbia|Vancouver|restaurant, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler|Whistler Village|restaurant|Stonesedge|Stonesedge Restaurant, 2 - LOCATION|geography|North America|United States|Utah|Moab|restaurant, 2 - LOCATION|location, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|Documentation and Ideas|menu, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|World Cuisine|North American food|Canadian food|Poutine, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Industry and Business|business|industry|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Other Common and Everyday Items|object, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Structures and Buildings|building|hospitality establishment|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Travel and Transit|travel|hotel|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|information|Content|documentation, 5 - HOW - VERBS & ADJECTIVES|characteristic|food, 6 - PHOTOGRAPHY & TECHNICAL|photo type|food, 8 - CONCEPT - STORY|concept|food
======== /Users/phil/Desktop/Archive/testfile.PNG
[PNG-InternationalText-XMP] Hierarchical Subject: 1 - TIME|time of day|night, 2 - LOCATION|North America|Canada|British Columbia|Whistler|Stonesedge Restaurant, CAN, Canada|British Columbia|Whistler, Events|Honeymoon, Locations|British Columbia, Locations|Canada, Objects|Menus, Places|Stonesedge, Places|Whistler, Places|Whistler Village, Restaurants, documentation, food|Poutine
======== /Users/phil/Desktop/Archive/testfile_original.PNG
[PNG-InternationalText-XMP] Hierarchical Subject: 1 - TIME|time of day|night, 2 - LOCATION|North America|Canada|British Columbia|Whistler|Stonesedge Restaurant, CAN, Canada|British Columbia|Whistler, Events|Honeymoon, Locations|British Columbia, Locations|Canada, Objects|Menus, Places|Stonesedge, Places|Whistler, Places|Whistler Village, Restaurants, documentation, food|Poutine
    1 directories scanned
    3 image files read
> exiftool a.png -tagsfromfile ~/Desktop/Archive/testfile_updated.PNG -hierarchicalsubject
    1 image files updated
> exiftool a.png -hierarchicalsubject -G5
[PNG-InternationalText-XMP] Hierarchical Subject: 1 - TIME|event|Unique Personal Events|Anniversary|Honeymoon, 1 - TIME|event|events, 1 - TIME|time of day|night, 2 - LOCATION|country code|CAN, 2 - LOCATION|geography|North America|Canada|British Columbia|Vancouver|restaurant, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler|Whistler Village|restaurant|Stonesedge|Stonesedge Restaurant, 2 - LOCATION|geography|North America|United States|Utah|Moab|restaurant, 2 - LOCATION|location, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|Documentation and Ideas|menu, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|World Cuisine|North American food|Canadian food|Poutine, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Industry and Business|business|industry|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Other Common and Everyday Items|object, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Structures and Buildings|building|hospitality establishment|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Travel and Transit|travel|hotel|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|information|Content|documentation, 5 - HOW - VERBS & ADJECTIVES|characteristic|food, 6 - PHOTOGRAPHY & TECHNICAL|photo type|food, 8 - CONCEPT - STORY|concept|food, 1 - TIME|event|Unique Personal Events|Anniversary|Honeymoon, 1 - TIME|event|events, 1 - TIME|time of day|night, 2 - LOCATION|country code|CAN, 2 - LOCATION|geography|North America|Canada|British Columbia|Vancouver|restaurant, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler, 2 - LOCATION|geography|North America|Canada|British Columbia|Whistler|Whistler Village|restaurant|Stonesedge|Stonesedge Restaurant, 2 - LOCATION|geography|North America|United States|Utah|Moab|restaurant, 2 - LOCATION|location, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|Documentation and Ideas|menu, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Food and Drink|World Cuisine|North American food|Canadian food|Poutine, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Industry and Business|business|industry|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Other Common and Everyday Items|object, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Structures and Buildings|building|hospitality establishment|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|Travel and Transit|travel|hotel|restaurant, 4 - WHAT - OBJECTS - PLANTS - ANIMALS|information|Content|documentation, 5 - HOW - VERBS & ADJECTIVES|characteristic|food, 6 - PHOTOGRAPHY & TECHNICAL|photo type|food, 8 - CONCEPT - STORY|concept|food
...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 ($).

outdoormagic

#9
Phil,

Thanks for looking into this. I see how I didn't share the issue correctly, which tells you I'm a newbie. I'm also trying other files as you and StarGeek have questions. Here's the update.

In the way I have my hierarchical keyword tree laid out, all entries start with a number between 1 and 8:

1 - Time
2 - Location

And so on.

If I look at the hierarchical subject after processing and don't see that, then I know my keywords aren't up to date.

Look at HierarchicalSubject on testfile.PNG before processing. This is the same as testfile_original.PNG, because testfile.PNG is the cropped version of testfile_original.PNG.

After processing, I should have that numbered first node described above.

If I process testfile.PNG, then I get testfile_updated.PNG, which has the desired tree.

But if process testfile_original.PNG, then the file doesn't get updated. In fact, after I call WriteInfo, the time stamp doesn't change on the file (I'm on Mac OS).

In retrospect, I should have named my files differently as this is confusing.

Somehow, cropping testfile_original.PNG and saving it did something that made its tags updatable. I'm unfortunately a user, not a programmer, so I am not able to figure that out.

Again, thanks for putting up with these rookie moves.




Phil Harvey

Here is your reason:

> exiftool ~/Desktop/Archive -validate -warning -a
======== /Users/phil/Desktop/Archive/testfile_updated.PNG
Validate                        : OK
======== /Users/phil/Desktop/Archive/testfile.PNG
Validate                        : OK
======== /Users/phil/Desktop/Archive/testfile_original.PNG
Validate                        : 6 Warnings (1 minor)
Warning                         : [minor] Text chunk(s) found after PNG IDAT (may be ignored by some readers)
Warning                         : Bad CRC for iTXt chunk
Warning                         : Bad CRC for iTXt chunk
Warning                         : Bad CRC for iTXt chunk
Warning                         : Bad CRC for iTXt chunk
Warning                         : Bad CRC for iTXt chunk
    1 directories scanned
    3 image files read


ExifTool won't update testfile_original.PNG because it has bad CRC values.  This indicates that the metadata may be corrupted, so ExifTool won't write the file to avoid causing further damage.  You would see this if you checked the Error tag after trying to write the file.

- 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 ($).

outdoormagic

Will do.

Thanks for a great tool. I have a lot to learn.