How to Correctly Get and Update Title, Comments, and Tags in JPEG Metadata Using Python?

149 Views Asked by At

I'm working on a Python script that processes a batch of JPEG images, updating their metadata to include new titles, comments, and tags. My goal is to read the metadata from a MySQL database and apply it to each image's IPTC data fields. I am using the iptcinfo3 library to handle IPTC metadata and the PIL (Pillow) library for general image processing tasks.

Despite my efforts, I've encountered issues with the iptcinfo3 library, specifically when updating the 'object name' (title), 'caption/abstract' (comments), and 'keywords' (tags). Here are the main points I need assistance with:

Correctly updating the IPTC fields for title, comments, and tags. I can retrieve the information from my database without issues, but I'm unsure if I'm updating the IPTC fields correctly. Here's a snippet of my current approach:

from PIL import Image
import iptcinfo3

def update_image_metadata(jpeg_path, title, keywords):
    info = iptcinfo3.IPTCInfo(jpeg_path, force=True)
    info['object name'] = "this is title"
    info['caption/abstract'] = f"this is comments {title} vector image"
    info['keywords'] = "this is tags"
    info.save_as(jpeg_path)

but neither getting nor updating works. This is the latest situation. Please help. thanks

enter image description here

1

There are 1 best solutions below

5
HOBE On

To manage and update title, comments, and tags in JPEG metadata, I recommend using the exiftool utility, which is a powerful tool for reading, writing, and editing meta information in a wide variety of files. While exiftool itself is a command-line utility, you can easily use it from Python by utilizing the subprocess module to execute exiftool commands. This approach provides a robust solution for dealing with metadata in images.

First, ensure that exiftool is installed on your system. You can download it from https://exiftool.org/ and follow the installation instructions for your operating system.

Here's a Python script that demonstrates how to update the title, comments, and tags in a JPEG image using exiftool:

import subprocess

def update_image_metadata(jpeg_path, title, comments, keywords):
    # Command to update the title, comments, and keywords
    cmd = [
        'exiftool',
        '-overwrite_original',  # Overwrites the original file with the updated metadata
        f'-Title={title}',
        f'-XMP:Description={comments}', # "XPComment" in Microsoft Windows
        f'-Keywords={keywords}',
        jpeg_path
    ]

    # Execute the command
    subprocess.run(cmd, check=True)

    print(f"Metadata updated for {jpeg_path}")

# Example usage
jpeg_path = 'path/to/your/image.jpg'
title = "Your Image Title"
comments = "This is a comment about the image."
keywords = "keyword1, keyword2, keyword3"
update_image_metadata(jpeg_path, title, comments, keywords)

This script constructs and executes an exiftool command that updates the specified metadata fields in the JPEG file. Be sure to replace 'path/to/your/image.jpg', 'Your Image Title', 'This is a comment about the image.', and 'keyword1, keyword2, keyword3' with your actual file path and metadata values.

Please note, this script requires that exiftool is correctly installed on your system and accessible from your Python script's environment.

  • Rewrite the Python example that accepts a comment, thus executes the exiftool while maintaining process open.

There is a Python wrapper for exiftool (PyExiftool) that runs exiftool in the background using its -stay_open option. This is preferable to running exiftool once per file, as exiftool's biggest performance hit is the startup time (see Exiftool Common Mistake #3). When processing a large number of files, using PyExiftool will greatly improve performance. – StarGeek

import exiftool # python -m pip install -U pyexiftool

files = ['your_image.jpg', 'your_image.png']
title = "My Image Title"
comments = "This is a comment"
keywords = "example, sample, demo"

with exiftool.ExifToolHelper() as et:
    # Execute the exiftool
    et.execute(
        f'-Title={title}',
        f'-XPComment={comments}',
        f'-Keywords={keywords}',
        *files
    )
    # Check the metadata
    metadata = et.get_metadata(files)
    for file, data in zip(files, metadata):
        print(f"{file}:{data}")
  • Using pyexiftool is 7.8 times faster than using subprocess for 10 images, 38.3 times faster for 100 images, and 62.1 times faster for 1000 images on my m1 macbook pro.