Merge pull request #18 from DarwinAwardWinner/master
Avoid leaking file descriptors and convert unrecognized colorspaces to RGB
This commit is contained in:
commit
f881a00c4b
1 changed files with 50 additions and 43 deletions
|
@ -204,7 +204,9 @@ def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=N
|
|||
pdf = pdfdoc(3, title, author, creator, producer, creationdate,
|
||||
moddate, subject, keywords)
|
||||
|
||||
for im in images:
|
||||
for imfilename in images:
|
||||
debug_out("Reading %s"%imfilename, verbose)
|
||||
with open(imfilename, "rb") as im:
|
||||
rawdata = im.read()
|
||||
im.seek(0)
|
||||
try:
|
||||
|
@ -263,8 +265,15 @@ def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=N
|
|||
else:
|
||||
# because we do not support /CCITTFaxDecode
|
||||
if color == '1':
|
||||
debug_out("Converting colorspace 1 to L", verbose)
|
||||
imgdata = imgdata.convert('L')
|
||||
color = 'L'
|
||||
elif color in ("RGB", "L"):
|
||||
debug_out("Colorspace is OK: %s"%color, verbose)
|
||||
else:
|
||||
debug_out("Converting colorspace %s to RGB"%color, verbose)
|
||||
imgdata = imgdata.convert('RGB')
|
||||
color = imgdata.mode
|
||||
imgdata = zlib.compress(imgdata.tostring())
|
||||
|
||||
# pdf units = 1/72 inch
|
||||
|
@ -277,8 +286,6 @@ def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=N
|
|||
|
||||
pdf.addimage(color, width, height, imgformat, imgdata, pdf_x, pdf_y)
|
||||
|
||||
im.close()
|
||||
|
||||
return pdf.tostring()
|
||||
|
||||
|
||||
|
@ -295,7 +302,7 @@ def valid_date(string):
|
|||
parser = argparse.ArgumentParser(
|
||||
description='Lossless conversion/embedding of images (in)to pdf')
|
||||
parser.add_argument(
|
||||
'images', metavar='infile', type=argparse.FileType('rb'),
|
||||
'images', metavar='infile', type=str,
|
||||
nargs='+', help='input file(s)')
|
||||
parser.add_argument(
|
||||
'-o', '--output', metavar='out', type=argparse.FileType('wb'),
|
||||
|
|
Loading…
Reference in a new issue