You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python为存储于Synology NAS的图片添加标签(兼容Synology Photos与Digikam标签共享)

Adding Tags to Images with Python for Synology Photos & Digikam

Awesome that you've got your photo workflow sorted out with Synology NAS, your food/non-food classifier, and tag sync between Synology Photos and Digikam! Since your two tools already share tags, we just need to write tags to standard image metadata fields that both apps recognize. Here are the most reliable methods:

IPTC Keywords is the gold standard for cross-app tag compatibility—both Synology Photos and Digikam natively read and sync these tags. pyexiv2 is a robust Python library built for handling image metadata.

Steps:

  1. Install the library:
pip install pyexiv2
  1. Write code to add tags
    First, make sure your Python environment can access your NAS files (e.g., mount the Synology shared folder as a local drive on Windows, or use a mount point on Linux/macOS).

Example code for single/multiple tags:

import pyexiv2

# Replace with your actual image path on the NAS
image_path = "/mnt/synology_photos/family_meal.jpg"

# Add a single tag (with deduplication)
with pyexiv2.Image(image_path) as img:
    existing_tags = img.read_iptc().get("Iptc.Application2.Keywords", [])
    new_tag = "Food"
    if new_tag not in existing_tags:
        existing_tags.append(new_tag)
        img.modify_iptc({"Iptc.Application2.Keywords": existing_tags})
        print(f"Successfully added tag: {new_tag}")

# Add multiple tags at once
with pyexiv2.Image(image_path) as img:
    existing_tags = img.read_iptc().get("Iptc.Application2.Keywords", [])
    new_tags = ["Food", "Family Gathering", "2024"]
    # Merge and deduplicate tags
    updated_tags = list(set(existing_tags + new_tags))
    img.modify_iptc({"Iptc.Application2.Keywords": updated_tags})
    print(f"Updated tags: {updated_tags}")

Method 2: Use Pillow + piexif for EXIF Tags

If you're already using Pillow for image processing, you can pair it with piexif to write tags to EXIF metadata. Note that EXIF is slightly less universal for tags than IPTC, but both apps still support it.

Steps:

  1. Install the required libraries:
pip install pillow piexif
  1. Example code:
from PIL import Image
import piexif

image_path = "/mnt/synology_photos/family_meal.jpg"

# Load the image and existing EXIF data
img = Image.open(image_path)
exif_data = piexif.load(img.info.get("exif", b""))

# Add tags to the EXIF UserComment field (comma-separated)
new_tags = ["Food", "Non-Food"]
tag_string = ", ".join(new_tags)
exif_data["Exif"][piexif.ExifIFD.UserComment] = tag_string.encode("utf-8")

# Save the updated EXIF data back to the image
exif_bytes = piexif.dump(exif_data)
img.save(image_path, exif=exif_bytes)
print(f"Added tags via EXIF: {new_tags}")

Important Notes:

  • NAS File Access: Ensure your Python script has read/write permissions for the NAS shared folder. Double-check mount settings if you're having trouble accessing files.
  • Refresh Tags in Apps: After adding tags, trigger a library scan in Synology Photos (Settings → Libraries → Scan Library) and refresh the folder in Digikam (right-click folder → Refresh) so both apps pick up the new tags.
  • Deduplication: The code examples include deduplication logic to avoid duplicate tags, which keeps your tag list clean and prevents sync issues.

内容的提问来源于stack exchange,提问作者CAD.Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 15:34:10