How do I write keywords to the EXIF-section

Started by Rabiator, October 28, 2023, 08:17:46 AM

Previous topic - Next topic

Rabiator

Hello,

I use V 12.68 and have written a Powershell-script to have full control over the import-workflow of new images. Some import-workflows of programs out there sometimes do weired things with the metadata when importing images to their own databases. Some programs do not leave the images untouched for any reason.

The script runs like a charm. It uses an argsfile to update the metadata and keeps EXIFTOOL open with the stay_open operator.

I can add keywords by writing commands to the argsfile. Here are some working examples:

#writing to XMP-section, prevending from duplicates
-XMP:Subject-=one
-XMP:Subject+=one

#writing to IPTC-section, prevending from duplicates
-IPTC:Keywords-=one
-IPTC:Keywords+=one

But when I tried

-keywords-=one
-keywords+=one

the keyword 'one' was added to the IPTC-section, even if the keyword 'one' already existed in the IPTC-section. It made no difference to add the 'keywords'-parameter before or after the 'IPTC'-parameter in the argsfile.

I had a look at the XMP-file I create by default for every imported image, and it showed for IPTC-keywords 'one one', no comma-separation.

Does the EXIF-section support keywords, and if it does, what is the command-option to access it?

If the EXIF-section does not support keywords, is it the default behavior to write to the IPTC-section when adding '-keywords+=one' to the EXIFTOOL-parameters?

Did I do anything wrong when using all three keywords-command-parameters to write keywords to every accessible section of the metadata?

Thank you for your time.

Peace





Phil Harvey

Quote from: Rabiator on October 28, 2023, 08:17:46 AMBut when I tried

-keywords-=one
-keywords+=one

the keyword 'one' was added to the IPTC-section, even if the keyword 'one' already existed in the IPTC-section.

Can you provide a sample where this happens?  It doesn't happen when I test here.

QuoteDoes the EXIF-section support keywords

EXIF does not support Keywords.

QuoteIf the EXIF-section does not support keywords, is it the default behavior to write to the IPTC-section when adding '-keywords+=one' to the EXIFTOOL-parameters?

Yes.

QuoteDid I do anything wrong when using all three keywords-command-parameters to write keywords to every accessible section of the metadata?

It looks OK to me.

- 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

Quote from: Rabiator on October 28, 2023, 08:17:46 AMDoes the EXIF-section support keywords, and if it does, what is the command-option to access it?

Technically, there is the EXIF:XPKeywords tag but I suggest that you don't bother with it.  It is, for the most part, a Windows OS only tag.  I've only seen about 2 programs other than MS software that will read it. Additionally, it is not a list type tag.  Instead, it is a simple string tag with semicolons separating each word. Because of this, you can't use
-XPKeywords-=one
-XPKeywords+=one
You would instead have to make a more complex command that would check for to see if the keyword is already on the list and append it separately.

Quote from: Rabiator on October 28, 2023, 08:17:46 AMthe keyword 'one' was added to the IPTC-section, even if the keyword 'one' already existed in the IPTC-section. It made no difference to add the 'keywords'-parameter before or after the 'IPTC'-parameter in the argsfile.

I had a look at the XMP-file I create by default for every imported image, and it showed for IPTC-keywords 'one one', no comma-separation.

You cannot write IPTC data to an XMP file, only XMP data.  If you use the command in FAQ #3 on your XMP file, you will see that you probably are writing to the XMP-pdf:Keywords tag, which is for PDFs, not images.
* 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).

Rabiator

#3
This reproducable.

This is what I did:

  • I added the '-keywords'-property to the argsfile again
  • I entered two keywords for the three 'keywords'-parameters, separated by a comma
  • The script wrote the args-parameters for a file (listing below)
  • The two keywords were added to the metadata
  • I used XNVIEWMP to look at the metadata

This is the args-file for the entire image:

-filename=P:\atest\exif\temp\nikon\2003\20031130\20031130-133800,000_e5000_00001.nef
-make=NIKON
-model=E5000
-artist=Uwe Fißenebert
-copyright=(C) 2003, Uwe Fißenebert - All rights reserved
-keywords-=test one
-keywords+=test one
-keywords-=test two
-keywords+=test two
-XMP:Subject-=test one
-XMP:Subject+=test one
-XMP:Subject-=test two
-XMP:Subject+=test two
-IPTC:Keywords-=test one
-IPTC:Keywords+=test one
-IPTC:Keywords-=test two
-IPTC:Keywords+=test two
P:\atest\exif\source\nikon\DSCN0056.NEF

XNVIEWMP did not show any keywords in the EXIF-section, but in the XMP and ITPC-section:

Here is the screenshot I took using XNVIEWMP

This is the image with the updated metadata

This is the XMP-file, created from the image:

XMP-file of the entire image

The images and the XMP-file are available until Nov 2nd 2023.

Have a nice day.

PH Edit: Added links to images


Rabiator

Hm, the images do not show up for any reason, bit the inserted URLs are correct.

Phil Harvey

You gave links to the web page for downloading the image, not for the image itself.  I have added links to your text so we can click on these to download the images.

- 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

Quote from: Rabiator on October 30, 2023, 09:18:53 AMXNVIEWMP did not show any keywords in the EXIF-section, but in the XMP and ITPC-section:

And it won't, because the only keywords like tag in EXIF is the XPKeywords tag I mentioned above.

QuoteThis is the XMP-file, created from the image:

How are you creating this XMP file?  If you take a look at that XMP file, you will see that the only Keywords entry in it is the XMP-pdf:Keywords tag I mentioned above.
C:\>exiftool -G1 -a -s -Keywords Y:\!temp\x\y\20031130-133800,000_e5000_00001.nef.xmp
[XMP-pdf]      Keywords                        : test one , test two , test one , test two

XMP-pdf:Keywords is a simple string tag, not a list time tag. Whatever is creating it is doing so by copying the full value of IPTC:Keywords as a string.  See below as for why the keywords are repeated.

Note that also shows that you have trailing spaces in your Args file, which are included in the tag values.

As for the duplicated keywords, you are adding values to IPTC:Keywords twice
-keywords-=test one
-keywords+=test one
-keywords-=test two
-keywords+=test two
<snip...>
-IPTC:Keywords-=test one
-IPTC:Keywords+=test one
-IPTC:Keywords-=test two
-IPTC:Keywords+=test two

What you are doing here is removing any previous value of "test one" and "test two" twice. But then, you are adding these tags twice.

Step by step
You remove any pre-existing Keyword with a value of "test one"
You add a Keyword of "test one"
You remove any pre-existing Keyword with a value of "test two"
You add a Keyword of "test two"
You remove any pre-existing Keyword with a value of "test one".  This does not affect the value of "test one" you just added.
You add a Keyword of "test one"   You now have two keywords with a value of "test one"
You remove any pre-existing Keyword with a value of "test two" his does not affect the value of "test two" you just added.
You add a Keyword of "test two"   You now have two keywords with a value of "test two"

What you want to do is only add keywords once.  It doesn't matter if you use Keywords or IPTC:Keywords, you don't want to include both.  Also, you want to avoid copying Keywords to XMP, as that will copy to the XMP-pdf:Keywords.  If you are copying IPTC to XMP with exiftool, I'd suggest using the iptc2xmp.args file, as that will correctly copy IPTC tags to their corresponding XMP tags.
* 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).

Rabiator

Thank you for the quick response. This clairifies many things I have struggled with.

I did not know that adding the keywords by using the '-keywords'- or the 'IPTC:keywords'-parameter will be sufficient to put the keywords into any important metadata-section.

The XMP-file is created by using the '-tagsfromfile' <sourcefile> -all format. This command creates a XMP-file in the same directory as the sourcefile and a filename 'sourcefile.ext.xmp'. This is what I want.

And thank you for mentioning the trailing spaces in the keywords.

I spent hours reading the EXIFTOOL-docs, FAQs, I had a look at examples and tested EXIFTOOL-commands in any variation. And still there is so much to learn about EXIFTOOL. Maybe this is because I'm not really a technician or coder. But after all I now have a reliable script that fits my needs for transfering images from the camera-output to my image-data-management perfectly.

You did a great job, thank you for this software.

Thank you for your helping hand.

Peace

StarGeek

Quote from: Rabiator on October 31, 2023, 06:58:47 AMI did not know that adding the keywords by using the '-keywords'- or the 'IPTC:keywords'-parameter will be sufficient to put the keywords into any important metadata-section.

This is covered by the second paragraph on the -TAG[+-^]=[VALUE] option.  Specifically
    If no group name is specified, the tag is created in the preferred group, and updated in any other location where a same-named tag already exists. The preferred group in JPEG and TIFF-format images is the first group in the following list where TAG is valid: 1) EXIF, 2) IPTC, 3) XMP.

Since Keywords doesn't appear in EXIF, exiftool then will write it to IPTC.  It will also update (but not create it) it in other tags with the name Keywords if they exist.  For example, XMP-acdsee:Keywords and XMP-pdf:Keywords.  My advice is to try and keep the tag names you are writing as simple as possible and let exiftool sort out where to write them.  I go into more details in this post (which I need to expand on a bit more).

QuoteThe XMP-file is created by using the '-tagsfromfile' <sourcefile> -all format. This command creates a XMP-file in the same directory as the sourcefile and a filename 'sourcefile.ext.xmp'. This is what I want.

I strongly suggest using the iptc2xmp.args I linked above instead.  There are only 8 IPTC tags that directly match XMP tags.  And there are 15 IPTC tags that will not get copied to their corrisponding locations in XMP.  So your XMP files will be missing data.

There is also an exif2xmp.args file, but I don't use that one, as it creates a lot of tags that I think are better off staying in EXIF.  Any Digital Asset Management (DAM) program you use will copy any data it needs in the XMP file if necessary.


QuoteI spent hours reading the EXIFTOOL-docs, FAQs, I had a look at examples and tested EXIFTOOL-commands in any variation. And still there is so much to learn about EXIFTOOL.

It has literally taken me a decade (since 2011) of using this program to get as far as I have and there is still a lot of stuff I haven't mastered.  -Stay_Open is one of these and the fact that you have a script using it means you're already a step up on me on the subject.  I just haven't had the need to work it out yet.
* 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).

Rabiator

Thank you for your response. I like to mention one final thing that drove me nuts:

In an argsfile, the statements -Keywords (-Keywords, IPTC:Keywords) MUST have a capital 'K' or will be ignored. In EXIFTOOL-statements written to the console -keywords works fine for any reason.

There is only a few help outsind in the internet to get things working with EXIFTOOL and stay_open. Here is a section from my POWERSHELL-script that shows you how I start/stop EXIFTOOL-process and a statement for a single file in the argsfile:


#EXIFTOOL-start-arguments, $OutputArgsFile=fullpath to argsfile
$ExifArgs = " -stay_open True -@ $($OutputArgsFile)"

#open WINDOWS-process starting EXIFTOOL
$process = Start-Process -FilePath "exiftool.exe" -NoNewWindow -PassThru -RedirectStandardOutput "$($OutputProcessStdTxt)" -RedirectStandardError "$($OutputProcessErrTxt)" -ArgumentList $ExifArgs

# this is a section in the argsfile for a single file

-filename=path\to\target-filename.ext
-make=The_Camera_Manufactor
-model=The_Camera_Model
-artist=
-artist=Your name
-copyright=
-copyright=(C) YYYY, Your copyright statement
-usercomment=
-XMP:Description=My comment
-hierarchicalsubject-=level1|level2
-hierarchicalsubject+=level1|level2
-hierarchicalsubject-=group1|subgroup1
-hierarchicalsubject+=group1|subgroup1
-Keywords-=keyword1
-Keywords+=keyword1
-Keywords-=my keyword 2
-Keywords+=my keyword 2
path\to\source-filename.ext
-execute

#this is the last statement for the argsfile

-stay_open
False
-execute

#in the script, wait for EXIFTOOL-operations to terminate
$process.WaitForExit()

Stop-Process $process # terminate EXIFTOOL process

Maybe it helps you to create your own argsfile and starting-stopping WINDOWS-EXIFTOOL-process

And yes, getting WINDOWS process-command and stay_open working hand in hand was a challenge. In my script it is still in some kind of BETA-state, but the work gets done.

I tried asyncronous-mode for WINDPWS-process, too, to get more control over the process and its progress, but that got very complicate very soon. So currently my script (a windows-form) works mainly like this:

  • Select rootfolder for import-images (recursively)
  • Step1:call EXIFTOOL to collect all wanted images in a JSON-file
  • import JSON-file with valid files
  • import JSON-file with invalid files
  • add all the data to every valid files you need for EXIFTOOL-argsfile
  • store all the data in a SQLite-database, time to check the EXIFTOOL-import-errors manually
  • Step2:open database and read data for valid files
  • start WINDOWS-process calling EXIFTOOL stay_open with argsfile and names of output-log-files
  • loop over files and write argsfile-statements from database-values to argsfile
  • writing to the argsfile is much faster than EXIFTOOL-operations, so
  • after finishing writing the argsfile, wait for EXIFTOOL-process to terminate
  • update database-values with EXIFTOOL-results (success, error)
  • import EXIFTOOL-Std-log and write it to database
  • import EXIFTOOL-Err-log and write it to database

Step3 is creating XMP-sidecars for all (newly created) RAW-files. As the script keeps the original files untouched, it renames the imported files and copies them to a new destination (my default source-folder for post-production).

Peace

StarGeek

Quote from: Rabiator on November 05, 2023, 11:15:40 AMIn an argsfile, the statements -Keywords (-Keywords, IPTC:Keywords) MUST have a capital 'K' or will be ignored. In EXIFTOOL-statements written to the console -keywords works fine for any reason.

Something else must be happening here, as case doesn't matter regardless of if it is on the command line or in an args file

Example using args file with upper, lower, and alternating case
C:\>type temp.txt
-KEYWORDS=Word 1
-keywords=Word 2
-KeYwOrDs=Word 3
C:\>exiftool -P -overwrite_original -all= -@ temp.txt y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -keywords y:\!temp\Test4.jpg
[IPTC]          Keywords                        : Word 1, Word 2, Word 3
* 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).

Rabiator

I have no idea why this is the case, but it is a minor issue for me, because writing a capital 'K' is no problem.

There is another interesting effect:

I build the argsfile by a POWERSHELL-function. Here is a loop, that creates the entries for a keyword as part of a concatenated string $wordArgs:

foreach ($keyword in $keywords) {
if ($keyword -ne "") {
$wordArgs += "`n-Keywords-=$($keyword.trim())"
$wordArgs += "`n-Keywords+=$($keyword.trim())"
        }
}

#alternative code:
foreach ($keyword in $keywords) {
if ($keyword -ne "") {
$wordArgs += "`n-Keywords-={0}" -f $keyword.trim()
$wordArgs += "`n-Keywords+={0}" -f $keyword.trim()
        }
}


In both cases the keywords in the argsfile get an additional space-character:

# this is a copy from the argsfile, the trailing space is present
-Keywords-=one
-Keywords+=one

I checked the length of the keyword before it is added to the statement, and the length is 3.

Interesting, if I use the same code-structure for adding the make-tag, no additional space-character is added in the argsfile and in the metadata:

$ExifArgs += "`n-make={0}" -f $fileData.make
$ExifArgs += "`n-make=$($fileData.make)" # alternative code


When I use the argsfile with the stay_open command, the additional spaces are present in the metadata. If I use the same argsfile as part of an EXIFTOOL-commandline-statement, the spaces are not present in the metadata.

I can live with that issue, but I wanted to mention it just in case someone else struggles with this, too.

Peace

StarGeek

I'm not sure what your question is here.  If you want a trailing space on the command line, you have to use quotes to include it
-Keyword="one "

This applies to adding and removing, as a keyword of "one" is different than a keyword of "one "
-Keywords-="one " -Keywords+="one "
* 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

This is a PowerShell question.  I have no idea why it is writing a space at the end of the line.

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