Use save parameters as encoderinfo defaults (#9001)

This commit is contained in:
Hugo van Kemenade 2025-06-30 15:13:12 +03:00 committed by GitHub
commit c22230b761
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 37 additions and 13 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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

View File

@ -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()

View File

@ -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)