use context manager instead of manually closing BytesIO and PIL.Image

This commit is contained in:
Johannes Schauer Marin Rodrigues 2021-04-28 15:37:44 +02:00
parent 02c85a50ad
commit 2eabebb513
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -1428,18 +1428,23 @@ def read_images(rawdata, colorspace, first_frame_only=False):
): ):
if first_frame_only and img_page_count > 0: if first_frame_only and img_page_count > 0:
break break
rawframe = BytesIO(rawdata[offset : offset + mpent["Size"]]) with BytesIO(rawdata[offset : offset + mpent["Size"]]) as rawframe:
imframe = Image.open(rawframe) with Image.open(rawframe) as imframe:
# The first frame contains the data that makes the JPEG a MPO # The first frame contains the data that makes the JPEG a MPO
# Could we thus embed an MPO into another MPO? Lets not support # Could we thus embed an MPO into another MPO? Lets not support
# such madness ;) # such madness ;)
if img_page_count > 0 and imframe.format != "JPEG": if img_page_count > 0 and imframe.format != "JPEG":
raise Exception("MPO payload must be a JPEG %s", imframe.format) raise Exception("MPO payload must be a JPEG %s", imframe.format)
color, ndpi, imgwidthpx, imgheightpx, rotation, iccp = get_imgmetadata( (
imframe, ImageFormat.JPEG, default_dpi, colorspace color,
) ndpi,
imframe.close() imgwidthpx,
rawframe.close() imgheightpx,
rotation,
iccp,
) = get_imgmetadata(
imframe, ImageFormat.JPEG, default_dpi, colorspace
)
if color == Colorspace["1"]: if color == Colorspace["1"]:
raise JpegColorspaceError("jpeg can't be monochrome") raise JpegColorspaceError("jpeg can't be monochrome")
if color == Colorspace["P"]: if color == Colorspace["P"]: