Use "with" to open and close input files
This commit is contained in:
parent
b726afbb5a
commit
d09cd0f197
1 changed files with 40 additions and 42 deletions
|
@ -206,51 +206,51 @@ def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=N
|
||||||
|
|
||||||
for imfilename in images:
|
for imfilename in images:
|
||||||
debug_out("Reading %s"%imfilename, verbose)
|
debug_out("Reading %s"%imfilename, verbose)
|
||||||
im = open(imfilename, "rb")
|
with open(imfilename, "rb") as im:
|
||||||
rawdata = im.read()
|
rawdata = im.read()
|
||||||
im.seek(0)
|
im.seek(0)
|
||||||
try:
|
try:
|
||||||
imgdata = Image.open(im)
|
imgdata = Image.open(im)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
# test if it is a jpeg2000 image
|
# test if it is a jpeg2000 image
|
||||||
if rawdata[:12] != "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A":
|
if rawdata[:12] != "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A":
|
||||||
error_out("cannot read input image (not jpeg2000)")
|
error_out("cannot read input image (not jpeg2000)")
|
||||||
error_out("PIL: %s"%e)
|
error_out("PIL: %s"%e)
|
||||||
exit(1)
|
exit(1)
|
||||||
# image is jpeg2000
|
# image is jpeg2000
|
||||||
width, height, ics = parsejp2(rawdata)
|
width, height, ics = parsejp2(rawdata)
|
||||||
imgformat = "JPEG2000"
|
imgformat = "JPEG2000"
|
||||||
|
|
||||||
if dpi:
|
if dpi:
|
||||||
ndpi = dpi, dpi
|
ndpi = dpi, dpi
|
||||||
debug_out("input dpi (forced) = %d x %d"%ndpi, verbose)
|
debug_out("input dpi (forced) = %d x %d"%ndpi, verbose)
|
||||||
else:
|
else:
|
||||||
ndpi = (96, 96) # TODO: read real dpi
|
ndpi = (96, 96) # TODO: read real dpi
|
||||||
debug_out("input dpi = %d x %d"%ndpi, verbose)
|
debug_out("input dpi = %d x %d"%ndpi, verbose)
|
||||||
|
|
||||||
if colorspace:
|
if colorspace:
|
||||||
color = colorspace
|
color = colorspace
|
||||||
debug_out("input colorspace (forced) = %s"%(ics))
|
debug_out("input colorspace (forced) = %s"%(ics))
|
||||||
|
else:
|
||||||
|
color = ics
|
||||||
|
debug_out("input colorspace = %s"%(ics), verbose)
|
||||||
else:
|
else:
|
||||||
color = ics
|
width, height = imgdata.size
|
||||||
debug_out("input colorspace = %s"%(ics), verbose)
|
imgformat = imgdata.format
|
||||||
else:
|
|
||||||
width, height = imgdata.size
|
|
||||||
imgformat = imgdata.format
|
|
||||||
|
|
||||||
if dpi:
|
if dpi:
|
||||||
ndpi = dpi, dpi
|
ndpi = dpi, dpi
|
||||||
debug_out("input dpi (forced) = %d x %d"%ndpi, verbose)
|
debug_out("input dpi (forced) = %d x %d"%ndpi, verbose)
|
||||||
else:
|
else:
|
||||||
ndpi = imgdata.info.get("dpi", (96, 96))
|
ndpi = imgdata.info.get("dpi", (96, 96))
|
||||||
debug_out("input dpi = %d x %d"%ndpi, verbose)
|
debug_out("input dpi = %d x %d"%ndpi, verbose)
|
||||||
|
|
||||||
if colorspace:
|
if colorspace:
|
||||||
color = colorspace
|
color = colorspace
|
||||||
debug_out("input colorspace (forced) = %s"%(color), verbose)
|
debug_out("input colorspace (forced) = %s"%(color), verbose)
|
||||||
else:
|
else:
|
||||||
color = imgdata.mode
|
color = imgdata.mode
|
||||||
debug_out("input colorspace = %s"%(color), verbose)
|
debug_out("input colorspace = %s"%(color), verbose)
|
||||||
|
|
||||||
debug_out("width x height = %d x %d"%(width,height), verbose)
|
debug_out("width x height = %d x %d"%(width,height), verbose)
|
||||||
debug_out("imgformat = %s"%imgformat, verbose)
|
debug_out("imgformat = %s"%imgformat, verbose)
|
||||||
|
@ -286,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)
|
pdf.addimage(color, width, height, imgformat, imgdata, pdf_x, pdf_y)
|
||||||
|
|
||||||
im.close()
|
|
||||||
|
|
||||||
return pdf.tostring()
|
return pdf.tostring()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue