Move exisitng folder name (structure) into image "tag"

Started by jappie, September 12, 2013, 07:33:43 AM

Previous topic - Next topic

Phil Harvey

This is one way:

exiftool "-keywords<${directory;tr( )(/)}" -sep "/" -r DIR

This command translates all spaces to "/" in the directory name, then splits them into separate keywords at the /'s.

And here is another way:

exiftool "-keywords<directory" -api listsplit="[/ ]" -r DIR

which uses the API ListSplit option to split on spaces or slashes.  (The API ListSplit option allows you to specify a regular expression, while the command-line -sep option does not.)

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

StarGeek

I believe the original post was before the advanced formatting feature was added.  With an up to date copy of exiftool, you could do this inline.

Assuming "Album" doesn't occur anywhere else in your directory structure, you could do this:
exiftool -r -sep "," "-keywords<${directory;s/.*\/(Album.*)/$1/;s/( +|\/)/,/g}" FileOrDir

This will first remove the directory path that's before "Album" (note that this is case sensitive), then replace all spaces and slashes in the paths with commas.  The sep option then sets the keywords using the comma as a separator.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

@StarGeek:  Good point about removing stuff before Album.  I was implicitly assuming that DIR would be "Album", which would require cd-ing first to the parent directory.  But other than that we had a similar initial idea.  But my 2nd thought of using the ListSep option is perhaps a bit simpler, but does require cd-ing to the parent directory first.

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

StarGeek

:D

Even though I know you posted an answer before me, I'm not going to let my slowness stop me from saying "I had an idea, too!"  I already invested the time in typing up an answer and double checking it, so I'm going get my say in.

* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

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

brightwolf

Excellent, Phil & StarGeek. Thanks for your quick answers. I already started changing the config file to use regex in the split (and running into problems with that, since all / and space characters were created as keywords too). The command line with -listsplit is so much simpler and yields exactly the desired result!

Note that "exiftool -r -sep "," "-keywords<${directory;s/.*\/(Album.*)/$1/;s/( +|\/)/,/g}" FileOrDir' yields a "bad substitution" problem. Since the "listsplit" option works for me, I did not further look into that.. 
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

StarGeek

Hmmm...  What version of exiftool are you using?  Or are you on Linux/Mac? 

The command should work.  I just copy/pasted it right now to test it and no error.

Either way, no worries, as long as you got something working for you.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

brightwolf

#22
Hi StarGeek. No worries...
I am using exiftool 10.21 on a Mac.

One more question I have about this command. It preserves existing keywords (which is good) but it also preserves the original photo as <photofile>_original. Why is that? Does updating the EXIF tags reprocess the JPG, causing some information loss? Or is it for another reason? If no quality is to be expected, can I somehow prevent the copy from happening? Note that I have more than 55000 photos to process with this command, amounting to almost 500 GB of data, and I have no intention of doubling that. Note also that I already have a backup (multiple, in fact, both on and off site). 
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

StarGeek

Quote from: brightwolf on March 29, 2017, 03:42:48 PM
I am using exiftool 10.21 on a Mac.

Ah, the problem then was with the double quotes.  Any time Mac/Linux has a $ in the command, then single quotes need to be used instead of double quotes (see .sig on Phil's post).

QuoteIt adds to the Keywords tag (which is good)

One thing that we forgot to mention is that these commands will overwrite the keywords with the new data.  If there already were some keywords, these will be lost.  If any of the files already have keywords, use +< instead of <.

Quotebut is also preserves the original photo as <photofile>_original. Why is that?

Exiftool will normally make a backup of the original file.  See second paragraph under Description in the docs.  If you want to avoid that, add -overwrite_original to the command.  Or you can delete them afterwards with the -delete_original command.

QuoteDoes updating the EXIF tags reprocess the JPG, causing some information loss?

Exiftool only changes metadata, never the original image.  Though there are a few metadata tags that can affect how the image is show, ICC_Profile for example.  But then, those are tags you shouldn't be messing with unless you know what you are doing anyway.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

brightwolf

OK clear, thanks for the quick update. I already replaced 'keywords<' with 'keywords+<', this indeed has the desired effect of existing keywords being preserved. As far as the copy is concerned, I will add the flag you mentioned. Thanks!
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

brightwolf

I have one observation I want to share here.

StarGeek, in your command using -sep, you included some regex to replace all folders before the 'Album' one so those would not show as keywords.

However, when either CD'ing to the Album folder and then running the exiftool command, or when mentioning the full folder in the exiftool command itself (eg User/Album/* instead of *) the exiftool ignores that first part of the folder structure already, and there's no need to replace it in the regex.

Is this intended behaviour? It works for me, though. Just wanted to share this observation.
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

StarGeek

I'm not sure I understand your question.  If you CD to the directory, the regex doesn't match and no replacement is done and all is fine.  But if you use the full path in the command, then directories above the "Album" should also be included.  For example, "X:" and "!temp" are included if my path is X:\!temp\Album and I don't include the first regex
C:\Programs>exiftool -r -sep "," "-keywords<${directory;s/( +|\/)/,/g}" X:\!temp\Album
    4 directories scanned
    2 image files updated

C:\Programs>exiftool -r -keywords X:\!temp\Album
======== X:/!temp/Album/Holiday/2010 Paris France/20151212-12.12.12.jpg
Keywords                        : X:, !temp, Album, Holiday, 2010, Paris, France
======== X:/!temp/Album/Holiday/2011 Montpellier France/20151212-12.12.12.jpg
Keywords                        : X:, !temp, Album, Holiday, 2011, Montpellier, France
    4 directories scanned
    2 image files read


I can't test the command on mac since I don't have access, but if you don't cd to the directory and instead use the full path, in your example, "user" should also be added to the keywords.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).