make pep8 compliant
This commit is contained in:
parent
9395b6fbbe
commit
198c98a5f9
2 changed files with 48 additions and 28 deletions
|
@ -277,7 +277,8 @@ if PY3:
|
|||
@classmethod
|
||||
def encode(cls, string, hextype=False):
|
||||
if hextype:
|
||||
return b'< ' + b' '.join(("%06x"%c).encode('ascii') for c in string) + b' >'
|
||||
return b'< ' + b' '.join(
|
||||
("%06x" % c).encode('ascii') for c in string) + b' >'
|
||||
else:
|
||||
try:
|
||||
string = string.encode('ascii')
|
||||
|
@ -292,7 +293,8 @@ else:
|
|||
@classmethod
|
||||
def encode(cls, string, hextype=False):
|
||||
if hextype:
|
||||
return b'< ' + b' '.join(("%06x"%c).encode('ascii') for c in string) + b' >'
|
||||
return b'< ' + b' '.join(
|
||||
("%06x" % c).encode('ascii') for c in string) + b' >'
|
||||
else:
|
||||
# This mimics exactely to what pdfrw does.
|
||||
string = string.replace(b'\\', b'\\\\')
|
||||
|
@ -393,8 +395,11 @@ class pdfdoc(object):
|
|||
colorspace = PdfName.DeviceCMYK
|
||||
elif color == Colorspace.P:
|
||||
if self.with_pdfrw:
|
||||
raise Exception("pdfrw does not support hex strings for palette image input, re-run with --without-pdfrw")
|
||||
colorspace = [ PdfName.Indexed, PdfName.DeviceRGB, len(palette)-1, PdfString.encode(palette, hextype=True)]
|
||||
raise Exception("pdfrw does not support hex strings for "
|
||||
"palette image input, re-run with "
|
||||
"--without-pdfrw")
|
||||
colorspace = [PdfName.Indexed, PdfName.DeviceRGB, len(palette)-1,
|
||||
PdfString.encode(palette, hextype=True)]
|
||||
else:
|
||||
raise UnsupportedColorspaceError("unsupported color space: %s"
|
||||
% color.name)
|
||||
|
@ -647,13 +652,14 @@ def get_imgmetadata(imgdata, imgformat, default_dpi, colorspace, rawdata=None):
|
|||
ics = imgdata.mode
|
||||
|
||||
if ics in ["LA", "PA", "RGBA"]:
|
||||
logging.warning("Image contains transparency which cannot be retained in PDF.")
|
||||
logging.warning("Image contains transparency which cannot be retained "
|
||||
"in PDF.")
|
||||
logging.warning("img2pdf will not perform a lossy operation.")
|
||||
logging.warning("You can remove the alpha channel using imagemagick:")
|
||||
logging.warning(" $ convert input.png -background white -alpha remove -alpha off output.png")
|
||||
logging.warning(" $ convert input.png -background white -alpha "
|
||||
"remove -alpha off output.png")
|
||||
raise Exception("Refusing to work on images with alpha channel")
|
||||
|
||||
|
||||
# Since commit 07a96209597c5e8dfe785c757d7051ce67a980fb or release 4.1.0
|
||||
# Pillow retrieves the DPI from EXIF if it cannot find the DPI in the JPEG
|
||||
# header. In that case it can happen that the horizontal and vertical DPI
|
||||
|
@ -694,7 +700,8 @@ def ccitt_payload_location_from_pil(img):
|
|||
# If Pillow is passed an invalid compression argument it will ignore it;
|
||||
# make sure the image actually got compressed.
|
||||
if img.info['compression'] != 'group4':
|
||||
raise ValueError("Image not compressed with CCITT Group 4 but with: %s" % img.info['compression'])
|
||||
raise ValueError("Image not compressed with CCITT Group 4 but with: %s"
|
||||
% img.info['compression'])
|
||||
|
||||
# Read the TIFF tags to find the offset(s) of the compressed data strips.
|
||||
strip_offsets = img.tag_v2[TiffImagePlugin.STRIPOFFSETS]
|
||||
|
@ -752,13 +759,15 @@ def parse_png(rawdata):
|
|||
pngidat += rawdata[i:i+n]
|
||||
elif rawdata[i-4:i] == b"PLTE":
|
||||
for j in range(i, i+n, 3):
|
||||
# with int.from_bytes() we would not have to prepend extra zeroes
|
||||
# with int.from_bytes() we would not have to prepend extra
|
||||
# zeroes
|
||||
color, = struct.unpack('>I', b'\x00'+rawdata[j:j+3])
|
||||
palette.append(color)
|
||||
i += n
|
||||
i += 12
|
||||
return pngidat, palette
|
||||
|
||||
|
||||
def read_images(rawdata, colorspace, first_frame_only=False):
|
||||
im = BytesIO(rawdata)
|
||||
im.seek(0)
|
||||
|
@ -796,7 +805,8 @@ def read_images(rawdata, colorspace, first_frame_only=False):
|
|||
if color == Colorspace['RGBA']:
|
||||
raise JpegColorspaceError("jpeg can't have an alpha channel")
|
||||
im.close()
|
||||
return [(color, ndpi, imgformat, rawdata, imgwidthpx, imgheightpx, [], False)]
|
||||
return [(color, ndpi, imgformat, rawdata, imgwidthpx, imgheightpx, [],
|
||||
False)]
|
||||
|
||||
# We can directly embed the IDAT chunk of PNG images if the PNG is not
|
||||
# interlaced
|
||||
|
@ -810,7 +820,8 @@ def read_images(rawdata, colorspace, first_frame_only=False):
|
|||
imgdata, imgformat, default_dpi, colorspace, rawdata)
|
||||
pngidat, palette = parse_png(rawdata)
|
||||
im.close()
|
||||
return [(color, ndpi, imgformat, pngidat, imgwidthpx, imgheightpx, palette, False)]
|
||||
return [(color, ndpi, imgformat, pngidat, imgwidthpx, imgheightpx,
|
||||
palette, False)]
|
||||
|
||||
# We can directly copy the data out of a CCITT Group 4 encoded TIFF, if it
|
||||
# only contains a single strip
|
||||
|
@ -822,14 +833,16 @@ def read_images(rawdata, colorspace, first_frame_only=False):
|
|||
if photo == 0:
|
||||
inverted = True
|
||||
elif photo != 1:
|
||||
raise ValueError("unsupported photometric interpretation for group4 tiff: %d" % photo)
|
||||
raise ValueError("unsupported photometric interpretation for "
|
||||
"group4 tiff: %d" % photo)
|
||||
color, ndpi, imgwidthpx, imgheightpx = get_imgmetadata(
|
||||
imgdata, imgformat, default_dpi, colorspace, rawdata)
|
||||
offset, length = ccitt_payload_location_from_pil(imgdata)
|
||||
im.seek(offset)
|
||||
rawdata = im.read(length)
|
||||
im.close()
|
||||
return [(color, ndpi, ImageFormat.CCITTGroup4, rawdata, imgwidthpx, imgheightpx, [], inverted)]
|
||||
return [(color, ndpi, ImageFormat.CCITTGroup4, rawdata, imgwidthpx,
|
||||
imgheightpx, [], inverted)]
|
||||
|
||||
# Everything else has to be encoded
|
||||
|
||||
|
@ -869,7 +882,8 @@ def read_images(rawdata, colorspace, first_frame_only=False):
|
|||
logging.debug("Colorspace is OK: %s", color)
|
||||
newimg = imgdata
|
||||
else:
|
||||
raise ValueError("unknown or unsupported colorspace: %s" % color.name)
|
||||
raise ValueError("unknown or unsupported colorspace: %s"
|
||||
% color.name)
|
||||
# the PNG format does not support CMYK, so we fall back to normal
|
||||
# compression
|
||||
if color in [Colorspace.CMYK, Colorspace["CMYK;I"]]:
|
||||
|
@ -1194,7 +1208,7 @@ def convert(*images, **kwargs):
|
|||
try:
|
||||
with open(img, "rb") as f:
|
||||
rawdata = f.read()
|
||||
except:
|
||||
except Exception:
|
||||
# whatever the exception is (string could contain NUL
|
||||
# characters or the path could just not exist) it's not a file
|
||||
# name so we now try treating it as raw image content
|
||||
|
|
|
@ -592,13 +592,17 @@ def test_suite():
|
|||
if imgprops.DecodeParms:
|
||||
if orig_img.format == 'PNG':
|
||||
pngidat, palette = img2pdf.parse_png(orig_imgdata)
|
||||
elif orig_img.format == 'TIFF' and orig_img.info['compression'] == "group4":
|
||||
offset, length = img2pdf.ccitt_payload_location_from_pil(orig_img)
|
||||
elif orig_img.format == 'TIFF' \
|
||||
and orig_img.info['compression'] == "group4":
|
||||
offset, length = \
|
||||
img2pdf.ccitt_payload_location_from_pil(
|
||||
orig_img)
|
||||
pngidat = orig_imgdata[offset:offset+length]
|
||||
else:
|
||||
pngbuffer = BytesIO()
|
||||
orig_img.save(pngbuffer, format="png")
|
||||
pngidat, palette = img2pdf.parse_png(pngbuffer.getvalue())
|
||||
pngidat, palette = img2pdf.parse_png(
|
||||
pngbuffer.getvalue())
|
||||
self.assertEqual(zlib.decompress(pngidat), imgdata)
|
||||
else:
|
||||
colorspace = imgprops.ColorSpace
|
||||
|
@ -610,17 +614,19 @@ def test_suite():
|
|||
colorspace = 'CMYK'
|
||||
else:
|
||||
raise Exception("invalid colorspace")
|
||||
im = Image.frombytes(colorspace, (int(imgprops.Width),
|
||||
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"):
|
||||
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
|
||||
# the python-pil version 2.3.0-1ubuntu3 in Ubuntu does
|
||||
# not have the close() method
|
||||
try:
|
||||
im.close()
|
||||
except AttributeError:
|
||||
|
|
Loading…
Reference in a new issue