1
0
Fork 0
forked from josch/img2pdf

restore pep8 compliance

This commit is contained in:
Johannes 'josch' Schauer 2017-01-20 09:19:30 +01:00
parent 7df29a9acc
commit 16993964ce
2 changed files with 33 additions and 26 deletions

View file

@ -759,7 +759,8 @@ def in_to_pt(length):
return 72*length return 72*length
def get_layout_fun(pagesize=None, imgsize=None, border=None, fit=None, auto_orient=False): def get_layout_fun(pagesize=None, imgsize=None, border=None, fit=None,
auto_orient=False):
def fitfun(fit, imgwidth, imgheight, fitwidth, fitheight): def fitfun(fit, imgwidth, imgheight, fitwidth, fitheight):
if fitwidth is None and fitheight is None: if fitwidth is None and fitheight is None:
raise ValueError("fitwidth and fitheight cannot both be None") raise ValueError("fitwidth and fitheight cannot both be None")

View file

@ -495,6 +495,7 @@ def test_suite():
# retrieve the original image frame that this page was # retrieve the original image frame that this page was
# generated from # generated from
orig_img.seek(pagenum) orig_img.seek(pagenum)
cur_page = x.Root.Pages.Kids[pagenum]
ndpi = orig_img.info.get("dpi", (96.0, 96.0)) ndpi = orig_img.info.get("dpi", (96.0, 96.0))
# In python3, the returned dpi value for some tiff images will # In python3, the returned dpi value for some tiff images will
@ -513,26 +514,28 @@ def test_suite():
else: else:
return ("%.4f" % f).rstrip("0") return ("%.4f" % f).rstrip("0")
self.assertEqual(sorted(x.Root.Pages.Kids[pagenum].keys()), self.assertEqual(sorted(cur_page.keys()),
[PdfName.Contents, PdfName.MediaBox, [PdfName.Contents, PdfName.MediaBox,
PdfName.Parent, PdfName.Resources, PdfName.Type]) PdfName.Parent, PdfName.Resources,
self.assertEqual(x.Root.Pages.Kids[pagenum].MediaBox, PdfName.Type])
['0', '0', format_float(pagewidth), format_float(pageheight)]) self.assertEqual(cur_page.MediaBox,
self.assertEqual(x.Root.Pages.Kids[pagenum].Parent, x.Root.Pages) ['0', '0', format_float(pagewidth),
self.assertEqual(x.Root.Pages.Kids[pagenum].Type, PdfName.Page) format_float(pageheight)])
self.assertEqual(x.Root.Pages.Kids[pagenum].Resources.keys(), self.assertEqual(cur_page.Parent, x.Root.Pages)
self.assertEqual(cur_page.Type, PdfName.Page)
self.assertEqual(cur_page.Resources.keys(),
[PdfName.XObject]) [PdfName.XObject])
self.assertEqual(x.Root.Pages.Kids[pagenum].Resources.XObject.keys(), self.assertEqual(cur_page.Resources.XObject.keys(),
[PdfName.Im0]) [PdfName.Im0])
self.assertEqual(x.Root.Pages.Kids[pagenum].Contents.keys(), self.assertEqual(cur_page.Contents.keys(),
[PdfName.Length]) [PdfName.Length])
self.assertEqual(x.Root.Pages.Kids[pagenum].Contents.Length, self.assertEqual(cur_page.Contents.Length,
str(len(x.Root.Pages.Kids[pagenum].Contents.stream))) str(len(cur_page.Contents.stream)))
self.assertEqual(x.Root.Pages.Kids[pagenum].Contents.stream, self.assertEqual(cur_page.Contents.stream,
"q\n%.4f 0 0 %.4f 0.0000 0.0000 cm\n" "q\n%.4f 0 0 %.4f 0.0000 0.0000 cm\n"
"/Im0 Do\nQ" % (pagewidth, pageheight)) "/Im0 Do\nQ" % (pagewidth, pageheight))
imgprops = x.Root.Pages.Kids[pagenum].Resources.XObject.Im0 imgprops = cur_page.Resources.XObject.Im0
# test if the filter is valid: # test if the filter is valid:
self.assertIn( self.assertIn(
@ -541,7 +544,8 @@ def test_suite():
[PdfName.CCITTFaxDecode]]) [PdfName.CCITTFaxDecode]])
# test if the colorspace is valid # test if the colorspace is valid
self.assertIn( self.assertIn(
imgprops.ColorSpace, [PdfName.DeviceGray, PdfName.DeviceRGB, imgprops.ColorSpace, [PdfName.DeviceGray,
PdfName.DeviceRGB,
PdfName.DeviceCMYK]) PdfName.DeviceCMYK])
# test if the image has correct size # test if the image has correct size
@ -549,9 +553,10 @@ def test_suite():
self.assertEqual(imgprops.Height, str(orig_img.size[1])) self.assertEqual(imgprops.Height, str(orig_img.size[1]))
# if the input file is a jpeg then it should've been copied # if the input file is a jpeg then it should've been copied
# verbatim into the PDF # verbatim into the PDF
if imgprops.Filter in [[PdfName.DCTDecode], [PdfName.JPXDecode]]: if imgprops.Filter in [[PdfName.DCTDecode],
[PdfName.JPXDecode]]:
self.assertEqual( self.assertEqual(
x.Root.Pages.Kids[pagenum].Resources.XObject.Im0.stream, cur_page.Resources.XObject.Im0.stream,
convert_load(orig_imgdata)) convert_load(orig_imgdata))
elif imgprops.Filter == [PdfName.CCITTFaxDecode]: elif imgprops.Filter == [PdfName.CCITTFaxDecode]:
tiff_header = tiff_header_for_ccitt( tiff_header = tiff_header_for_ccitt(
@ -560,7 +565,7 @@ def test_suite():
imgio = BytesIO() imgio = BytesIO()
imgio.write(tiff_header) imgio.write(tiff_header)
imgio.write(convert_store( imgio.write(convert_store(
x.Root.Pages.Kids[pagenum].Resources.XObject.Im0.stream)) cur_page.Resources.XObject.Im0.stream))
imgio.seek(0) imgio.seek(0)
im = Image.open(imgio) im = Image.open(imgio)
self.assertEqual(im.tobytes(), orig_img.tobytes()) self.assertEqual(im.tobytes(), orig_img.tobytes())
@ -570,11 +575,10 @@ def test_suite():
pass pass
elif imgprops.Filter == [PdfName.FlateDecode]: elif imgprops.Filter == [PdfName.FlateDecode]:
# otherwise, the data is flate encoded and has to be equal to # otherwise, the data is flate encoded and has to be equal
# the pixel data of the input image # to the pixel data of the input image
imgdata = zlib.decompress( imgdata = zlib.decompress(
convert_store( convert_store(cur_page.Resources.XObject.Im0.stream))
x.Root.Pages.Kids[pagenum].Resources.XObject.Im0.stream))
colorspace = imgprops.ColorSpace colorspace = imgprops.ColorSpace
if colorspace == PdfName.DeviceGray: if colorspace == PdfName.DeviceGray:
colorspace = 'L' colorspace = 'L'
@ -588,11 +592,13 @@ def test_suite():
int(imgprops.Height)), int(imgprops.Height)),
imgdata) imgdata)
if orig_img.mode == '1': if orig_img.mode == '1':
self.assertEqual(im.tobytes(), orig_img.convert("L").tobytes()) self.assertEqual(im.tobytes(),
orig_img.convert("L").tobytes())
elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"): elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"):
self.assertEqual(im.tobytes(), orig_img.convert("RGB").tobytes()) self.assertEqual(im.tobytes(),
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have orig_img.convert("RGB").tobytes())
# the close() method # the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not
# have the close() method
try: try:
im.close() im.close()
except AttributeError: except AttributeError: