forked from josch/img2pdf
use TiffImagePlugin.STRIP_SIZE for Pillow >= 8.4.0
This commit is contained in:
parent
d9345ac767
commit
c553e169a4
1 changed files with 43 additions and 19 deletions
|
@ -399,6 +399,28 @@ class ExifOrientationError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# temporary change the attribute of an object using a context manager
|
||||||
|
class temp_attr:
|
||||||
|
def __init__(self, obj, field, value):
|
||||||
|
self.obj = obj
|
||||||
|
self.field = field
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.exists = False
|
||||||
|
if hasattr(self.obj, self.field):
|
||||||
|
self.exists = True
|
||||||
|
self.old_value = getattr(self.obj, self.field)
|
||||||
|
print(f"setting {self.obj}.{self.field} = {self.value}")
|
||||||
|
setattr(self.obj, self.field, self.value)
|
||||||
|
|
||||||
|
def __exit__(self, exctype, excinst, exctb):
|
||||||
|
if self.exists:
|
||||||
|
setattr(self.obj, self.field, self.old_value)
|
||||||
|
else:
|
||||||
|
delattr(self.obj, self.field)
|
||||||
|
|
||||||
|
|
||||||
# without pdfrw this function is a no-op
|
# without pdfrw this function is a no-op
|
||||||
def my_convert_load(string):
|
def my_convert_load(string):
|
||||||
return string
|
return string
|
||||||
|
@ -1428,27 +1450,29 @@ def transcode_monochrome(imgdata):
|
||||||
# into putting everything into a single strip. Thanks to Andrew Murray for
|
# into putting everything into a single strip. Thanks to Andrew Murray for
|
||||||
# the hack.
|
# the hack.
|
||||||
#
|
#
|
||||||
# This can be dropped once this gets merged:
|
# Since version 8.4.0 Pillow allows us to modify the strip size explicitly
|
||||||
# https://github.com/python-pillow/Pillow/pull/5744
|
tmp_strip_size = (imgdata.size[0] + 7) // 8 * imgdata.size[1]
|
||||||
pillow__getitem__ = TiffImagePlugin.ImageFileDirectory_v2.__getitem__
|
if hasattr(TiffImagePlugin, "STRIP_SIZE"):
|
||||||
|
# we are using Pillow 8.4.0 or later
|
||||||
|
with temp_attr(TiffImagePlugin, "STRIP_SIZE", tmp_strip_size):
|
||||||
|
im.save(newimgio, format="TIFF", compression="group4")
|
||||||
|
else:
|
||||||
|
# only needed for Pillow 8.3.x but works for versions before that as
|
||||||
|
# well
|
||||||
|
pillow__getitem__ = TiffImagePlugin.ImageFileDirectory_v2.__getitem__
|
||||||
|
|
||||||
def __getitem__(self, tag):
|
def __getitem__(self, tag):
|
||||||
overrides = {
|
overrides = {
|
||||||
TiffImagePlugin.ROWSPERSTRIP: imgdata.size[1],
|
TiffImagePlugin.ROWSPERSTRIP: imgdata.size[1],
|
||||||
TiffImagePlugin.STRIPBYTECOUNTS: [
|
TiffImagePlugin.STRIPBYTECOUNTS: [tmp_strip_size],
|
||||||
(imgdata.size[0] + 7) // 8 * imgdata.size[1]
|
TiffImagePlugin.STRIPOFFSETS: [0],
|
||||||
],
|
}
|
||||||
TiffImagePlugin.STRIPOFFSETS: [0],
|
return overrides.get(tag, pillow__getitem__(self, tag))
|
||||||
}
|
|
||||||
return overrides.get(tag, pillow__getitem__(self, tag))
|
|
||||||
|
|
||||||
# use try/finally to make sure that __getitem__ is reset even if save()
|
with temp_attr(
|
||||||
# raises an exception
|
TiffImagePlugin.ImageFileDirectory_v2, "__getitem__", __getitem__
|
||||||
try:
|
):
|
||||||
TiffImagePlugin.ImageFileDirectory_v2.__getitem__ = __getitem__
|
im.save(newimgio, format="TIFF", compression="group4")
|
||||||
im.save(newimgio, format="TIFF", compression="group4")
|
|
||||||
finally:
|
|
||||||
TiffImagePlugin.ImageFileDirectory_v2.__getitem__ = pillow__getitem__
|
|
||||||
|
|
||||||
# Open new image in memory
|
# Open new image in memory
|
||||||
newimgio.seek(0)
|
newimgio.seek(0)
|
||||||
|
|
Loading…
Reference in a new issue