亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python中將圖像轉(zhuǎn)換為PDF的方法實(shí)現(xiàn)

 更新時(shí)間:2023年08月29日 11:06:41   作者:無水先生  
本文主要介紹了Python中將圖像轉(zhuǎn)換為PDF的方法實(shí)現(xiàn),主要使用img2pdf和PyPDF2軟件包,具有一定的參考價(jià)值,感興趣的可以了解一下

一、說明

如何使得圖像轉(zhuǎn)化成pdf文件, 想要將一個(gè)或多個(gè)圖像轉(zhuǎn)換為 PDF 文檔?看看img2pdf和PyPDF2軟件包就是您的最佳選擇。

二、需要哪些程序包?

首先,您只需要一個(gè) 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 

第一個(gè)包img2pdf將用于將圖像轉(zhuǎn)換為PDF文件。然后,PyPDF2 可用于將多個(gè) PDF 合并為一個(gè) PDF 文件。枕頭是一個(gè)圖像處理庫;它提供了轉(zhuǎn)換所需的附加功能。

現(xiàn)在可以導(dǎo)入這些包以及 和 。osgoogle.colab

# required libraries
import os
import img2pdf
import PyPDF2
from PIL import Image
from google.colab import files

三、img2pdf官方文檔

img2pdf是一個(gè)開源的Python包,用于將圖像轉(zhuǎn)換為pdf格式。它包括另一個(gè)模塊枕頭,也可用于增強(qiáng)圖像(亮度,對(duì)比度和其他東西) 使用此命令安裝軟件包

pip install img2pdf

以下是實(shí)現(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

四、準(zhǔn)備映像

在編寫更多代碼之前,了解每個(gè)圖像的文件位置非常重要。為了盡可能簡化此操作,可以在 Colab 環(huán)境中創(chuàng)建一個(gè)新文件夾:

mkdir images

所有圖像都需要使用 提供的上傳程序同時(shí)上傳到此位置。這些文件將根據(jù)其名稱進(jìn)行排序,因此它們應(yīng)命名為類似 .google.colabpage1.png, page2.png, ..., page9.png

os.chdir("images")
files.upload()

將圖像存儲(chǔ)在已知的文件位置后,其名稱可以存儲(chǔ)在列表中。

imgs = os.listdir()
imgs.sort()

如果圖像超過 9 個(gè),則此方法可能會(huì)出現(xiàn)問題,應(yīng)按文件所需的順序創(chuàng)建列表。

五、將圖像轉(zhuǎn)換為 PDF

然后可以使用 for 循環(huán)遍歷每個(gè)圖像,將其轉(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 文件后,可以獨(dú)立使用并使用 下載它們,也可以將它們合并在一起。要將文件合并在一起,請?zhí)崛?PDF 文件列表并按頁碼對(duì)其進(jìn)行排序。files.download('filename.pdf')

os.chdir("../pdfs")
pdfs = os.listdir()

同樣,如果有超過 9 個(gè)圖像或 PDF,它們應(yīng)按各自的順序存儲(chǔ)在列表中。

對(duì)象可用于將每個(gè) PDF 連接成單個(gè)文件。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將按其各自名稱的順序包含每個(gè)圖像。

七、完整程序

完整的代碼可以在下面找到。它是高度可定制的,以滿足大多數(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的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python圖像轉(zhuǎn)換為PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Python畫股票的K線圖的方法步驟

    使用Python畫股票的K線圖的方法步驟

    這篇文章主要介紹了使用Python畫股票的K線圖的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • python實(shí)現(xiàn)一次創(chuàng)建多級(jí)目錄的方法

    python實(shí)現(xiàn)一次創(chuàng)建多級(jí)目錄的方法

    這篇文章主要介紹了python實(shí)現(xiàn)一次創(chuàng)建多級(jí)目錄的方法,涉及Python中os模塊makedirs方法的使用技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-05-05
  • Python-openpyxl表格讀取寫入的案例詳解

    Python-openpyxl表格讀取寫入的案例詳解

    這篇文章主要介紹了Python-openpyxl表格讀取寫入的案例分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python用于url解碼和中文解析的小腳本(python url decoder)

    python用于url解碼和中文解析的小腳本(python url decoder)

    這篇文章主要介紹了python用于url解碼和中文解析的代碼,需要的朋友可以參考下
    2013-08-08
  • wxPython實(shí)現(xiàn)繪圖小例子

    wxPython實(shí)現(xiàn)繪圖小例子

    這篇文章主要為大家詳細(xì)介紹了wxPython實(shí)現(xiàn)繪圖小例子,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • python中array數(shù)組添加一行或一列數(shù)據(jù)的具體實(shí)現(xiàn)

    python中array數(shù)組添加一行或一列數(shù)據(jù)的具體實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于python中array數(shù)組添加一行或一列數(shù)據(jù)的具體實(shí)現(xiàn),最近經(jīng)常使用到數(shù)組方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Python模糊查詢本地文件夾去除文件后綴的實(shí)例(7行代碼)

    Python模糊查詢本地文件夾去除文件后綴的實(shí)例(7行代碼)

    下面小編就為大家?guī)硪黄狿ython模糊查詢本地文件夾去除文件后綴的實(shí)例(7行代碼) 。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • pytorch加載的cifar10數(shù)據(jù)集過程詳解

    pytorch加載的cifar10數(shù)據(jù)集過程詳解

    這篇文章主要介紹了pytorch加載的cifar10數(shù)據(jù)集,到底有沒有經(jīng)過歸一化,本文對(duì)這一問題給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • Pandas庫中ffill函數(shù)的具體使用

    Pandas庫中ffill函數(shù)的具體使用

    ffill(forward fill)是Pandas庫中DataFrame和Series對(duì)象的一個(gè)函數(shù),用于填充缺失值,本文主要介紹了Pandas庫中ffill函數(shù)的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • 使用C#配合ArcGIS Engine進(jìn)行地理信息系統(tǒng)開發(fā)

    使用C#配合ArcGIS Engine進(jìn)行地理信息系統(tǒng)開發(fā)

    這篇文章主要介紹了使用C#配合ArcGIS Engine進(jìn)行地理信息系統(tǒng)開發(fā),ArcGIS Engine是Windows系統(tǒng)上可以讓程序員創(chuàng)建自定義的GIS桌面程序,需要的朋友可以參考下
    2016-02-02

最新評(píng)論