diff --git a/doc/functions.rst b/doc/functions.rst index 5c14605..25db0bf 100644 --- a/doc/functions.rst +++ b/doc/functions.rst @@ -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 :: diff --git a/piexif/_common.py b/piexif/_common.py index 6069c60..165ab85 100644 --- a/piexif/_common.py +++ b/piexif/_common.py @@ -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 """ @@ -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 \ @@ -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) diff --git a/piexif/_transplant.py b/piexif/_transplant.py index 0abf7f1..276ac1c 100644 --- a/piexif/_transplant.py +++ b/piexif/_transplant.py @@ -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) @@ -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 @@ -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) diff --git a/tests/images/01.jpg b/tests/images/01.jpg index 2b9ae90..c13dc39 100644 Binary files a/tests/images/01.jpg and b/tests/images/01.jpg differ diff --git a/tests/s_test.py b/tests/s_test.py index 5d105de..fac801b 100644 --- a/tests/s_test.py +++ b/tests/s_test.py @@ -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() + 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) + ) + self.assertNotEqual( + _common.get_iptc_seg(copy_segments), + None + ) + os.remove("transplant.jpg") + def test_transplant_m(self): """'transplant' on memory. """