Adding filename parts as keywords

Started by brightwolf, April 29, 2021, 04:05:53 PM

Previous topic - Next topic

brightwolf

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. 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
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

Phil Harvey

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

brightwolf

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?
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

Phil Harvey

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

brightwolf

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!
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

Phil Harvey

Right.  Forgot that ListSplit is a -api option.

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

brightwolf

#6
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:

  • It uses the directory to set the keywords, splitting on either / or space
  • It disregards the directory path until a certain (unique) folder name (in my case, 'Photos'). Only subfolders of Photos are used to set the keywords.
  • It uses the filename to set keywords too, splitting on space
  • It disregards the original filename, like IMG_1099.JPG, so I do not end up with filenames as keywords
  • It overwrites the original with the new keywords so I do not end up with duplicate photo files
  • It can be ran as often as needed: in a next run, newly discovered keywords will be added, but existing keywords will not be duplicated
  • It does all this recursively on the directory (no looping over individual photo files as I did in my original script - much faster)
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
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

knighter_au

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