'Split multi-page tiff image using python
I have a function that enables me to split the multi-page tiff
from PIL import Image, ImageSequence
im = Image.open("Sample.tiff")
for i, page in enumerate(ImageSequence.Iterator(im)):
page.save("Page%d.png" % i)
The code is working well for some tiff images but not for all of them. Here's a sample of the tiff that doesn't work with code It raises an error like that
Traceback (most recent call last):
File "C:\Users\Future\Desktop\demo.py", line 6, in <module>
page.save("Page%d.png" % i)
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 2130, in save
self._ensure_mutable()
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 619, in _ensure_mutable
self._copy()
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 612, in _copy
self.load()
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\TiffImagePlugin.py", line 1088, in load
return self._load_libtiff()
File "C:\Users\Future\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\TiffImagePlugin.py", line 1192, in _load_libtiff
raise OSError(err)
OSError: -9
tiff sample: https://www.mediafire.com/view/9l3iyke0s7t2col/Sample.tiff/file
Solution 1:[1]
You don't say which one of your two files works and which one doesn't! I checked both files with:
tiffinfo IMAGE1.TIF > 1.txt
tiffinfo IMAGE2.TIF > 2.txt
then I diffed the two text files with:
opendiff [12].txt
The main difference is the compression - both of which are potentially problematic for some software. One is CCITT Group4 and the other is JPEG-compressed.
If you don't have tiffinfo (part of the TIFF library package), you can alternatively use:
exiftool IMAGE.TIF
or ImageMagick:
magick identify -verbose IMAGE.TIF
I suggest you check your PIL installation with the following as you need lib tiff installed for handling compressed images:
python -m PIL
--------------------------------------------------------------------
Pillow 9.0.1
Python 3.10.0 (v3.10.0:b494f5935c, Oct 4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)]
--------------------------------------------------------------------
Python modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
Binary modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.0.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.11.1
--- LITTLECMS2 support ok, loaded 2.13
--- WEBP support ok, loaded 1.2.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.2
--- OPENJPEG (JPEG2000) support ok, loaded 2.4.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.11
--- LIBTIFF support ok, loaded 4.2.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open, save
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP image/webp
Extensions: .webp
Features: open, save, save_all
--------------------------------------------------------------------
WMF
Extensions: .emf, .wmf
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------
If you can't get PIL working, you might consider Christoph's tifffile.
Note that exiftool can tell you the compression like this:
exiftool -s -s -s -compression Sample1.tiff
T6/Group 4 Fax
Or, same with Python:
import exiftool
# If "exiftool" is not on your PATH, add the full path inside parentheses on next line
with exiftool.ExifTool() as et:
c = et.execute(b"-compression", b"Sample1.tiff")
You can also get the TIFF compression type with the Python magic module:
import magic
print(magic.from_file('a.tif'))
Sample Output
'TIFF image data, big-endian, direntries=16, height=20, bps=0, compression=LZW, PhotometricIntepretation=RGB, orientation=upper-left, width=20'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

