詳解python實現(xiàn)多張多格式圖片轉(zhuǎn)PDF并打包成exe
轉(zhuǎn)PDF初始代碼
從文件夾中讀取圖片數(shù)據(jù),然后將他們保存為PDF格式。
不長,大概10行代碼。
from PIL import Image from os import * def PictureToPDF(picture_path, name): pictures = [] picture_file = listdir(picture_path) for file in picture_file: picture = Image.open(picture_path + '\\' + file) pictures.append(picture) pictures[0].save(picture_path + '\\' + name + '.pdf', "PDF", save_all=True, append_images=pictures) PictureToPDF(r'C:\Users\Yezi\Desktop\Temp', 'test')
但這個有缺陷,部分圖片轉(zhuǎn)換過程有漏洞,經(jīng)過多次實驗,發(fā)現(xiàn)部分圖片會出現(xiàn)這種錯誤:
ValueError: cannot save mode RGBA
我查詢了許多資料并測試了很久,發(fā)現(xiàn)是圖片格式色彩空間問題。
平常圖片的顏色是RGB三種顏色,red紅,green綠,blue藍,但是png圖片使用的是RGBA色彩空間,除了平常的RGB,多出來的A是alpha,是不透明度參數(shù),有利于合成和融合圖片。
但是在轉(zhuǎn)PDF的過程,就不知道A怎么辦,所以會出問題。
因此有下面的修改版,去掉A。
轉(zhuǎn)PDF最終代碼
與原來的相比,增加了一個判斷語句,如果圖片格式是png類型,那么就去掉里面的A。
from PIL import Image from os import * def PictureToPDF(picture_path, name): pictures = [] picture_file = listdir(picture_path) for file in picture_file: picture = Image.open(picture_path + '\\' + file) if 'png' in file: r, g, b, a = picture.split() picture = Image.merge("RGB", (r, g, b)) pictures.append(picture) pictures[0].save(picture_path + '\\' + name + '.pdf', "PDF", save_all=True, append_images=pictures) PictureToPDF(r'C:\Users\Yezi\Desktop\Temp', 'test')
GUI界面設(shè)計代碼
然后是加上GUI界面設(shè)計,這個問題就比較多,首先UI長這個樣子:
p>整個頁面的代碼是這樣的:
window = tkinter.Tk() main = tkinter.Frame(window) main.grid(row=0, column=0) tkinter.Label(window, text='圖片路徑:').grid(row=0, column=0) pathEntry = tkinter.Entry(window, width=32) pathEntry.grid(row=0, column=1, columnspan=10) tkinter.Label(window, text='PDF名字:').grid(row=1, column=0) nameEntry = tkinter.Entry(window, width=32) nameEntry.grid(row=1, column=1, columnspan=10) tkinter.Button(window, text='開始轉(zhuǎn)換', command=convert).grid(row=2, column=4) window.mainloop()
代碼不長,我懶得解釋了問題在于捕獲路徑的時候有一個問題,正常windows文件路徑是這樣的:
C:\Users\Yezi\Desktop\Python\PictureToPDF
但是這樣是不能識別的,,因為 \ 會形成轉(zhuǎn)義字符,所以我們在前面加了個r ,表示不用轉(zhuǎn)義字符,但是在弄成可執(zhí)行文件的時候,路徑是個參數(shù),不能加r了,因此,我寫了個函數(shù)把路徑這個字符串變成可以識別的路徑,這樣的路徑可以識別:
C:/Users/Yezi/Desktop/Python/PictureToPDF
或者是這樣的:
C:\\Users\\Yezi\\Desktop\\Python\\PictureToPDF
但是前面的比較好轉(zhuǎn),所以我就讓它變成 / 這個:
def convert(): char = "\\" path = pathEntry.get() for it in path: if it in char: path = path.replace(it, '\\')
打包成可執(zhí)行文件
win+R輸入cmd打開命令提示行。
pip安裝這個包。
pip install Pyinstaller
準備好圖標和代碼文件。
進入相應文件夾。
用這個命令(后面的圖標名和文件名需要進行相應的修改哦)。
pyinstaller -F -i ./alpaca.ico PictureToPDF.py
然后等待……
完成之后到dist文件夾中找到這個程序,即可。
完整代碼
import os import PIL import tkinter def PictureToPDF(picture_path, name): pictures = [] picture_file = os.listdir(picture_path) for file in picture_file: picture = PIL.Image.open(picture_path + '\\' + file) if 'png' in file: r, g, b, a = picture.split() picture = PIL.Image.merge("RGB", (r, g, b)) pictures.append(picture) pictures[0].save(picture_path + '\\' + name + '.pdf', "PDF", save_all=True, append_images=pictures) def GUI(): def convert(): char = "\\" path = pathEntry.get() for it in path: if it in char: path = path.replace(it, '\\') PictureToPDF(path, nameEntry.get()) window = tkinter.Tk() main = tkinter.Frame(window) main.grid(row=0, column=0) tkinter.Label(window, text='圖片路徑:').grid(row=0, column=0) pathEntry = tkinter.Entry(window, width=32) pathEntry.grid(row=0, column=1, columnspan=10) tkinter.Label(window, text='PDF名字:').grid(row=1, column=0) nameEntry = tkinter.Entry(window, width=32) nameEntry.grid(row=1, column=1, columnspan=10) tkinter.Button(window, text='開始轉(zhuǎn)換', command=convert).grid(row=2, column=4) window.mainloop() GUI()
附錄
os模塊
os是用來處理文件和目錄的。
os.listdir(path)
os.listdir(path)是用來返回path指定的文件夾包含的文件或文件夾的名字的列表。
str.lower()
str.lower()會返回將字符串中所有大寫字符轉(zhuǎn)換為小寫后生成的字符串。
PIL模塊
好像就是pillow。
Image.open(path+picture's full name)
Image.open()用來讀取一張照片。
以上就是詳解python實現(xiàn)多張多格式圖片轉(zhuǎn)PDF并打包成exe的詳細內(nèi)容,更多關(guān)于python圖片格式轉(zhuǎn)PDF打包exe的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
查找python項目依賴并生成requirements.txt的方法
今天小編就為大家分享一篇查找python項目依賴并生成requirements.txt的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python 數(shù)據(jù)可視化pyecharts的使用詳解
這篇文章主要介紹了Python 數(shù)據(jù)可視化pyecharts的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06Python的加密模塊之hashlib 與 base64詳解及常用加密方法
我們來學習一下 Python 中的加密模塊,加密模塊在工作中被廣泛應用,比如數(shù)據(jù)的傳入 不希望被捕獲,通過把數(shù)據(jù)加密。這樣即使被捕獲也無法獲取到數(shù)據(jù)的真實信息,今天我們就來學習一下關(guān)于加密的方法,感興趣的朋友跟隨小編一起看看吧2023-02-02如何實現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片)
這篇文章主要介紹了如何實現(xiàn)在jupyter notebook中播放視頻(不停地展示圖片),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python中str is not callable問題詳解及解決辦法
這篇文章主要介紹了Python中str is not callable問題詳解及解決辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02