Trying to clear xmp except keywords and then copy keywords

Started by Ferdinand Paris, August 20, 2014, 08:41:56 AM

Previous topic - Next topic

Ferdinand Paris

I have an xmp sidecar and a jpg file.  I'm trying to clear the xmp from the jpg, excepting any existing keywords, and then copy the keywords from the xmp sidecar to the jpg.

This line doesn't copy the keywords, and also also wipes any existing keywords in the jpg:
exiftool.exe -overwrite_original_in_place -xmp:all= --xmp-lr:hierarchicalSubject --xmp-dc:subject -Tagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg

I'm guessing that the line in the documentation under --TAG:  "Once excluded from the output, a tag may not be re-included by a subsequent option"  explains why the copy doesn't work, although I'd have thought that at least the existing keywords would be retained.

Therefore this line does perform the copy, but doesn't retain the keywords in the JPG:
exiftool.exe -overwrite_original_in_place -xmp:all= -Tagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg

Is it possible to do what I'd trying to do?

Phil Harvey

Quote from: Ferdinand Paris on August 20, 2014, 08:41:56 AM
exiftool.exe -overwrite_original_in_place -xmp:all= --xmp-lr:hierarchicalSubject --xmp-dc:subject -Tagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg

I'm guessing that the line in the documentation under --TAG:  "Once excluded from the output, a tag may not be re-included by a subsequent option"  explains why the copy doesn't work, although I'd have thought that at least the existing keywords would be retained.

This comment applies when reading.  When writing, this is the appropriate section of the --TAG documentation:

            [...] May also be used following a
            -tagsFromFile option to exclude tags from being copied (when
            redirecting to another tag, it is the source tag that should be
            excluded), or to exclude groups from being deleted when deleting
            all information (eg. "-all= --exif:all" deletes all but EXIF
            information).  But note that this will not exclude individual tags
            from a group delete
(unless a family 2 group is specified, see
            note 4 below).  Instead, individual tags may be recovered using
            the -tagsFromFile option (eg. "-all= -tagsfromfile @ -artist").


QuoteIs it possible to do what I'd trying to do?

I think this may do what you want:

exiftool -xmp:all= -addtagsfromfile AA.jpg -xmp-lr:hierarchicalSubject -xmp-dc:subject -addtagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg

(notice I used -addtagsfromfile AA.jpg instead of -addtagsfromfile @, because it seems there is a bug in -addtagsfromfile which doesn't allow it to work properly with a multiple files and a variable filename -- I'll fix this.)

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

Ferdinand Paris

Thanks Phil.  That makes sense when you think look at it, but unfortunately it doesn't work.

I can see that
exiftool -xmp:all= -addtagsfromfile AA.jpg -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg
is successful in wiping xmp while keeping the two keywords fields.

But as soon as I add in the command to also copy keywords from the xmp sidecar:
exiftool -xmp:all= -addtagsfromfile AA.jpg -xmp-lr:hierarchicalSubject -xmp-dc:subject -addtagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg
what I get is the keywords from the sidecar, but none of the original keywords in the jpg.

If I reverse the order of the copy commands:
exiftool -xmp:all= -addtagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject -addtagsfromfile AA.jpg -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg
then I get the opposite.  The keywords are those originally in the jpg, and there's no sign of those in the xmp sidecar.

It seems that whichever copy happens last takes precedence, i.e. each copy is a complete replacement.

I don't think it can be done this way.  This is part of an IMatch 5 script.  What I would need to do is to extract the keywords from each file, combine them and then give them to ExifTool to write, unless you can suggest another way.

Thanks for your help.

Phil Harvey

Here is what I get with your command:

> exiftool -ver
9.69
> exiftool AA.jpg -subject=one
    1 image files updated
> exiftool AA.xmp -subject=two
    1 image files created
> exiftool -xmp:all= -addtagsfromfile AA.jpg -xmp-lr:hierarchicalSubject -xmp-dc:subject -addtagsfromfile AA.xmp -xmp-lr:hierarchicalSubject -xmp-dc:subject AA.jpg
    1 image files updated
> exiftool AA.jpg -subject
Subject                         : one, two


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

Ferdinand Paris

If I replicate this exactly, it works.    :o    :-[

Now of course this example is stylised.  My naming etc is more complex.  So my question now is: what am I doing differently (i.e. wrong) in the production example??

Thanks again. :D

Ferdinand Paris

Got it!!!!!!!!!!!!!!!!!!   :-[    :)   :-[   :)

I have just discovered the difference between -tagsfromfile and -addtagsfromfile. 

Gosh there are a lot of options in ET, aren't there?  And it helps you read your code snippets carefully.  Sorry.

And thanks.




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

Ferdinand Paris

A further question if I may.  In this example, is there any easy way to prevent or remove duplicates?  I.e., if the same keyword exists in both the .xmp file and the .jpg file, is there a way to prevent the keyword appearing twice in the .jpg after the command is executed? 

I have a hunch that the answer is no, for my reading of the documentation.  There is an --a option, but I can't get it to work and my reading of the documentation seems to imply to that it doesn't apply in this case.

Ferdinand Paris

p.s.  I found the faq entry about preventing duplication of entries like keywords.  Is there a syntax like that that I can use in my example, where I an copying entries from a file rather than adding them in one by one?

Phil Harvey

#9
There is a syntax, but it doesn't work properly when importing from multiple files (I'll look into this).  For one file, it looks like this:

exiftool -tagsfromfile AA.xmp "-subject-<subject" "-subject" AA.jpg

So you would need to clear the other XMP tags in a separate command.

- Phil

Edit: I've looked into this, and understand the behaviour.  This trick doesn't work when copying from multiple files because "-<" only removes existing list items.  But we have already removed all list items with -xmp:all=, so it has no effect in this command.  There is currently no way to remove a single list item from the new items which have been queued up to be written.  Either you replace all new items (-tagsFromFile), or you add to them (-addTagsFromFile), but there is nothing in between.
...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 ($).

Ferdinand Paris

#10
Yes, I think that this requirement, while not necessarily exotic or obscure, is still a bit too much to do in one run.

Fortunately I am doing this in an IMatch script and so can harvest the existing keywords from the jpg in advance and insert them into command line.  Although it's not entirely elegant.  But even then there are traps.  For example this:

exiftool -xmp:all= -tagsfromfile AA.xmp -xmp-dc:subject -xmp-dc:subject-=keyone -xmp-dc:subject+=keyone [etc] AA.jpg

can still result in duplicates it seems.  I guess this is because the subtraction won't remove any keywords copied from the xmp until that copy has been executed.  Or perhaps my syntax is wrong.  Anyway, this version below seems to have worked and doesn't create duplicates:

exiftool -xmp:all= -tagsfromfile AA.xmp -xmp-dc:subject AA.jpg -execute -xmp-dc:subject-=keyone -xmp-dc:subject+=keyone [etc] AA.jpg -execute

Is this the best way of doing this?

[p.s.  I found this post, which may have been another solution, but it looks too complex to be used in an IMatch script that will be posted online and used by others:

https://exiftool.org/forum/index.php/topic,4137.0.html   ]

Phil Harvey

Yes, this is the simplest solution.

You could also create a user-defined tag to remove the duplicates, but that wouldn't solve the problem of duplicates from different -tagsFromFile files in the same command.

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