skip empty fields when joining metadata into a string

Started by Harald, March 09, 2017, 10:07:39 AM

Previous topic - Next topic

Harald

I want to change the description in my image files with a concatenation of the current description, country, city, location and photographer. This works well:
WINDOWS:exiftool -m  "-xmp:description<${xmp:description} - ${xmp:location}, ${xmp:city}, ${xmp:country}; Photographer: ${xmp:creator}; ${exif:createdate}" -d %Y-%m -usercomment='processed' DIR


INPUT:
Description                     : Long Life puja offered by Sera Je Monastery, monks offering the puja.
City                                : Bodhgaya
Country                          : India
Creator                           : Losang Sherab
Create Date                    : 2017:01:02 17:03:25

OUTPUT:
"Long Life puja offered to Lama Zopa Rinpoche by Sera Je Monastery, monks offering the puja. - , Bodhgaya, India; Photographer: Losang Sherab; 2017-01"

As you can see the missing location generates an extra " ," which I would like to get rid of.
Thanks a lot in advance for your help.


Phil Harvey

Try this:

WINDOWS:exiftool "-xmp:description<${xmp:description} - ${xmp:city}, ${xmp:country}; Photographer: ${xmp:creator}; ${exif:createdate}" "-xmp:description<${xmp:description} - ${xmp:location}, ${xmp:city}, ${xmp:country}; Photographer: ${xmp:creator}; ${exif:createdate}" -d %Y-%m -usercomment='processed' DIR

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

Harald

Hi Phil,
thanks for your quick reply.
This is definitely beyond me, but in any case, it seems not work.

This is the output after running the command on a file where the "City" is not specified, thus note the ", ," after "Kopan Monastery":

Enthronement of Thubten Rigsel Rinpoche, with Lama Zopa Rinpoche and Khen Rinpoche Geshe Chonyi - Kopan Monastery, Nepal; Photographer: Losang Sherab - Kopan Monastery, , Nepal; Photographer: Losang Sherab;
2017-03
Maybe I did not make clear the issue? When fields are empty, the command should ignore the empty field AND not put the "," in the concatenated string.

I hope you have an alternative solution, as I just spent several hours yesterday fixing captions. Thanks in any case for your great support and this awesome tool.
kind regards,
Harald

Phil Harvey

Hi Harald,

My example was only for a missing xmp:location.  This technique isn't well suited to handling arbitrary missing tags.  For this, a user-defined Composite tag is definitely the way to go.

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

Harald

Thanks a lot, Phil. I had a look into this composite declarations, but lacking all basis (I guess that is Perl based syntax?) I have no clue where to start and 1 hour research on Composite tags did not unearth a lot either.
Are you or anybody you know doing consultancy work on demand (which would be delivering the ready made composite description)? If you do, how much would it be? And if you know somebody, maybe you could get me in touch.

Thanks a lot in advance,
Harald

Phil Harvey

#5
Hi Harald,

OK, well thanks for trying.  Hopefully you learned something.  The attached config file should do what you want with this command:

exiftool -config harald.config-if "not defined $usercomment or $usercomment ne 'processed'" "-xmp:description<mytag" -d %Y-%m -usercomment="processed" DIR

- Phil

Edit: Changed config file to use "$prt[5]" instead of "$val[5]" for print-formatted date/time value
...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 ($).

Harald

You are my hero, Phil  ;D thanks a lot. I will have a look at it tomorrow and report back. And it is definitely time for a new donation. People like you should also get material reward for their generosity of time. And of course, being a Buddhist, I know that you will get plenty of reward because of the good karma you create. And I guess the tons of appreciation you receive is anyway more worth then what money can buy :)
Thanks a lot again.
kind regards,
Harald

_____________________________

Harald

Hi again, Phil.
tip should arrive any moment at your account :)
Your config works like a charm. Nothing short of magic :)

Only one small thing left: I had set the date formatting to -d %Y-%m  in my commands and nicely ended up with 2017-02 instead of 2017:01:02 17:03:25 at the end of the caption. I tried to apply these in the config file on both, the "5 => 'EXIF:CreateDate'," and the push @list, $val[5] if defined $val[5]; but obviously it is not that simple. :)
May I ask you where to put that formatting?

Thanks a lot in advance for your help.


Phil Harvey

Ah, right.  I should have thought of that.  Replace "$val[5]" with "$prt[5]" in the config file to use the print-formatted version of the date/time value.  I have updated the attached config file in my previous post accordingly.

And thanks for the donation!

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

Harald

Hi Phil,
thanks a lot. I am afraid I do not get it.
I downloaded the updated config file but it does not change anything. To be sure it is loaded I replaced "MyTag" with "WebCaptionTag" and it works to call it with that name.
I typed "exiftool -WebCaptionTag ."
OUTPUT: ======== ./DSC02521-2.jpg
Web Caption Tag                 : Long Life puja offered to Lama Zopa Rinpoche by Sera Je Monastery, monks offering the puja. - Bodhgaya, India; Photographer: Losang Sherab; 2017:01:02 17:03:25

So the config loads.

Do I have to give the formatting options in the command?

Also I then have to write this info into IPTC:caption-abstract. But when I type
exiftool  -overwrite_original_in_place -iptc:caption-abstract<${WebCaptionTag} .
it says: " ${WebCaptionTag}: ambiguous redirect"
??
Sorry to be such a bother  :-[

Phil Harvey

Quote from: Harald on March 15, 2017, 09:48:42 AM
Do I have to give the formatting options in the command?

Yes.

Quoteexiftool  -overwrite_original_in_place -iptc:caption-abstract<${WebCaptionTag} .
it says: " ${WebCaptionTag}: ambiguous redirect"

Try this:

exiftool -overwrite_original_in_place -d %Y-%m "-iptc:caption-abstract<WebCaptionTag" .

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

Harald

Hi Phil,

seems to be complicated  ::)
It works just as advertised. ON MAC  :P
On Windows, this is what happens:
If I just output the tag with
      exiftool -overwrite_original_in_place -d %Y-%m -WebCaptionTag .
I get
======== ./_DSC5882.jpg
Web Caption Tag                 : Rinpoche giving oral transmission at the ruins  of Nalanda, attended by Kopan monks and nuns and others - Bodhgaya, India; Photographer: Kunchok Gyaltsen; 2017:01:15 16:41:54
Obviously no change to previous

When I write it to the caption tag as advised
exiftool -overwrite_original_in_place -d %Y-%m "-iptc:caption-abstract<WebCaptionTag" .
I get this:
======== ./_DSC5882.jpg
Caption-Abstract                : Rinpoche giving oral transmission at the ruins  of Nalanda, attended by Kopan monks and nuns and others - Bodhgaya, India; Photographer: ARRAY(0x30da280); 2017:01:15 16:41:54
So it formats the photographer instead of the date.

:-\
Sorry, pile of obstacles :(
Thanks a lot in advance for your help.

Phil Harvey

That command should work the same on both Windows and Mac unless you are running from a Windows .BAT file in which case you must change all "%" to "%%" in the command.

The problem with the "ARRAY" is because there are multiple XMP:Creator items.  I didn't realize this was a list-type tag.  The attached config file should solve this problem by joining multiple Creators with a comma.

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

Harald

Hi Phil,

yes, it should :)
But of course we know about the difference between theory and practice :)

Anyway, your latest change works flawlessly. Amazing. Thanks so much.
I hope this thread turns out useful for many others.

And I hope I do not have to bother you now for quite some time. Wish you lots of strong winds this summer :)