Skip to content

Commit 0bb99e5

Browse files
committed
Use save parameters as encoderinfo defaults
1 parent f3b05d6 commit 0bb99e5

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

Tests/test_file_mpo.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,20 @@ def test_save_all() -> None:
312312
def test_save_xmp() -> None:
313313
im = Image.new("RGB", (1, 1))
314314
im2 = Image.new("RGB", (1, 1), "#f00")
315-
im2.encoderinfo = {"xmp": b"Second frame"}
316-
im_reloaded = roundtrip(im, xmp=b"First frame", save_all=True, append_images=[im2])
317315

318-
assert im_reloaded.info["xmp"] == b"First frame"
316+
def roundtrip_xmp():
317+
im_reloaded = roundtrip(im, xmp=b"Default", save_all=True, append_images=[im2])
318+
xmp = [im_reloaded.info["xmp"]]
319+
im_reloaded.seek(1)
320+
return xmp + [im_reloaded.info["xmp"]]
319321

320-
im_reloaded.seek(1)
321-
assert im_reloaded.info["xmp"] == b"Second frame"
322+
# Use the save parameters for all frames by default
323+
assert roundtrip_xmp() == [b"Default", b"Default"]
324+
325+
# Specify a value for the first frame
326+
im.encoderinfo = {"xmp": b"First frame"}
327+
assert roundtrip_xmp() == [b"First frame", b"Default"]
328+
329+
# Specify value for the second frame
330+
im2.encoderinfo = {"xmp": b"Second frame"}
331+
assert roundtrip_xmp() == [b"Default", b"Second frame"]

Tests/test_file_tiff.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,16 +695,21 @@ def test_rowsperstrip(self, tmp_path: Path) -> None:
695695
assert im.tag_v2[278] == 256
696696

697697
im = hopper()
698+
im.encoderinfo = {"tiffinfo": {278: 100}}
698699
im2 = Image.new("L", (128, 128))
699-
im2.encoderinfo = {"tiffinfo": {278: 256}}
700-
im.save(outfile, save_all=True, append_images=[im2])
700+
im3 = im2.copy()
701+
im3.encoderinfo = {"tiffinfo": {278: 300}}
702+
im.save(outfile, save_all=True, tiffinfo={278: 200}, append_images=[im2, im3])
701703

702704
with Image.open(outfile) as im:
703705
assert isinstance(im, TiffImagePlugin.TiffImageFile)
704-
assert im.tag_v2[278] == 128
706+
assert im.tag_v2[278] == 100
705707

706708
im.seek(1)
707-
assert im.tag_v2[278] == 256
709+
assert im.tag_v2[278] == 200
710+
711+
im.seek(2)
712+
assert im.tag_v2[278] == 300
708713

709714
def test_strip_raw(self) -> None:
710715
infile = "Tests/images/tiff_strip_raw.tif"

src/PIL/Image.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,8 @@ def save(
25552555
self.load()
25562556

25572557
save_all = params.pop("save_all", None)
2558-
self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params}
2558+
self._default_encoderinfo = params
2559+
self._attach_default_encoderinfo(self)
25592560
self.encoderconfig: tuple[Any, ...] = ()
25602561

25612562
if format.upper() not in SAVE:
@@ -2600,6 +2601,12 @@ def save(
26002601
if open_fp:
26012602
fp.close()
26022603

2604+
def _attach_default_encoderinfo(self, im: Image) -> Any:
2605+
self.encoderinfo = {
2606+
**im._default_encoderinfo,
2607+
**getattr(self, "encoderinfo", {}),
2608+
}
2609+
26032610
def seek(self, frame: int) -> None:
26042611
"""
26052612
Seeks to the given frame in this sequence file. If you seek

src/PIL/MpoImagePlugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
6464
JpegImagePlugin._save(im_frame, fp, filename)
6565
offsets.append(fp.tell())
6666
else:
67+
im_frame._attach_default_encoderinfo(im)
6768
im_frame.save(fp, "JPEG")
6869
offsets.append(fp.tell() - offsets[-1])
6970

src/PIL/TiffImagePlugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,8 +2310,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
23102310
try:
23112311
with AppendingTiffWriter(fp) as tf:
23122312
for ims in [im] + append_images:
2313-
if not hasattr(ims, "encoderinfo"):
2314-
ims.encoderinfo = {}
2313+
ims._attach_default_encoderinfo(im)
23152314
if not hasattr(ims, "encoderconfig"):
23162315
ims.encoderconfig = ()
23172316
nfr = getattr(ims, "n_frames", 1)

0 commit comments

Comments
 (0)