From 820997e48a55a68b119f1fa9369380f917fde166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Komar=C4=8Devi=C4=87?= Date: Thu, 27 May 2021 15:40:04 +0200 Subject: [PATCH] Limit TIFF strip size when saving with libtiff --- src/PIL/TiffImagePlugin.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index d74af284fcd..58e9019283e 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1553,8 +1553,13 @@ def _save(im, fp, filename): ifd[COLORMAP] = tuple(v * 256 for v in lut) # data orientation stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8) - ifd[ROWSPERSTRIP] = im.size[1] + rows_per_strip = im.size[1] strip_byte_counts = stride * im.size[1] + # aim for 64 KB strips + while strip_byte_counts > 2 ** 16 and rows_per_strip > 1: + rows_per_strip = (rows_per_strip + 1) // 2 + strip_byte_counts = stride * rows_per_strip + ifd[ROWSPERSTRIP] = rows_per_strip if strip_byte_counts >= 2 ** 16: ifd.tagtype[STRIPBYTECOUNTS] = TiffTags.LONG ifd[STRIPBYTECOUNTS] = strip_byte_counts