State Abbreviations User Defined tag

Started by StarGeek, November 13, 2017, 01:06:26 PM

Previous topic - Next topic

StarGeek

Here's a simple user defined tag that will return the US Postal Code based upon XMP:State or IPTC:Province-State metadata in the file.

Example output:
C:\>exiftool -config USPostalCode.config -USPostalCode -State y:\!temp\Test3.jpg
US Postal Code                  : ME
State                           : Maine


edit:  Updated and renamed
edit 2:  Updated again
"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

Phil Harvey

Hi StarGeek,

Interesting, but I was expecting something else when I opened the file.  A postal code in canada is what you call a zip code in the US.

If you want, you could add Canada postal abbreviations to the list:

'alberta' => 'AB',
'british columbia' => 'BC',
'manitoba' => 'MB',
'new brunswick' => 'NB',
'newfoundland' => 'NL',
'nova scotia' => 'NS',
'northwest territories' => 'NT',
'nunavut' => 'NU',
'ontario' => 'ON',
'prince edward island' => 'PE',
'quebec' => 'QC',
'saskatchewan' => 'SK',
'yukon' => 'YT',

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

StarGeek

Yeah, looking around now there is some discrepancies with what they're called.  But Abbreviations seems to be the official term for the USPS, so I'll change it.  I did think about adding Canada and Mexico as well, but got lazy.  I found the USPS page with the US possessions such as Puerto Rico and Guam and I'll probably add those as well. 

Hmm... Maybe not Mexico.  I'm not sure I want to deal with accent marks.

"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

StarGeek

Updated to add Canada and Mexico to the list.  Renamed to reflect that it was no longer just US state abbreviations.

But there's also a problem, probably a Windows only one.

I added a subroutine to deal with accented characters (based upon this StackOverflow answer).  The problem is that it doesn't work one Windows unless the -L option is added.  For example, if the State is Québec, it appears that internally that the MyNormalize routine is getting a value of QuA©bec, same as if it was printed on the Windows command line.

Any way to to do this properly?
"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

Phil Harvey

Ah.  MyNormalize is assuming wide characters, but ExifTool treats all strings as byte streams internally.  You may have to convert the byte stream by calling utf8::decode.  I don't have time to try this right now, but try adding this as the second line of MyNormalize:

    utf8::decode($_);

You may have to add "use utf8;" to your config file.

Also, I would have to think about how to deal with non-utf8 encodings... later when I have more time.

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

StarGeek

That worked.

As an additional bonus, my main reason for following through on this, is that MyNormalize() can be used with the -api Filter to work on any tag if desired.

C:\Windows\System32>exiftool -config C:\Programs\UnixUtils\ExifTool_Configs\StateAbbrev.config -a -s -G1 -state -state# -Location -Location# -api "filter=MyNormalize($_)" y:\!temp\Test3.jpg
[XMP-photoshop] State                           : Quebec
[XMP-photoshop] State                           : Québec
[XMP-iptcCore]  Location                        : Yucatan
[XMP-iptcCore]  Location                        : Yucatán

"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

Phil Harvey

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

ekkidee

That's pretty slick. Could this be done for timezone names too? (Or does exiftool already produce names when given a GMT offset?)

StarGeek

Depends upon what you mean by timezone names, but I think not without some extra work.  If you mean something that EST, then there's other things to take into account.  For example, given an offset of -5, is that EST or CDT?  So then you have to take into account location and the timestamp. 
"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