Python合并多張圖片成PDF
最近需要將記的筆記整理成一個 pdf 進(jìn)行保存,所以就研究了一下如何利用 Python 代碼將拍下來的照片整個合并成一個 pdf
過程拿到一個需求最重要的就是將大塊任務(wù)拆分成一個個小模塊,逐個擊破。
拍照這一步首先是將所有的書頁拍好,需要注意的是要按照書的頁碼來拍,因?yàn)楹竺娴呐判蚴前凑瘴募M(jìn)行排序的,拍照的文件名基本上是按照時間生成的,如果拍的時候亂了,到時候生成的 pdf 里面的頁碼也會亂掉。
用到的Python 操作庫Python 最好的地方就是有大量的第三方庫能幫我們快速實(shí)現(xiàn)我們想要的方法,搜索到了兩個庫,PyFPDF 和img2pdf,我們這里選擇img2pdf來完成我們的需求
pip install img2pdfPython遍歷文件夾獲取圖片
dirname = 'f:/wlzcool' imgs = [] for fname in os.listdir(dirname):if not fname.endswith('.jpg'): continuepath = os.path.join(dirname, fname)if os.path.isdir(path): continueimgs.append(path)
需要注意圖片的文件名如果是純數(shù)字且位數(shù)不一樣,排序會為1之后是10而不是2,需要進(jìn)行一個排序,如果是手機(jī)拍的文件就沒有這個問題。
files.sort(key=lambda x: int(x[:-4]))旋轉(zhuǎn)圖片展示方向并壓縮像素
有的時候手機(jī)拍出來的圖片是水平的,需要將其改為豎直的用rotate旋轉(zhuǎn)方向的時候需要注意加上expand=True 這個參數(shù),否則會有黑邊出現(xiàn)。手機(jī)的照片像素太高,有的需要進(jìn)行壓縮以保證最后生成的pdf的大小適中。
img = Image.open(path)if img.size[0] > img.size[1]:im_rotate = img.rotate(90, expand=True)size = (int(im_rotate.size[0] / 3), int(im_rotate.size[1] / 3))im_rotate = im_rotate.resize(size)im_rotate.save(savepath, quality=95) else:size = (int(img.size[0] / 3), int(img.size[1] / 3))img = img.resize(size)img.save(savepath, quality=95)整體代碼
寫成腳本需要考慮的有很多,為了方便使用,需要將各種參數(shù)改為允許用戶輸入的。比如圖片文件夾所在的路徑,壓縮比之類的
from PIL import Imageimport osimport img2pdfflag = Falsewhile not flag: dirname = input('請輸入圖片文件夾所在路徑(例如d:/wlzcool):') flag = os.path.exists(dirname) if not flag:print('圖片文件夾所在路徑不存在!')saveflag = Falsewhile not saveflag: savedirname = input('請輸入目標(biāo)圖片文件夾所在路徑(例如d:/wlzcool2):') saveflag = os.path.exists(savedirname) if not saveflag:print('圖片文件夾所在路徑不存在!')automakedir = input('是否自動創(chuàng)建對應(yīng)文件夾?(是Y/否N):')if automakedir.strip().upper() == 'Y': os.makedirs(savedirname) saveflag = Truefiles = os.listdir(dirname)reductionFactor = int(input('請輸入長寬壓縮比(例如3):'))if reductionFactor <= 0: reductionFactor = 3isConvertBlack = input('是否輸出黑白版本?(是Y/否N):').strip().upper() == 'Y'for fname in files: if not fname.endswith('.jpg'):continue path = os.path.join(dirname, fname) savePath = os.path.join(savedirname, fname) if os.path.isdir(path):continue img = Image.open(path)if img.size[0] > img.size[1]:im_rotate = img.rotate(90, expand=True)size = (int(im_rotate.size[0] / reductionFactor), int(im_rotate.size[1] / reductionFactor))im_rotate = im_rotate.resize(size)if isConvertBlack: im_rotate = im_rotate.convert('L')im_rotate.save(savePath, quality=95) else:size = (int(img.size[0] / reductionFactor), int(img.size[1] / reductionFactor))img = img.resize(size)if isConvertBlack: img = img.convert('L')img.save(savePath, quality=95)filename = input('請輸入輸出文件名(例如:第一章):')with open(filename + '.pdf', 'wb') as f: imgs = [] files = os.listdir(savedirname) for fname in files:if not fname.endswith('.jpg'): continuepath = os.path.join(savedirname, fname)if os.path.isdir(path): continueimgs.append(path) f.write(img2pdf.convert(imgs))將腳本打包成exe
不是所有的電腦都有Python環(huán)境,我們需要將腳本打包成exe方便在任意一臺電腦上使用。使用 PyInstaller 來進(jìn)行腳本的打包
安裝 PyInstallerpip install pyinstaller打包腳本
在腳本所在的路徑的cmd中執(zhí)行以下命令即可
pyinstaller -F yourprogram.py總結(jié)
到此這篇關(guān)于Python實(shí)現(xiàn)圖片合并pdf的方法的文章就介紹到這了,更多相關(guān)Python 圖片合并pdf內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML在語音合成中的應(yīng)用2. 不要在HTML中濫用div3. jscript與vbscript 操作XML元素屬性的代碼4. XML入門的常見問題(三)5. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)6. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別7. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)8. 基于PHP做個圖片防盜鏈9. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)10. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)
