1
0
Fork 0
forked from josch/img2pdf

Allow multipage CCITT group 4 TIFF images

This commit is contained in:
Johannes 'josch' Schauer 2018-08-16 00:23:18 +02:00
parent a628ed22f9
commit ba5a9a1dfc
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -861,6 +861,30 @@ def read_images(rawdata, colorspace, first_frame_only=False):
return [(color, ndpi, imgformat, pngidat, imgwidthpx, imgheightpx,
palette, False)]
# If our input is not JPEG or PNG, then we might have a format that
# supports multiple frames (like TIFF or GIF), so we need a loop to
# iterate through all frames of the image.
#
# Each frame gets compressed using PNG compression *except* if:
#
# * The image is monochrome => encode using CCITT group 4
#
# * The image is CMYK => zip plain RGB data
#
# * We are handling a CCITT encoded TIFF frame => embed data
result = []
img_page_count = 0
# loop through all frames of the image (example: multipage TIFF)
while True:
try:
imgdata.seek(img_page_count)
except EOFError:
break
if first_frame_only and img_page_count > 0:
break
# 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 \
@ -878,7 +902,6 @@ def read_images(rawdata, colorspace, first_frame_only=False):
offset, length = ccitt_payload_location_from_pil(imgdata)
im.seek(offset)
rawdata = im.read(length)
im.close()
fillorder = imgdata.tag_v2.get(TiffImagePlugin.FILLORDER)
if fillorder is None:
# no FillOrder: nothing to do
@ -896,22 +919,10 @@ def read_images(rawdata, colorspace, first_frame_only=False):
else:
raise ValueError("unsupported FillOrder: %d" % fillorder)
logging.debug("read_images() embeds a TIFF")
return [(color, ndpi, ImageFormat.CCITTGroup4, rawdata, imgwidthpx,
imgheightpx, [], inverted)]
# Everything else has to be encoded
result = []
img_page_count = 0
# loop through all frames of the image (example: multipage TIFF)
while True:
try:
imgdata.seek(img_page_count)
except EOFError:
break
if first_frame_only and img_page_count > 0:
break
result.append((color, ndpi, ImageFormat.CCITTGroup4, rawdata,
imgwidthpx, imgheightpx, [], inverted))
img_page_count += 1
continue
logging.debug("Converting frame: %d" % img_page_count)