src/tests/__init__.py: add support for multiframe gif tests
This commit is contained in:
parent
e20fe4944c
commit
8476e333cd
1 changed files with 112 additions and 86 deletions
|
@ -476,101 +476,127 @@ def test_suite():
|
||||||
x = PdfReader(StringIO(convert_load(output)))
|
x = PdfReader(StringIO(convert_load(output)))
|
||||||
self.assertEqual(sorted(x.keys()), [PdfName.Info, PdfName.Root,
|
self.assertEqual(sorted(x.keys()), [PdfName.Info, PdfName.Root,
|
||||||
PdfName.Size])
|
PdfName.Size])
|
||||||
self.assertEqual(x.Size, '7')
|
self.assertIn(x.Root.Pages.Count, ('1', '2'))
|
||||||
|
if len(x.Root.Pages.Kids) == '1':
|
||||||
|
self.assertEqual(x.Size, '7')
|
||||||
|
self.assertEqual(len(x.Root.Pages.Kids), 1)
|
||||||
|
elif len(x.Root.Pages.Kids) == '2':
|
||||||
|
self.assertEqual(x.Size, '10')
|
||||||
|
self.assertEqual(len(x.Root.Pages.Kids), 2)
|
||||||
self.assertEqual(x.Info, {})
|
self.assertEqual(x.Info, {})
|
||||||
self.assertEqual(sorted(x.Root.keys()), [PdfName.Pages,
|
self.assertEqual(sorted(x.Root.keys()), [PdfName.Pages,
|
||||||
PdfName.Type])
|
PdfName.Type])
|
||||||
self.assertEqual(x.Root.Type, PdfName.Catalog)
|
self.assertEqual(x.Root.Type, PdfName.Catalog)
|
||||||
self.assertEqual(sorted(x.Root.Pages.keys()),
|
self.assertEqual(sorted(x.Root.Pages.keys()),
|
||||||
[PdfName.Count, PdfName.Kids, PdfName.Type])
|
[PdfName.Count, PdfName.Kids, PdfName.Type])
|
||||||
self.assertEqual(x.Root.Pages.Count, '1')
|
|
||||||
self.assertEqual(x.Root.Pages.Type, PdfName.Pages)
|
self.assertEqual(x.Root.Pages.Type, PdfName.Pages)
|
||||||
self.assertEqual(len(x.Root.Pages.Kids), 1)
|
|
||||||
self.assertEqual(sorted(x.Root.Pages.Kids[0].keys()),
|
|
||||||
[PdfName.Contents, PdfName.MediaBox,
|
|
||||||
PdfName.Parent, PdfName.Resources, PdfName.Type])
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].MediaBox,
|
|
||||||
['0', '0', '115', '48'])
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Parent, x.Root.Pages)
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Type, PdfName.Page)
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Resources.keys(),
|
|
||||||
[PdfName.XObject])
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Resources.XObject.keys(),
|
|
||||||
[PdfName.Im0])
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Contents.keys(),
|
|
||||||
[PdfName.Length])
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Contents.Length,
|
|
||||||
str(len(x.Root.Pages.Kids[0].Contents.stream)))
|
|
||||||
self.assertEqual(x.Root.Pages.Kids[0].Contents.stream,
|
|
||||||
"q\n115.0000 0 0 48.0000 0.0000 0.0000 cm\n/Im0 "
|
|
||||||
"Do\nQ")
|
|
||||||
|
|
||||||
imgprops = x.Root.Pages.Kids[0].Resources.XObject.Im0
|
|
||||||
|
|
||||||
# test if the filter is valid:
|
|
||||||
self.assertIn(
|
|
||||||
imgprops.Filter, [[PdfName.DCTDecode], [PdfName.JPXDecode],
|
|
||||||
[PdfName.FlateDecode],
|
|
||||||
[PdfName.CCITTFaxDecode]])
|
|
||||||
# test if the colorspace is valid
|
|
||||||
self.assertIn(
|
|
||||||
imgprops.ColorSpace, [PdfName.DeviceGray, PdfName.DeviceRGB,
|
|
||||||
PdfName.DeviceCMYK])
|
|
||||||
# test if the image has correct size
|
|
||||||
orig_img = Image.open(f)
|
orig_img = Image.open(f)
|
||||||
self.assertEqual(imgprops.Width, str(orig_img.size[0]))
|
for pagenum in range(len(x.Root.Pages.Kids)):
|
||||||
self.assertEqual(imgprops.Height, str(orig_img.size[1]))
|
# retrieve the original image frame that this page was
|
||||||
# if the input file is a jpeg then it should've been copied
|
# generated from
|
||||||
# verbatim into the PDF
|
orig_img.seek(pagenum)
|
||||||
if imgprops.Filter in [[PdfName.DCTDecode], [PdfName.JPXDecode]]:
|
|
||||||
self.assertEqual(
|
|
||||||
x.Root.Pages.Kids[0].Resources.XObject.Im0.stream,
|
|
||||||
convert_load(orig_imgdata))
|
|
||||||
elif imgprops.Filter == [PdfName.CCITTFaxDecode]:
|
|
||||||
tiff_header = tiff_header_for_ccitt(
|
|
||||||
int(imgprops.Width), int(imgprops.Height),
|
|
||||||
int(imgprops.Length), 4)
|
|
||||||
imgio = BytesIO()
|
|
||||||
imgio.write(tiff_header)
|
|
||||||
imgio.write(convert_store(
|
|
||||||
x.Root.Pages.Kids[0].Resources.XObject.Im0.stream))
|
|
||||||
imgio.seek(0)
|
|
||||||
im = Image.open(imgio)
|
|
||||||
self.assertEqual(im.tobytes(), orig_img.tobytes())
|
|
||||||
try:
|
|
||||||
im.close()
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
elif imgprops.Filter == [PdfName.FlateDecode]:
|
ndpi = orig_img.info.get("dpi", (96.0, 96.0))
|
||||||
# otherwise, the data is flate encoded and has to be equal to
|
# In python3, the returned dpi value for some tiff images will
|
||||||
# the pixel data of the input image
|
# not be an integer but a float. To make the behaviour of
|
||||||
imgdata = zlib.decompress(
|
# img2pdf the same between python2 and python3, we convert that
|
||||||
convert_store(
|
# float into an integer by rounding.
|
||||||
x.Root.Pages.Kids[0].Resources.XObject.Im0.stream))
|
# Search online for the 72.009 dpi problem for more info.
|
||||||
colorspace = imgprops.ColorSpace
|
ndpi = (int(round(ndpi[0])), int(round(ndpi[1])))
|
||||||
if colorspace == PdfName.DeviceGray:
|
imgwidthpx, imgheightpx = orig_img.size
|
||||||
colorspace = 'L'
|
pagewidth = 72*imgwidthpx/ndpi[0]
|
||||||
elif colorspace == PdfName.DeviceRGB:
|
pageheight = 72*imgheightpx/ndpi[1]
|
||||||
colorspace = 'RGB'
|
|
||||||
elif colorspace == PdfName.DeviceCMYK:
|
def format_float(f):
|
||||||
colorspace = 'CMYK'
|
if int(f) == f:
|
||||||
else:
|
return str(int(f))
|
||||||
raise Exception("invalid colorspace")
|
else:
|
||||||
im = Image.frombytes(colorspace, (int(imgprops.Width),
|
return ("%.4f" % f).rstrip("0")
|
||||||
int(imgprops.Height)),
|
|
||||||
imgdata)
|
self.assertEqual(sorted(x.Root.Pages.Kids[pagenum].keys()),
|
||||||
if orig_img.mode == '1':
|
[PdfName.Contents, PdfName.MediaBox,
|
||||||
orig_img = orig_img.convert("L")
|
PdfName.Parent, PdfName.Resources, PdfName.Type])
|
||||||
elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"):
|
self.assertEqual(x.Root.Pages.Kids[pagenum].MediaBox,
|
||||||
orig_img = orig_img.convert("RGB")
|
['0', '0', format_float(pagewidth), format_float(pageheight)])
|
||||||
self.assertEqual(im.tobytes(), orig_img.tobytes())
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Parent, x.Root.Pages)
|
||||||
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Type, PdfName.Page)
|
||||||
# the close() method
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Resources.keys(),
|
||||||
try:
|
[PdfName.XObject])
|
||||||
im.close()
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Resources.XObject.keys(),
|
||||||
except AttributeError:
|
[PdfName.Im0])
|
||||||
pass
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Contents.keys(),
|
||||||
|
[PdfName.Length])
|
||||||
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Contents.Length,
|
||||||
|
str(len(x.Root.Pages.Kids[pagenum].Contents.stream)))
|
||||||
|
self.assertEqual(x.Root.Pages.Kids[pagenum].Contents.stream,
|
||||||
|
"q\n%.4f 0 0 %.4f 0.0000 0.0000 cm\n"
|
||||||
|
"/Im0 Do\nQ" % (pagewidth, pageheight))
|
||||||
|
|
||||||
|
imgprops = x.Root.Pages.Kids[pagenum].Resources.XObject.Im0
|
||||||
|
|
||||||
|
# test if the filter is valid:
|
||||||
|
self.assertIn(
|
||||||
|
imgprops.Filter, [[PdfName.DCTDecode], [PdfName.JPXDecode],
|
||||||
|
[PdfName.FlateDecode],
|
||||||
|
[PdfName.CCITTFaxDecode]])
|
||||||
|
# test if the colorspace is valid
|
||||||
|
self.assertIn(
|
||||||
|
imgprops.ColorSpace, [PdfName.DeviceGray, PdfName.DeviceRGB,
|
||||||
|
PdfName.DeviceCMYK])
|
||||||
|
|
||||||
|
# test if the image has correct size
|
||||||
|
self.assertEqual(imgprops.Width, str(orig_img.size[0]))
|
||||||
|
self.assertEqual(imgprops.Height, str(orig_img.size[1]))
|
||||||
|
# if the input file is a jpeg then it should've been copied
|
||||||
|
# verbatim into the PDF
|
||||||
|
if imgprops.Filter in [[PdfName.DCTDecode], [PdfName.JPXDecode]]:
|
||||||
|
self.assertEqual(
|
||||||
|
x.Root.Pages.Kids[pagenum].Resources.XObject.Im0.stream,
|
||||||
|
convert_load(orig_imgdata))
|
||||||
|
elif imgprops.Filter == [PdfName.CCITTFaxDecode]:
|
||||||
|
tiff_header = tiff_header_for_ccitt(
|
||||||
|
int(imgprops.Width), int(imgprops.Height),
|
||||||
|
int(imgprops.Length), 4)
|
||||||
|
imgio = BytesIO()
|
||||||
|
imgio.write(tiff_header)
|
||||||
|
imgio.write(convert_store(
|
||||||
|
x.Root.Pages.Kids[pagenum].Resources.XObject.Im0.stream))
|
||||||
|
imgio.seek(0)
|
||||||
|
im = Image.open(imgio)
|
||||||
|
self.assertEqual(im.tobytes(), orig_img.tobytes())
|
||||||
|
try:
|
||||||
|
im.close()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
elif imgprops.Filter == [PdfName.FlateDecode]:
|
||||||
|
# otherwise, the data is flate encoded and has to be equal to
|
||||||
|
# the pixel data of the input image
|
||||||
|
imgdata = zlib.decompress(
|
||||||
|
convert_store(
|
||||||
|
x.Root.Pages.Kids[pagenum].Resources.XObject.Im0.stream))
|
||||||
|
colorspace = imgprops.ColorSpace
|
||||||
|
if colorspace == PdfName.DeviceGray:
|
||||||
|
colorspace = 'L'
|
||||||
|
elif colorspace == PdfName.DeviceRGB:
|
||||||
|
colorspace = 'RGB'
|
||||||
|
elif colorspace == PdfName.DeviceCMYK:
|
||||||
|
colorspace = 'CMYK'
|
||||||
|
else:
|
||||||
|
raise Exception("invalid colorspace")
|
||||||
|
im = Image.frombytes(colorspace, (int(imgprops.Width),
|
||||||
|
int(imgprops.Height)),
|
||||||
|
imgdata)
|
||||||
|
if orig_img.mode == '1':
|
||||||
|
self.assertEqual(im.tobytes(), orig_img.convert("L").tobytes())
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in a new issue