Command to reapply hierarchical tags as distinct tags (on an incremental basis)

Started by mlaverdiere, April 25, 2018, 03:18:00 PM

Previous topic - Next topic

mlaverdiere

Hi,

Having used exiftool with great success/pleasure for various tasks, I doubt that I will be able to overcome this "exotic" problem... but just in case, here is my question:

Let's say I have a bunch of pictures with the following hierarchical tags: 

Nature|Flower|Rose

Would it be possible to use exiftool to automatically retag these pictures with each distinct part of the hierachical tags (on an incremental basis), so the result would be the following:

Nature
Nature|Flower
Nature|Flower|Rose

Please note that my goal is not to insert the specific tags in the command (i.e. Nature|Flower|Rose) but to have a generic command that would reapply any hierarchical tag as described above.

Thanks in advance for any help with this.

p.s.: If someone wonder why I'm looking for this, see this darktable bug report: https://redmine.darktable.org/issues/12132

Phil Harvey

This is possible using the advanced formatting option and a bit of Perl trickery:

exiftool '-hierarchicalsubject<${hierarchicalsubject;my @a=split /--/;my @b=@a;my $n=@b;for (;;) { foreach (@a) { push @b, $_ if s/\|[^|]+$// } last if $n==@b; $n = @b} $_=join "--",@b}' a.jpg -sep '--' FILE

The above quoting is for Mac/Linux.  Swap the double and single quotes for Windows.

There may be a simpler way, but this worked for me.

% exiftool -hierarchicalsubject='Nature|Flower|Rose' -hierarchicalsubject='this|that' a.jpg
    1 image files updated
% exiftool -hierarchicalsubject a.jpg
Hierarchical Subject            : Nature|Flower|Rose, this|that
% exiftool '-hierarchicalsubject<${hierarchicalsubject;my @a=split /--/;my @b=@a;my $n=@b;for (;;) { foreach (@a) { push @b, $_ if s/\|[^|]+$// } last if $n==@b; $n = @b} $_=join "--",@b}' -sep '--' a.jpg
    1 image files updated
% exiftool -hierarchicalsubject a.jpg
Hierarchical Subject            : Nature|Flower|Rose, this|that, Nature|Flower, this, Nature


I hope the order doesn't matter.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

mlaverdiere

Just to confirm that Phil's proposal is working like a charm (as always!).

A picture with tags Nature|Flower|Rose, will get the following tags after being "treated" with Phil's proposed command:

Nature
Nature|Flower
Nature|Flower|Rose

Now, in case it might be useful to someone: I want to use this command in a daily cron job and I do observe that if you apply this command twice on a picture, all tags will get duplicated. The remedy I found is to use the following command, to delete duplicated tags:

exiftool -m -overwrite_original -sep '##' '-hierarchicalSubject<${hierarchicalSubject;NoDups}' FILE

Many thanks.