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

Add option to copy iptc data when transplanting exif information #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion doc/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ remove

transplant
----------
.. py:function:: piexif.transplant(filename1, filename2)
.. py:function:: piexif.transplant(filename1, filename2, include_iptc=False)

Copies exif data from filename1 to filename2.

:param str filename1: JPEG
:param str filename2: JPEG
:param bool include_iptc: copy iptc data

::

Expand Down
19 changes: 18 additions & 1 deletion piexif/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def read_exif_from_file(filename):
f.close()
return exif


def get_exif_seg(segments):
"""Returns Exif from JPEG meta data list
"""
Expand All @@ -66,7 +67,16 @@ def get_exif_seg(segments):
return None


def merge_segments(segments, exif=b""):
def get_iptc_seg(segments):
"""Returns iptc from JPEG meta data list
"""
for seg in segments:
if seg[0:2] == b"\xff\xed":
return seg
return None


def merge_segments(segments, exif=b"", iptc=b""):
"""Merges Exif with APP0 and APP1 manipulations.
"""
if segments[1][0:2] == b"\xff\xe0" and \
Expand All @@ -91,4 +101,11 @@ def merge_segments(segments, exif=b""):
else:
if exif:
segments.insert(1, exif)

if iptc:
if segments[4][0:2] == b"\xff\xed":
segments[4] = iptc
else:
segments.insert(4, iptc)

return b"".join(segments)
6 changes: 4 additions & 2 deletions piexif/_transplant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ._common import *


def transplant(exif_src, image, new_file=None):
def transplant(exif_src, image, new_file=None, include_iptc=False):
"""
py:function:: piexif.transplant(filename1, filename2)

Expand All @@ -22,6 +22,8 @@ def transplant(exif_src, image, new_file=None):
if exif is None:
raise ValueError("not found exif in input")

iptc = get_iptc_seg(segments) if include_iptc else b""

output_file = False
if image[0:2] == b"\xff\xd8":
image_data = image
Expand All @@ -30,7 +32,7 @@ def transplant(exif_src, image, new_file=None):
image_data = f.read()
output_file = True
segments = split_into_segments(image_data)
new_data = merge_segments(segments, exif)
new_data = merge_segments(segments, exif, iptc)

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
Expand Down
Binary file modified tests/images/01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions tests/s_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,25 @@ def test_transplant(self):
piexif.load("transplant.jpg"))
os.remove("transplant.jpg")

def test_transplant_with_iptc_data(self):
piexif.transplant(INPUT_FILE1, INPUT_FILE_PEN, "transplant.jpg", include_iptc=True)
i = Image.open("transplant.jpg")
i.close()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you still need to open and close it?

with open(INPUT_FILE1, 'rb') as f:
original_segments = _common.split_into_segments(f.read())
with open("transplant.jpg", 'rb') as f:
copy_segments = _common.split_into_segments(f.read())

self.assertEqual(
_common.get_iptc_seg(original_segments),
_common.get_iptc_seg(copy_segments)
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could make sure that these are not equal before calling transplant

self.assertNotEqual(
_common.get_iptc_seg(copy_segments),
None
)
os.remove("transplant.jpg")

def test_transplant_m(self):
"""'transplant' on memory.
"""
Expand Down