本文主要用到pil庫的image模塊加矛,通過open方法打開圖像,通過save另存一下儡羔,就實(shí)現(xiàn)了jpg轉(zhuǎn)pdf宣羊,特簡單。對應(yīng)單個(gè)文件處理的函數(shù)實(shí)現(xiàn)如下:
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')