最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、說明

如何使得圖像轉化成pdf文件, 想要將一個或多個圖像轉換為 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將用于將圖像轉換為PDF文件。然后,PyPDF2 可用于將多個 PDF 合并為一個 PDF 文件。枕頭是一個圖像處理庫;它提供了轉換所需的附加功能。

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

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

三、img2pdf官方文檔

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

pip install img2pdf

以下是實現(xiàn):圖像可以使用img2pdf模塊提供的img2pdf.convert()函數(shù)轉換為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ù)其名稱進行排序,因此它們應命名為類似 .google.colabpage1.png, page2.png, ..., page9.png

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

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

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

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

五、將圖像轉換為 PDF

然后可以使用 for 循環(huá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")

六、合并文檔

將圖像轉換為 PDF 文件后,可以獨立使用并使用 下載它們,也可以將它們合并在一起。要將文件合并在一起,請?zhí)崛?PDF 文件列表并按頁碼對其進行排序。files.download('filename.pdf')

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

同樣,如果有超過 9 個圖像或 PDF,它們應按各自的順序存儲在列表中。

對象可用于將每個 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

到此這篇關于Python中將圖像轉換為PDF的方法實現(xiàn)的文章就介紹到這了,更多相關Python圖像轉換為PDF內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • pandas to_excel 添加顏色操作

    pandas to_excel 添加顏色操作

    這篇文章主要介紹了pandas to_excel 添加顏色操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解

    cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解

    getStructuringElement()函數(shù)可用于構造一個特定大小和形狀的結構元素,用于圖像形態(tài)學處理,這篇文章主要介紹了cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解的相關資料,需要的朋友可以參考下
    2022-12-12
  • Python中利用aiohttp制作異步爬蟲及簡單應用

    Python中利用aiohttp制作異步爬蟲及簡單應用

    asyncio可以實現(xiàn)單線程并發(fā)IO操作,是Python中常用的異步處理模塊。這篇文章主要介紹了Python中利用aiohttp制作異步爬蟲的相關知識,需要的朋友可以參考下
    2018-11-11
  • python實現(xiàn)爬山算法的思路詳解

    python實現(xiàn)爬山算法的思路詳解

    爬山算法會收斂到局部最優(yōu),解決辦法是初始值在定義域上隨機取亂數(shù)100次,總不可能100次都那么倒霉。這篇文章主要介紹了python實現(xiàn)爬山算法的思路詳解,需要的朋友可以參考下
    2019-04-04
  • python實現(xiàn)的重啟關機程序實例

    python實現(xiàn)的重啟關機程序實例

    這篇文章主要介紹了python實現(xiàn)的重啟關機程序,有不錯的借鑒價值,需要的朋友可以參考下
    2014-08-08
  • python學習開發(fā)mock接口

    python學習開發(fā)mock接口

    這篇文章主要為大家詳細介紹了python學習開發(fā)mock接口的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python學習實操案例(五)

    python學習實操案例(五)

    這篇文章主要介紹了pyth學習實操案例,主要分享的小練習有我的咖啡館你做主、顯示2019中超聯(lián)賽中前五名排行、模擬手機通訊錄,適合初學者,需要的小伙伴可以參考一下
    2022-02-02
  • Python判斷字符串是否為字母或者數(shù)字(浮點數(shù))的多種方法

    Python判斷字符串是否為字母或者數(shù)字(浮點數(shù))的多種方法

    本文給大家?guī)砣N方法基于Python判斷字符串是否為字母或者數(shù)字(浮點數(shù)),非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • 利用Rust實現(xiàn)Python加速的技巧分享

    利用Rust實現(xiàn)Python加速的技巧分享

    這篇文章主要想來和大家一起探討一下關于使用Rust對Python計算進行加速的問題,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-09-09
  • matlab xlabel位置的設置方式

    matlab xlabel位置的設置方式

    這篇文章主要介紹了matlab xlabel位置的設置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評論

宁远县| 台南市| 偃师市| 盘锦市| 盐城市| 门头沟区| 礼泉县| 韩城市| 许昌县| 岳阳县| 平湖市| 砀山县| 通州区| 双牌县| 河北省| 建湖县| 大庆市| 连平县| 富源县| 福泉市| 南陵县| 海淀区| 望都县| 台江县| 阿图什市| 安平县| 佛冈县| 黑龙江省| 盐城市| 郓城县| 岳西县| 大兴区| 平山县| 桂林市| 新密市| 小金县| 大同县| 芦山县| 东乌珠穆沁旗| 绍兴市| 屯昌县|