Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write iptc metadata changes to image binaries #2731

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions superdesk/media/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import logging

from typing import BinaryIO, Dict, List, Literal, Mapping, TypedDict, Union

from pyexiv2 import convert_xmp_to_iptc
from superdesk.text_utils import decode
from PIL import Image, ExifTags
from PIL import IptcImagePlugin
Expand Down Expand Up @@ -243,23 +245,28 @@ def write_metadata(input: bytes, metadata: PhotoMetadata) -> bytes:
@param metadata: dict
"""
xmp = {
"Xmp.dc.description": metadata.get("Description", ""),
"Xmp.photoshop.CaptionWriter": metadata.get("DescriptionWriter", ""),
"Xmp.photoshop.Headline": metadata.get("Headline", ""),
"Xmp.photoshop.Instructions": metadata.get("Instructions", ""),
"Xmp.photoshop.TransmissionReference": metadata.get("JobId", ""),
"Xmp.dc.title": metadata.get("Title", ""),
"Xmp.dc.creator": metadata.get("Creator", []),
"Xmp.photoshop.AuthorsPosition": metadata.get("CreatorsJobtitle", ""),
"Xmp.dc.rights": metadata.get("CopyrightNotice", ""),
"Xmp.photoshop.City": metadata.get("City", ""),
"Xmp.photoshop.Country": metadata.get("Country", ""),
"Xmp.iptc.CountryCode": metadata.get("CountryCode", ""),
"Xmp.photoshop.Credit": metadata.get("CreditLine", ""),
"Xmp.photoshop.State": metadata.get("ProvinceState", ""),
"Xmp.dc.description": metadata.get("Description"),
"Xmp.photoshop.CaptionWriter": metadata.get("DescriptionWriter"),
"Xmp.photoshop.Headline": metadata.get("Headline"),
"Xmp.photoshop.Instructions": metadata.get("Instructions"),
"Xmp.photoshop.TransmissionReference": metadata.get("JobId"),
"Xmp.dc.title": metadata.get("Title"),
"Xmp.dc.creator": metadata.get("Creator"),
"Xmp.photoshop.AuthorsPosition": metadata.get("CreatorsJobtitle"),
"Xmp.dc.rights": metadata.get("CopyrightNotice"),
"Xmp.photoshop.City": metadata.get("City"),
"Xmp.photoshop.Country": metadata.get("Country"),
"Xmp.iptc.CountryCode": metadata.get("CountryCode"),
"Xmp.photoshop.Credit": metadata.get("CreditLine"),
"Xmp.photoshop.State": metadata.get("ProvinceState"),
}

xmp = {k: v for k, v in xmp.items() if v}
iptc = convert_xmp_to_iptc(xmp)

with pyexiv2.ImageData(input) as img:
img.modify_xmp(xmp)
img.modify_iptc(iptc)
return img.get_bytes()


Expand Down
9 changes: 9 additions & 0 deletions tests/media/image_metadata_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import io

from pytest import fixture
from PIL import Image
from PIL.IptcImagePlugin import getiptcinfo
from superdesk.media.image import (
PhotoMetadata,
PhotoMetadataMapping,
Expand Down Expand Up @@ -59,6 +63,11 @@ def test_picture_metadata_read_write(image_binary) -> None:
metadata = read_metadata(next_image)
assert metadata == updated

image = Image.open(io.BytesIO(next_image))
iptc_info = getiptcinfo(image)
assert iptc_info is not None
assert iptc_info[(2, 120)].decode() == "description"


def test_get_metadata_from_item() -> None:
item = Item(
Expand Down
2 changes: 1 addition & 1 deletion tests/timer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def test_timer(self, logger):
with timer("foo"):
time.sleep(0.1)
logger.info.assert_called_once_with("%s: %.3fms", "foo", ANY)
self.assertEqual(100, int(logger.info.call_args[0][2]))
self.assertAlmostEqual(100, int(logger.info.call_args[0][2]), delta=50) # 50ms tolerance
Loading