Python中將圖像轉(zhuǎn)換為PDF的方法實現(xiàn)
一、說明
如何使得圖像轉(zhuǎn)化成pdf文件, 想要將一個或多個圖像轉(zhuǎn)換為 PDF 文檔?看看img2pdf和PyPDF2軟件包就是您的最佳選擇。
二、需要哪些程序包?
首先,您只需要一個 Python 環(huán)境,最好是 3.10 或更高版本。本教程中的代碼是在使用 Python 3.10.12 的 Google Colab 環(huán)境中執(zhí)行的。
第一步是確保在 Python 環(huán)境中安裝以下包:
- img2pdf
- PyPDF2
- Pillow
Pip 可用于在 Colab 中安裝這些軟件包:
pip install img2pdf PyPDF2 Pillow
第一個包img2pdf將用于將圖像轉(zhuǎn)換為PDF文件。然后,PyPDF2 可用于將多個 PDF 合并為一個 PDF 文件。枕頭是一個圖像處理庫;它提供了轉(zhuǎn)換所需的附加功能。
現(xiàn)在可以導入這些包以及 和 。osgoogle.colab
# required libraries import os import img2pdf import PyPDF2 from PIL import Image from google.colab import files
三、img2pdf官方文檔
img2pdf是一個開源的Python包,用于將圖像轉(zhuǎn)換為pdf格式。它包括另一個模塊枕頭,也可用于增強圖像(亮度,對比度和其他東西) 使用此命令安裝軟件包
pip install img2pdf
以下是實現(xiàn):圖像可以使用img2pdf模塊提供的img2pdf.convert()函數(shù)轉(zhuǎn)換為pdf字節(jié),然后在wb模式下打開pdf文件并用字節(jié)寫入。
python
# Python3 program to convert image to pdf
# using img2pdf library # importing necessary libraries import img2pdf from PIL import Image import os # storing image path img_path = "C:/Users/Admin/Desktop/GfG_images/do_nawab.png" # storing pdf path pdf_path = "C:/Users/Admin/Desktop/GfG_images/file.pdf" # opening image image = Image.open(img_path) # converting into chunks using img2pdf pdf_bytes = img2pdf.convert(image.filename) # opening or creating pdf file file = open(pdf_path, "wb") # writing pdf files with chunks file.write(pdf_bytes) # closing image file image.close() # closing pdf file file.close() # output print("Successfully made pdf file")
輸出:
Successfully made pdf file
四、準備映像
在編寫更多代碼之前,了解每個圖像的文件位置非常重要。為了盡可能簡化此操作,可以在 Colab 環(huán)境中創(chuàng)建一個新文件夾:
mkdir images
所有圖像都需要使用 提供的上傳程序同時上傳到此位置。這些文件將根據(jù)其名稱進行排序,因此它們應(yīng)命名為類似 .google.colabpage1.png, page2.png, ..., page9.png
os.chdir("images") files.upload()
將圖像存儲在已知的文件位置后,其名稱可以存儲在列表中。
imgs = os.listdir() imgs.sort()
如果圖像超過 9 個,則此方法可能會出現(xiàn)問題,應(yīng)按文件所需的順序創(chuàng)建列表。
五、將圖像轉(zhuǎn)換為 PDF
然后可以使用 for 循環(huán)遍歷每個圖像,將其轉(zhuǎn)換為 PDF,并將其寫入名為 的新文件夾。pdfs
# create a folder called pdfs os.mkdir("../pdfs") # loop over each image for ind, img in enumerate(imgs): # open each image with Image.open(img) as image: # convert the image to a PDF pdf = img2pdf.convert(image.filename) # write the PDF to its final destination with open(f"../pdfs/pdf{ind+1}.pdf", "wb") as file: file.write(pdf) print(f"Converted {img} to pdf{ind+1}.pdf")
六、合并文檔
將圖像轉(zhuǎn)換為 PDF 文件后,可以獨立使用并使用 下載它們,也可以將它們合并在一起。要將文件合并在一起,請?zhí)崛?PDF 文件列表并按頁碼對其進行排序。files.download('filename.pdf')
os.chdir("../pdfs") pdfs = os.listdir()
同樣,如果有超過 9 個圖像或 PDF,它們應(yīng)按各自的順序存儲在列表中。
對象可用于將每個 PDF 連接成單個文件。PdfMerger
pdfMerge = PyPDF2.PdfMerger() # loop through each pdf page for pdf in pdfs: # open each pdf with open(pdf, 'rb') as pdfFile: # merge each file pdfMerge.append(PyPDF2.PdfReader(pdfFile)) # write the merged pdf pdfMerge.write('merged.pdf') # download the final pdf files.download('merged.pdf')
最終合并的PDF將按其各自名稱的順序包含每個圖像。
七、完整程序
完整的代碼可以在下面找到。它是高度可定制的,以滿足大多數(shù)用例。
pip install img2pdf PyPDF2 Pillow mkdir images # required libraries import os import img2pdf import PyPDF2 from PIL import Image from google.colab import files os.chdir("images") files.upload() imgs = os.listdir() # create a folder called pdfs os.mkdir("../pdfs") # loop over each image for ind, img in enumerate(imgs): # open each image with Image.open(img) as image: # convert the image to a PDF pdf = img2pdf.convert(image.filename) # write the PDF to its final destination with open(f"../pdfs/pdf{ind+1}.pdf", "wb") as file: file.write(pdf) print(f"Converted {img} to pdf{ind+1}.pdf") os.chdir("../pdfs") pdfs = os.listdir() pdfs.sort() pdfMerge = PyPDF2.PdfMerger() # loop through each pdf page for pdf in pdfs: # open each pdf with open(pdf, 'rb') as pdfFile: # merge each file pdfMerge.append(PyPDF2.PdfReader(pdfFile)) # write the merged pdf pdfMerge.write('merged.pdf') # download the final pdf files.download('merged.pdf')
八、引用
https://www.geeksforgeeks.org/python-convert-image-to-pdf-using-img2pdf-module/
Merging PDFs with Python | Python-bloggers
到此這篇關(guān)于Python中將圖像轉(zhuǎn)換為PDF的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)Python圖像轉(zhuǎn)換為PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)一次創(chuàng)建多級目錄的方法
這篇文章主要介紹了python實現(xiàn)一次創(chuàng)建多級目錄的方法,涉及Python中os模塊makedirs方法的使用技巧,非常簡單實用,需要的朋友可以參考下2015-05-05python用于url解碼和中文解析的小腳本(python url decoder)
這篇文章主要介紹了python用于url解碼和中文解析的代碼,需要的朋友可以參考下2013-08-08python中array數(shù)組添加一行或一列數(shù)據(jù)的具體實現(xiàn)
這篇文章主要給大家介紹了關(guān)于python中array數(shù)組添加一行或一列數(shù)據(jù)的具體實現(xiàn),最近經(jīng)常使用到數(shù)組方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-09-09Python模糊查詢本地文件夾去除文件后綴的實例(7行代碼)
下面小編就為大家?guī)硪黄狿ython模糊查詢本地文件夾去除文件后綴的實例(7行代碼) 。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11pytorch加載的cifar10數(shù)據(jù)集過程詳解
這篇文章主要介紹了pytorch加載的cifar10數(shù)據(jù)集,到底有沒有經(jīng)過歸一化,本文對這一問題給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11使用C#配合ArcGIS Engine進行地理信息系統(tǒng)開發(fā)
這篇文章主要介紹了使用C#配合ArcGIS Engine進行地理信息系統(tǒng)開發(fā),ArcGIS Engine是Windows系統(tǒng)上可以讓程序員創(chuàng)建自定義的GIS桌面程序,需要的朋友可以參考下2016-02-02