diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 3e887d36b..9262e6ca7 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -317,13 +317,24 @@ def test_save_all() -> None: def test_save_xmp() -> None: im = Image.new("RGB", (1, 1)) im2 = Image.new("RGB", (1, 1), "#f00") + + def roundtrip_xmp() -> list[Any]: + im_reloaded = roundtrip(im, xmp=b"Default", save_all=True, append_images=[im2]) + xmp = [im_reloaded.info["xmp"]] + im_reloaded.seek(1) + return xmp + [im_reloaded.info["xmp"]] + + # Use the save parameters for all frames by default + assert roundtrip_xmp() == [b"Default", b"Default"] + + # Specify a value for the first frame + im.encoderinfo = {"xmp": b"First frame"} + assert roundtrip_xmp() == [b"First frame", b"Default"] + del im.encoderinfo + + # Specify value for the second frame im2.encoderinfo = {"xmp": b"Second frame"} - im_reloaded = roundtrip(im, xmp=b"First frame", save_all=True, append_images=[im2]) + assert roundtrip_xmp() == [b"Default", b"Second frame"] # Test that encoderinfo is unchanged assert im2.encoderinfo == {"xmp": b"Second frame"} - - assert im_reloaded.info["xmp"] == b"First frame" - - im_reloaded.seek(1) - assert im_reloaded.info["xmp"] == b"Second frame" diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 046a9f1f1..bd364377b 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -681,16 +681,21 @@ class TestFileTiff: assert im.tag_v2[278] == 256 im = hopper() + im.encoderinfo = {"tiffinfo": {278: 100}} im2 = Image.new("L", (128, 128)) - im2.encoderinfo = {"tiffinfo": {278: 256}} - im.save(outfile, save_all=True, append_images=[im2]) + im3 = im2.copy() + im3.encoderinfo = {"tiffinfo": {278: 300}} + im.save(outfile, save_all=True, tiffinfo={278: 200}, append_images=[im2, im3]) with Image.open(outfile) as im: assert isinstance(im, TiffImagePlugin.TiffImageFile) - assert im.tag_v2[278] == 128 + assert im.tag_v2[278] == 100 im.seek(1) - assert im.tag_v2[278] == 256 + assert im.tag_v2[278] == 200 + + im.seek(2) + assert im.tag_v2[278] == 300 def test_strip_raw(self) -> None: infile = "Tests/images/tiff_strip_raw.tif" diff --git a/src/PIL/Image.py b/src/PIL/Image.py index b17441aa7..9fc1c7067 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2556,8 +2556,9 @@ class Image: self.load() save_all = params.pop("save_all", None) + self._default_encoderinfo = params encoderinfo = getattr(self, "encoderinfo", {}) - self.encoderinfo = {**encoderinfo, **params} + self._attach_default_encoderinfo(self) self.encoderconfig: tuple[Any, ...] = () if format.upper() not in SAVE: @@ -2599,6 +2600,11 @@ class Image: if open_fp: fp.close() + def _attach_default_encoderinfo(self, im: Image) -> dict[str, Any]: + encoderinfo = getattr(self, "encoderinfo", {}) + self.encoderinfo = {**im._default_encoderinfo, **encoderinfo} + return encoderinfo + def seek(self, frame: int) -> None: """ Seeks to the given frame in this sequence file. If you seek diff --git a/src/PIL/MpoImagePlugin.py b/src/PIL/MpoImagePlugin.py index 784b6f208..b1ae07873 100644 --- a/src/PIL/MpoImagePlugin.py +++ b/src/PIL/MpoImagePlugin.py @@ -69,7 +69,9 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: JpegImagePlugin._save(im_frame, fp, filename) offsets.append(fp.tell()) else: + encoderinfo = im_frame._attach_default_encoderinfo(im) im_frame.save(fp, "JPEG") + im_frame.encoderinfo = encoderinfo offsets.append(fp.tell() - offsets[-1]) ifd = TiffImagePlugin.ImageFileDirectory_v2() diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 146b01e5f..daf20f2e8 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -2311,8 +2311,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: try: with AppendingTiffWriter(fp) as tf: for ims in [im] + append_images: - if not hasattr(ims, "encoderinfo"): - ims.encoderinfo = {} + encoderinfo = ims._attach_default_encoderinfo(im) if not hasattr(ims, "encoderconfig"): ims.encoderconfig = () nfr = getattr(ims, "n_frames", 1) @@ -2322,6 +2321,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: ims.load() _save(ims, tf, filename) tf.newFrame() + ims.encoderinfo = encoderinfo finally: im.seek(cur_idx)