src/tests/__init__.py: handle PNG input images properly

This commit is contained in:
Johannes 'josch' Schauer 2018-07-18 00:05:15 +02:00
parent b99fae1380
commit d931f02709
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -589,30 +589,39 @@ def test_suite():
# to the pixel data of the input image # to the pixel data of the input image
imgdata = zlib.decompress( imgdata = zlib.decompress(
convert_store(cur_page.Resources.XObject.Im0.stream)) convert_store(cur_page.Resources.XObject.Im0.stream))
colorspace = imgprops.ColorSpace if imgprops.DecodeParms:
if colorspace == PdfName.DeviceGray: if orig_img.format == 'PNG':
colorspace = 'L' pngidat, palette = img2pdf.parse_png(orig_imgdata)
elif colorspace == PdfName.DeviceRGB: else:
colorspace = 'RGB' pngbuffer = BytesIO()
elif colorspace == PdfName.DeviceCMYK: orig_img.save(pngbuffer, format="png")
colorspace = 'CMYK' pngidat, palette = img2pdf.parse_png(pngbuffer.getvalue())
self.assertEqual(zlib.decompress(pngidat), imgdata)
else: else:
raise Exception("invalid colorspace") colorspace = imgprops.ColorSpace
im = Image.frombytes(colorspace, (int(imgprops.Width), if colorspace == PdfName.DeviceGray:
int(imgprops.Height)), colorspace = 'L'
imgdata) elif colorspace == PdfName.DeviceRGB:
if orig_img.mode == '1': colorspace = 'RGB'
self.assertEqual(im.tobytes(), elif colorspace == PdfName.DeviceCMYK:
orig_img.convert("L").tobytes()) colorspace = 'CMYK'
elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"): else:
self.assertEqual(im.tobytes(), raise Exception("invalid colorspace")
orig_img.convert("RGB").tobytes()) im = Image.frombytes(colorspace, (int(imgprops.Width),
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not int(imgprops.Height)),
# have the close() method imgdata)
try: if orig_img.mode == '1':
im.close() self.assertEqual(im.tobytes(),
except AttributeError: orig_img.convert("L").tobytes())
pass elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"):
self.assertEqual(im.tobytes(),
orig_img.convert("RGB").tobytes())
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not
# have the close() method
try:
im.close()
except AttributeError:
pass
# now use pdfrw to parse and then write out both pdfs and check the # now use pdfrw to parse and then write out both pdfs and check the
# result for equality # result for equality
y = PdfReader(out) y = PdfReader(out)