From c1585856188f5b1c4b1131b6b2304eadb798fbb8 Mon Sep 17 00:00:00 2001 From: Johannes 'josch' Schauer Date: Wed, 17 Feb 2016 20:31:46 +0100 Subject: [PATCH] only use jp2 to parse jpeg2000 if PIL doesn't support jpeg2000 --- src/img2pdf.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index 30d3fe3..5775e97 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -542,11 +542,17 @@ class pdfdoc(object): def get_imgmetadata(imgdata, imgformat, default_dpi, colorspace, rawdata=None): - if imgformat == ImageFormat.JPEG2000 and rawdata is not None: - imgwidthpx, imgheightpx, ics = parsejp2(rawdata) + if imgformat == ImageFormat.JPEG2000 \ + and rawdata is not None and imgdata is None: + # this codepath gets called if the PIL installation is not able to + # handle JPEG2000 files + imgwidthpx, imgheightpx, ics, hdpi, vdpi = parsejp2(rawdata) - # TODO: read real dpi from input jpeg2000 image - ndpi = (default_dpi, default_dpi) + if hdpi is None: + hdpi = default_dpi + if vdpi is None: + vdpi = default_dpi + ndpi = (hdpi, vdpi) else: imgwidthpx, imgheightpx = imgdata.size @@ -591,6 +597,7 @@ def get_imgmetadata(imgdata, imgformat, default_dpi, colorspace, rawdata=None): def read_images(rawdata, colorspace, first_frame_only=False): im = BytesIO(rawdata) im.seek(0) + imgdata = None try: imgdata = Image.open(im) except IOError as e: