1
0
Fork 0
forked from josch/img2pdf

move closing code into its own function

This commit is contained in:
Johannes Schauer Marin Rodrigues 2021-04-14 08:25:18 +02:00
parent c97ce34023
commit 02c85a50ad
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -1368,6 +1368,16 @@ def read_images(rawdata, colorspace, first_frame_only=False):
if imgformat is None: if imgformat is None:
imgformat = ImageFormat.other imgformat = ImageFormat.other
def cleanup():
if imgdata is not None:
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have the
# close() method
try:
imgdata.close()
except AttributeError:
pass
im.close()
logger.debug("imgformat = %s", imgformat.name) logger.debug("imgformat = %s", imgformat.name)
# depending on the input format, determine whether to pass the raw # depending on the input format, determine whether to pass the raw
@ -1384,8 +1394,8 @@ def read_images(rawdata, colorspace, first_frame_only=False):
raise JpegColorspaceError("jpeg can't have a color palette") raise JpegColorspaceError("jpeg can't have a color palette")
if color == Colorspace["RGBA"]: if color == Colorspace["RGBA"]:
raise JpegColorspaceError("jpeg can't have an alpha channel") raise JpegColorspaceError("jpeg can't have an alpha channel")
im.close()
logger.debug("read_images() embeds a JPEG") logger.debug("read_images() embeds a JPEG")
cleanup()
return [ return [
( (
color, color,
@ -1453,8 +1463,7 @@ def read_images(rawdata, colorspace, first_frame_only=False):
) )
) )
img_page_count += 1 img_page_count += 1
imgdata.close() cleanup()
im.close()
return result return result
# We can directly embed the IDAT chunk of PNG images if the PNG is not # We can directly embed the IDAT chunk of PNG images if the PNG is not
@ -1469,7 +1478,6 @@ def read_images(rawdata, colorspace, first_frame_only=False):
imgdata, imgformat, default_dpi, colorspace, rawdata imgdata, imgformat, default_dpi, colorspace, rawdata
) )
pngidat, palette = parse_png(rawdata) pngidat, palette = parse_png(rawdata)
im.close()
# PIL does not provide the information about the original bits per # PIL does not provide the information about the original bits per
# sample. Thus, we retrieve that info manually by looking at byte 9 in # sample. Thus, we retrieve that info manually by looking at byte 9 in
# the IHDR chunk. We know where to find that in the file because the # the IHDR chunk. We know where to find that in the file because the
@ -1478,6 +1486,7 @@ def read_images(rawdata, colorspace, first_frame_only=False):
if depth not in [1, 2, 4, 8, 16]: if depth not in [1, 2, 4, 8, 16]:
raise ValueError("invalid bit depth: %d" % depth) raise ValueError("invalid bit depth: %d" % depth)
logger.debug("read_images() embeds a PNG") logger.debug("read_images() embeds a PNG")
cleanup()
return [ return [
( (
color, color,
@ -1684,13 +1693,7 @@ def read_images(rawdata, colorspace, first_frame_only=False):
) )
) )
img_page_count += 1 img_page_count += 1
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have the cleanup()
# close() method
try:
imgdata.close()
except AttributeError:
pass
im.close()
return result return result