ExifTool Forum

ExifTool => Newbies => Topic started by: endelos on January 09, 2022, 12:33:55 PM

Title: Write exif tags by extracting parts of the filename
Post by: endelos on January 09, 2022, 12:33:55 PM
Hi everybody,

I'm a complete newbie with ExifTool, and I'm struggling to extract parts of the filename of my images.

My goal is to write different tags from those parts.
Ex :
My filename : date. title - tech - dimensions - location
1638-39c. Mercury takes Bacchus to be Brought up by Nymphs - oil on canvas - 113 x 135 cm - St Petersburg, The Hermitage
(some dates are numbers only: just "1638.")

date > EXIF: XPKeywords
title > EXIF: ImageDescription
tech > EXIF: XPSubject
dimensions > EXIF: XPComment
location > EXIF: Copyright

I could find a bit of documentation here and there but I'm very bad at using REGEX, the best I've come up with for now is :
exiftool "-exif:XPKeywords<${Filename;s/\..*//}" "-exif:imagedescription<${Filename;s/-.*//}" "-Copyright<${Filename;s/.*?cm\s-(.*)\.[^.]+/$1/;}" "O:\myDir"

wich extract the location in copyright correctly (it copies everything after "cm -", which is always the end of the dimensions part).
It extracts everything before the first "." the date to XPKeywords.

I'm still working on the imageDescription's one...

I'm totally lost, if anyone is good with REGEX, I would really appreciate your help !
Thank you !
Title: Re: Write exif tags by extracting parts of the filename
Post by: StarGeek on January 09, 2022, 01:16:06 PM
Try this
exiftool "-XPKeywords<${BaseName;m/(^[^\.]+)\. .+? - .+? - .+? - .+$/;$_=$1}" "-ImageDescription<${BaseName;m/^[^\.]+\. (.+?) - .+? - .+? - .+$/;$_=$1}" "-XPSubject<${BaseName;m/^[^\.]+\. .+? - (.+?) - .+? - .+$/;$_=$1}" "-XPComment<${BaseName;m/^[^\.]+\. .+? - .+? - (.+?) - .+$/;$_=$1}" "-Copyright<${BaseName;m/^[^\.]+\. .+? - .+? - .+? - (.+)$/;$_=$1}" /path/to/files/

Example output
C:\Programs\My_Stuff>exiftool -P -overwrite_original "-XPKeywords<${BaseName;m/(^[^\.]+)\. .+? - .+? - .+? - .+$/;$_=$1}" "-ImageDescription<${BaseName;m/^[^\.]+\. (.+?) - .+? - .+? - .+$/;$_=$1}" "-XPSubject<${BaseName;m/^[^\.]+\. .+? - (.+?) - .+? - .+$/;$_=$1}" "-XPComment<${BaseName;m/^[^\.]+\. .+? - .+? - (.+?) - .+$/;$_=$1}" "-Copyright<${BaseName;m/^[^\.]+\. .+? - .+? - .+? - (.+)$/;$_=$1}"  "Y:\!temp\!aaa\1638-39c. Mercury takes Bacchus to be Brought up by Nymphs - oil on canvas - 113 x 135 cm - St Petersburg, The Hermitage.jpg"
    1 image files updated

C:\Programs\My_Stuff>exiftool -G1 -a -s -EXIF:All "Y:\!temp\!aaa\1638-39c. Mercury takes Bacchus to be Brought up by Nymphs - oil on canvas - 113 x 135 cm - St Petersburg, The Hermitage.jpg"
[IFD0]          ImageDescription                : Mercury takes Bacchus to be Brought up by Nymphs
[IFD0]          XResolution                     : 72
[IFD0]          YResolution                     : 72
[IFD0]          ResolutionUnit                  : inches
[IFD0]          YCbCrPositioning                : Centered
[IFD0]          Copyright                       : St Petersburg, The Hermitage
[IFD0]          XPComment                       : 113 x 135 cm
[IFD0]          XPKeywords                      : 1638-39c
[IFD0]          XPSubject                       : oil on canvas


This is admittedly longer than it needs to be because I just copy/pasted the pattern for all inputs, changing only the capture group.  It also requires that the format be exactly as you posted, no extra spaces and no hyphens as part of the text you want to copy.

Title: Re: Write exif tags by extracting parts of the filename
Post by: endelos on January 09, 2022, 01:43:33 PM
This works incredibly good !  :o

I will dive in this expression in order to understand all of it. I've never used the match system m/, only the substitute's one.

Anyway, thank you very much for your help, I was still struggling with the ImageDescription capture group, you saved me a few days and a nervous breakdown ^^
:D
Title: Re: Write exif tags by extracting parts of the filename
Post by: Redbill73 on January 14, 2022, 04:09:38 PM
Hello,
could you please help me?
I have thousands of photos with such filename format:
2019-10-23 Kids on the beach 001.jpg
2018-03-14 Playing with the dogs 003.jpg

I want the date to go to the date taken field, the text to subject field and the number nowhere. Is there a way?
Title: Re: Write exif tags by extracting parts of the filename
Post by: StarGeek on January 14, 2022, 05:06:33 PM
By "subject", do you want it to go in as a keywords (which would be the Subject tag), or as a description?

This will place the text in as a keyword
exiftool "-DateTimeOriginal<${Filename;m/(\d{4}-\d\d-\d\d)/;$_=$1} 00:00:00" "-Subject<${Basename;m/ (.+) \d+$/;$_=$1}" /path/to/files/

Replace Subject with Description to place the text as a description. 

Because the date/time tag requires a time which isn't included in the filename, this command will set the time to midnight on the day extracted from the filename.

This command creates backup files.  Add -Overwrite_Original (https://exiftool.org/exiftool_pod.html#overwrite_original) to suppress the creation of backup files.  Add -r (https://exiftool.org/exiftool_pod.html#r-.--recurse) to recurse into subdirectories.  If this command is run under Unix/Mac, reverse any double/single quotes to avoid bash interpretation.
Title: Re: Write exif tags by extracting parts of the filename
Post by: Redbill73 on January 14, 2022, 06:28:49 PM
Thank you very much for your immediate response! I'm sure it'll work like a charm! I'll let you know!