本文主要用到pil库的image模块,通过open方法打开图像,通过save另存一下,就实现了jpg转pdf,特简单。对应单个文件处理的函数实现如下:
def jpg2pdf(jpgFile):
path,fileName = jpgFile.rsplit('\\',1)
preName,postName = fileName.rsplit('.',1)
img = Image.open(jpgFile)
return img.save(path+"\\"+preName+'.pdf', "PDF", resolution=100.0, save_all=True)
完整代码
from PIL import Image
import os
def jpg2pdf(jpgFile):
path,fileName = jpgFile.rsplit('\\',1)
preName,postName = fileName.rsplit('.',1)
img = Image.open(jpgFile)
return img.save(path+"\\"+preName+'.pdf', "PDF", resolution=100.0, save_all=True)
def jpg2pdfByPath(pathName):
files = os.listdir(pathName)
for f in files:
if f.lower().find(".jpg")>0 :
jpg2pdf(pathName+'\\'+f)
jpg2pdfByPath(r'c:\img\jpg')