Skip to content

How to write a multipage TIFF with different ImageFileDirectories for each frame? #8775

@anntzer

Description

@anntzer

What did you do?

I would like to write a multipage tiff file with different ImageFileDirectories for each frame. I started with the example given at #3636 (comment) (#3636 being essentially the same issue).

What did you expect to happen?

The example code should print 1 2 3 4.

What actually happened?

The code prints 1 2 3 4, then crashes with

Exception ignored in: <PIL.TiffImagePlugin.AppendingTiffWriter object at 0x101bb2930>
Traceback (most recent call last):
  File "/path/to/lib/python3.13/site-packages/PIL/TiffImagePlugin.py", line 2197, in close
  File "/path/to/lib/python3.13/site-packages/PIL/TiffImagePlugin.py", line 2068, in finalize
ValueError: seek of closed file

What are your OS, Python and Pillow versions?

--------------------------------------------------------------------
Pillow 11.1.0
Python 3.13.0 | packaged by conda-forge | (main, Oct 17 2024, 12:32:35) [Clang 17.0.6 ]
--------------------------------------------------------------------
Python executable is /path/to/miniforge3/envs/default/bin/python
System Python files loaded from /path/to/miniforge3/envs/default
--------------------------------------------------------------------
Python Pillow modules loaded from /path/to/.local/lib/python3.13/site-packages/PIL
Binary Pillow modules loaded from /path/to/.local/lib/python3.13/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.1.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.5.0
--- JPEG support ok, compiled for libjpeg-turbo 3.1.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.zlib-ng, compiled for zlib-ng 2.2.2
--- LIBTIFF support ok, loaded 4.6.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------

A few notes:

  • The crash can be worked around by making the file-handle closure a no-op (fp.close = lambda: None immediately after opening the file), but that's a not-so-nice hack.
  • The example code given in the linked issue relies on the private TiffImagePlugin._save method; it would be nice if a (perhaps simpler) public API was available.
  • The example code sets a different encoderinfo on each frame, and as such actually runs afoul of the (limited...) documentation regarding encoderinfo at https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#reading-multi-frame-tiff-images: "for correct results, all the appended images should have the same encoderinfo and encoderconfig properties."

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions