方法一
def jpegtopdf(self,path,outputpdfpath,uuid_):
filelist = []
filelist.extend(glob.glob(os.path.join(path, ‘*.jpeg’)))
im = Image.open(filelist[0])
maxw = im.size[0]
maxh = im.size[1]
maxsize = (maxw, maxh)
c = canvas.Canvas(outputpdfpath, pagesize=maxsize)
l = len(filelist)
for i in range(l):
c.drawImage(filelist[i], 0, 0, maxw, maxh)
c.showPage()
c.save()
生成的pdf会出现固定尺寸大小非各个图片自适应
方法二
代码如下:
file_list内为个jpeg路径
for x in file_list:
# if “jpg” in x or “png” in x or “tif” in x or ‘jpeg’ in x:
# new_pic.append(path1 + ‘/’ + x)
img = Image.open( x)
img = img.convert(‘RGB’)
im_list.append(img)
img1 = im_list.pop(0)
# img1.show()
img1.save(path2, “pdf”, resolution=100.0, save_all=True, append_images=im_list)
img1.close()
如上会出现顺序错乱问题解决如下:
添加列表排序规则
filelist.sort(key=lambda x: int(re.findall(r’\d+', x)[0]))
完美解决!