Skip to content

Commit 5c93145

Browse files
committed
Allow encoderconfig and encoderinfo to be set for appended TIFF images
1 parent 3407f76 commit 5c93145

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

Tests/test_file_tiff.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,18 @@ def test_rowsperstrip(self, tmp_path: Path) -> None:
661661
assert isinstance(im, TiffImagePlugin.TiffImageFile)
662662
assert im.tag_v2[278] == 256
663663

664+
im = hopper()
665+
im2 = Image.new("L", (128, 128))
666+
im2.encoderinfo = {"tiffinfo": {278: 256}}
667+
im.save(outfile, save_all=True, append_images=[im2])
668+
669+
with Image.open(outfile) as im:
670+
assert isinstance(im, TiffImagePlugin.TiffImageFile)
671+
assert im.tag_v2[278] == 128
672+
673+
im.seek(1)
674+
assert im.tag_v2[278] == 256
675+
664676
def test_strip_raw(self) -> None:
665677
infile = "Tests/images/tiff_strip_raw.tif"
666678
with Image.open(infile) as im:

docs/handbook/image-file-formats.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,9 +1162,7 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum
11621162

11631163
**append_images**
11641164
A list of images to append as additional frames. Each of the
1165-
images in the list can be single or multiframe images. Note however, that for
1166-
correct results, all the appended images should have the same
1167-
``encoderinfo`` and ``encoderconfig`` properties.
1165+
images in the list can be single or multiframe images.
11681166

11691167
.. versionadded:: 4.2.0
11701168

src/PIL/TiffImagePlugin.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,22 +2295,19 @@ def fixOffsets(
22952295

22962296

22972297
def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
2298-
encoderinfo = im.encoderinfo.copy()
2299-
encoderconfig = im.encoderconfig
2300-
append_images = list(encoderinfo.get("append_images", []))
2298+
append_images = list(im.encoderinfo.get("append_images", []))
23012299
if not hasattr(im, "n_frames") and not append_images:
23022300
return _save(im, fp, filename)
23032301

23042302
cur_idx = im.tell()
23052303
try:
23062304
with AppendingTiffWriter(fp) as tf:
23072305
for ims in [im] + append_images:
2308-
ims.encoderinfo = encoderinfo
2309-
ims.encoderconfig = encoderconfig
2310-
if not hasattr(ims, "n_frames"):
2311-
nfr = 1
2312-
else:
2313-
nfr = ims.n_frames
2306+
if not hasattr(ims, "encoderinfo"):
2307+
ims.encoderinfo = {}
2308+
if not hasattr(ims, "encoderconfig"):
2309+
ims.encoderconfig = ()
2310+
nfr = getattr(ims, "n_frames", 1)
23142311

23152312
for idx in range(nfr):
23162313
ims.seek(idx)

0 commit comments

Comments
 (0)