ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: brightwolf on April 29, 2021, 04:05:53 PM

Title: Adding filename parts as keywords
Post by: brightwolf on April 29, 2021, 04:05:53 PM
Hello, I have found the way to split the directory path of each photo file and add the parts as keywords in an other post on this forum here:  Help in adding path information as keywords (http://help%20in%20adding%20path%20information%20as%20keywords). It works like a charm. But I cannot figure out how to also add parts from the filename to the keywords.

Here's what I have achieved so far. For example, I have the following photo file: /Users/me/Pictures/Photo Albums/Holidays/Egypt 1997/IMG_1099.JPG
When I run the path-to-keywords script, my photo ends up having the following keywords: Holidays, Egypt, 1997

But image I would rename the photo file so it included some photo specific keywords, for example the precise location or the persons in the photo: /Users/me/Pictures/Photo Albums/Holidays/Egypt 1997/Pyramids Me Wife IMG_1099.JPG
Then, when I would run the path-file-to-keywords script, I would end up having the following keywords: Holidays, Egypt, 1997, Pyramids, Me, Wife
(and probably one additional keyword IMG_1099.JPG but that is no big deal).

How could I achieve this?

The (shortened) version of my script looks like this:

DIRTREEDIR="/Users/me/Scripts/Exiftool"
DIR=$1
IFS=$'\n'; set -f
for f in $(find $DIR -name '*.jpg' -or -name '*.JPG' -or -name '*.cr2' -or -name '*.CR2' -or -name '*.tif' -or -name '*.TIF')
do
echo "$f"
exiftool -config "$DIRTREEDIR/exiftool_dirtree_config" -r -m "-Keywords+<dirtree" "$f" -api listsplit="[/ ]" -overwrite_original
echo "DONE $f: Keywords added"
done
unset IFS; set +f
echo "### All done."


The dirtree script it is referring to looks like this:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        DirTree => {
            Require => 'Directory',
            ValueConv => q{
                $val =~ s(.*Photos[\\/])();
                my @parts = split '/', $val;
                shift @parts if @parts and not $parts[0];
                return \@parts;
            },
        },
    },
);
1;  #end
Title: Re: Adding filename parts as keywords
Post by: Phil Harvey on April 29, 2021, 04:25:16 PM
I don't have time to test this right now, but try this:

exiftool -addtagsfromfile @ "-subject<${directory;s{.*Photo.*?/)()}" "-subject<basename" -listsplit "[ /]" DIR

This should remove everything up to and including a directory name containing "Photo", then split on "/" or SPACE, and add to XMP:Subject (the contemporary equivalent of IPTC:Keywords), then also add the filename without extension, split into words on SPACE.  Requires ExifTool 12.22 or later.

- Phil
Title: Re: Adding filename parts as keywords
Post by: brightwolf on April 29, 2021, 06:02:02 PM
Thanks Phil, for your suggestion. I managed to get the following command working:
exiftool -addtagsfromfile @ '-subject<${directory;s{.*Photo.*?/)()}' -r -m "-subject<basename" -listsplit "[ /]" -overwrite_original

It adds two keywords: the path (it seems to ignore the "Photo" clause) and the filename without extension.
What I am trying to achieve is, to have all parts of the directory and the filename added as individual keywords.

So: /Photo/Holiday/Egypt 1997/Me Wife IMG_1099.JPG
would yield at least the following keywords: Holiday; Egypt; 1997; Me; Wife

I can reach the first path of this goal by using the script I already have. It is slow because it loops over all files individually, but it gets the job done.
This additional command would add "Me Wife IMG_1099" as one extra tag. How can I get the name parts of the filename split on the space?
Title: Re: Adding filename parts as keywords
Post by: Phil Harvey on April 29, 2021, 07:15:01 PM
Ooops.  I got my brackets wrong.  It should be:

exiftool -addtagsfromfile @ "-subject<${directory;s(.*Photo.*?/)()}" "-subject<basename" -listsplit "[ /]" DIR

And if this works as intended, the file name should split on spaces.

- Phil
Title: Re: Adding filename parts as keywords
Post by: brightwolf on April 30, 2021, 01:05:15 PM
Thanks Phil! I had to rewrite the command a bit further to this:

exiftool -addtagsfromfile @ '-subject<${directory;s(.*Photo.*?/)()}' '-subject<basename' -api listsplit='[ /]' DIR

to get it to work. But it works like a charm now, many thanks!
Title: Re: Adding filename parts as keywords
Post by: Phil Harvey on April 30, 2021, 01:18:31 PM
Right.  Forgot that ListSplit is a -api option.

- Phil
Title: Re: Adding filename parts as keywords
Post by: brightwolf on April 30, 2021, 04:25:23 PM
Just to say thanks again Phil, for your help and your insanely helpful tool.

And for the ones after me, who are looking for something similar, here's the command line that meets all my requirements:
exiftool -r -m -addtagsfromfile @ '-subject<${directory;s(.*Photos.*?/)()}' '-subject<${filename;s(\S*\..*)()}' -api listsplit='[ /]' -overwrite_original DIR

Example: Photo file /Users/me/Photos/Holidays/Egypt 1997/Pyramids Me Wife IMG_1099.JPG
will yield the keywords: Holidays, Egypt, 1997, Pyramids, Me, Wife
Title: Re: Adding filename parts as keywords
Post by: knighter_au on August 04, 2024, 07:46:09 AM
Thanks for this amazing tool and for this awesome thread!
For those on Windows(!), just wanted to show the Windows equivalent which is working for me...

exiftool -r -m -addtagsfromfile @ "-subject<${directory;s(.*photos.*?/)()}" -api listsplit="[ /]" -overwrite_original DIR
For my use, I did not need to add tags from the filename (just the directory).