Skip to content

Commit

Permalink
Allow encoderconfig and encoderinfo to be set for appended TIFF images
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 28, 2025
1 parent 3407f76 commit bda9ac6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,18 @@ def test_rowsperstrip(self, tmp_path: Path) -> None:
assert isinstance(im, TiffImagePlugin.TiffImageFile)
assert im.tag_v2[278] == 256

im = hopper()
im2 = Image.new("L", (128, 128))
im2.encoderinfo = {"tiffinfo": {278: 256}}
im.save(outfile, save_all=True, append_images=[im2])

with Image.open(outfile) as im:
assert isinstance(im, TiffImagePlugin.TiffImageFile)
assert im.tag_v2[278] == 128

im.seek(1)
assert im.tag_v2[278] == 256

def test_strip_raw(self) -> None:
infile = "Tests/images/tiff_strip_raw.tif"
with Image.open(infile) as im:
Expand Down
4 changes: 1 addition & 3 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1162,9 +1162,7 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum

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

.. versionadded:: 4.2.0

Expand Down
11 changes: 5 additions & 6 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2305,12 +2305,11 @@ 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:
ims.encoderinfo = encoderinfo
ims.encoderconfig = encoderconfig
if not hasattr(ims, "n_frames"):
nfr = 1
else:
nfr = ims.n_frames
if not hasattr(ims, "encoderinfo"):
ims.encoderinfo = {}
if not hasattr(ims, "encoderconfig"):
ims.encoderconfig = ()
nfr = getattr(ims, "n_frames", 1)

for idx in range(nfr):
ims.seek(idx)
Expand Down

0 comments on commit bda9ac6

Please sign in to comment.