I have the following script that takes a word from the filename and adds it as a keyword to the image metadata. It works, but there are two issues:
- For each image a new JPG_ORIGINAL file is created. Why does this happen and how do I prevent this from happening?
- The new keyword is added at the back of all of the existing keywords, even though I insert it as the first item in my array. The order of keywords is important for my use case and I want it at the start.
import os
import iptcinfo3
import subprocess
folder_path = r"C:\Users\Maryse\Documents\testimg"
exiftool_path = r"C:\Program Files\ExifTool\ExifTool.exe"
for filename in os.listdir(folder_path):
image_path = os.path.join(folder_path, filename)
info = iptcinfo3.IPTCInfo(image_path)
print("info: ", info)
keywords = info['keywords']
# Convert the byte strings to regular strings
keywords = [keyword.decode('utf-8') for keyword in keywords]
parts = image_path.split("_")
first = parts[2].capitalize()
keywords.insert(0, first)
print("keywords: ", keywords)
subprocess.run([exiftool_path, '-XPKeywords=' + ', '.join(keywords), image_path])
Quote from: schaakspeler on April 05, 2023, 10:49:23 AMFor each image a new JPG_ORIGINAL file is created. Why does this happen and how do I prevent this from happening?
From the Docs (https://exiftool.org/exiftool_pod.html#DESCRIPTION):
By default the original files are preserved with _original appended to their names -- be sure to verify that the new files are OK before erasing the originals.
You can suppress the creation of backup files by adding the
-overwrite_original option (https://exiftool.org/exiftool_pod.html#overwrite_original).
QuoteThe new keyword is added at the back of all of the existing keywords, even though I insert it as the first item in my array.
Are you using exiftool to check this or something else? Use the command in FAQ #3 (https://exiftool.org/faq.html#Q3) to double check and see how the data is actually written.
I suspect that you are using Windows->Properties to view the data. If so, what is happening is that Windows will combine three separate tags to list under the "Tags" property. Those are
IPTC:Keywords,
XMP:Subject, and
XPKeywords. Odds are, one or both of the first two have the data as well, and you're only updating
XPKeywords.
The
XPKeywords tag is supposed to be a semicolon separated string, but it looks like Windows will also accept a comma separated one. TIL. But Windows will rewrite it as a semicolon separated string if rewritten through the Properties window.
One additional thing to take note of is that the
XPKeywords is different from the
IPTC:Keywords and
XMP:Subject tags. Those are fully separated list type tags and not comma separated strings. Each entry is stored completely separate from the others and you can't simply write
-Subject="one, two, three". That writes a single entry with a value of "one, two, three", not three separate entries.
See FAQ #17 (https://exiftool.org/faq.html#Q17) for more details on list type tags.