我有一千张PNG图像,并使用reportlab将这些图像添加到PDF中,代码是:
def pic_exits(pic_path):
if os.path.exists(pic_path):
#print ('pic_exits')
#print (pic_path)
target = PIL.Image.open(pic_path)
#fp = open(pic_path,'rb')
#target = PIL.Image.open(fp)
#fp.close()
targetx,targety = target.size
del target
#print (targetx,targety)
tx,ty = get_xy(targetx,targety)
targetimg = Image(pic_path, tx, ty)
else:
targetimg = Paragraph('<para align = center>not exist</para>',Bodystyle)
return targetimg
def draw_sourcepic(self_dir,pic_list):
pic = [pic_list[i:i+3] for i in range(0,len(pic_list),3)]
data = []
i = 0
for val in pic:
eachline = []
for pic_path in val:
i+=1
pic_image = pic_exits(pic_path)
eachline.append(pic_image)
while len(eachline) < 3:
eachline.append(None)
data.append(eachline)
pic_table = drawTable(data,(150,150,150),None)
Story.append(pic_table)
但它有以下错误: IOError: Cannot open resource "C:\workspace\decode\2012\result\pic\510.png 这可能意味着我打开了太多图像,
官方文件说:一张图片(数字图片)。PIL/Java 1.4 支持的格式(支持 Python/Java 图像库。目前,作为可流动的图像始终在框架中水平居中。我们允许两种惰性以允许文档中包含许多图像,这可能导致到文件句柄饥饿。lazy=1 在需要之前不要打开图像。lazy=2 在需要时打开图像然后关闭它。
但是当我改成这个时: targetimg = Image(pic_path, tx, ty, lazy = 2) 它有以下错误: AttributeError: ._file
如何将数千张图像添加到 PDF 中?问题很长,