From b758b54ce80e6a3d44db5a176040105debd32819 Mon Sep 17 00:00:00 2001 From: Johannes 'josch' Schauer Date: Mon, 20 Aug 2018 10:25:20 +0200 Subject: [PATCH] src/img2pdf.py: Disallow TIFF input with more than 8 bits per sample because PIL will silently convert it to 8 bits per sample --- src/img2pdf.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/img2pdf.py b/src/img2pdf.py index 31ba08d..001d0be 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -898,6 +898,17 @@ def read_images(rawdata, colorspace, first_frame_only=False): if first_frame_only and img_page_count > 0: break + # PIL is unable to preserve the data of 16-bit RGB TIFF files and will + # convert it to 8-bit without the possibility to retrieve the original + # data + # https://github.com/python-pillow/Pillow/issues/1888 + # + # Some tiff images do not have BITSPERSAMPLE set. Use this to create + # such a tiff: tiffset -u 258 test.tif + if imgformat == ImageFormat.TIFF \ + and max(imgdata.tag_v2.get(TiffImagePlugin.BITSPERSAMPLE, [1])) > 8: + raise ValueError("PIL is unable to preserve more than 8 bits per sample") + # We can directly copy the data out of a CCITT Group 4 encoded TIFF, if it # only contains a single strip if imgformat == ImageFormat.TIFF \