對於企業來說,將圖片轉換為PDF文件是常見的需求之一。在Python中,通過使用一些特定的庫和模塊,可以很容易地實現這個功能。這篇文章將介紹如何使用Python將圖片轉化為PDF,以及其他相關的實現方法。
一、Python圖片轉PDF
Python的reportlab和fpdf是兩個常用的庫,用於將圖像轉換為PDF文件。這兩個庫的使用方法類似,主要差別在於使用的功能和樣式。
1. Reportlab
from reportlab.pdfgen import canvas
from PIL import Image
def imageToPdf(imagePath, pdfPath):
image = Image.open(imagePath)
(imageWidth, imageHeight) = image.size
pdf = canvas.Canvas(pdfPath, pageSize=(imageWidth, imageHeight))
pdf.drawImage(imagePath, 0, 0)
pdf.save()
這段代碼將打開指定的圖片文件,獲取其大小,創建一個新的PDF文件,並將圖片繪製在PDF中。在運行此腳本之後,您將在同一目錄下獲得一個名為output.pdf的PDF文件,其中包含您轉換的圖像。
2. fpdf
from fpdf import FPDF
from PIL import Image
def imageToPdf(imagePath, pdfPath):
image = Image.open(imagePath)
(imageWidth, imageHeight) = image.size
pdf = FPDF(unit="pt", format=[imageWidth, imageHeight])
pdf.add_page()
pdf.image(imagePath, 0, 0)
pdf.output(pdfPath, "F")
這段代碼是使用fpdf庫實現的類似功能,其中也是打開指定的圖片文件,獲取其大小,創建一個新的PDF文件,並將圖片繪製在PDF中。在運行此腳本之後,您將在同一目錄下獲得一個名為output.pdf的PDF文件,其中包含您轉換的圖像。
二、Python圖片轉PDF如何提高速度
將大量圖像批量轉換並保存為PDF文件可能會花費很長時間,尤其是在處理較大圖像時。下面是一些技巧,可用於提高Python圖片轉PDF的速度。
1. 轉換的圖像應該小於PDF的尺寸
當您將大型圖像轉換為PDF時,最好將其縮小到PDF文件的實際大小。這可以減少處理時間,因為它將減少必要的空間和時間,同時還可以節省計算機內存。
2. 使用多進程或多線程實現並行編程
使用並行編程技術,可同時處理多個轉換作業,使整個處理過程更快。可以使用多進程或多線程實現這項工作。Python的concurrent.futures模塊可用於實現多線程,而multiprocessing模塊可用於實現多進程。
三、Python圖片轉文字
另一種常見的技術是OCR(Optical Character Recognition,光學字元識別),用於將圖像中的文字轉換為可編輯和可搜索的文本。Python中有很多OCR庫可以幫助我們實現這個目標,下面是一些常用的OCR庫:
- tesseract
- pytesseract
- ocrmypdf
這裡以pytesseract為例,它是Google提供的開源OCR庫的Python封裝。要在Python中使用pytesseract首先要安裝tesseract和pytesseract庫。
pip install tesseract
pip install pytesseract
下面是一個使用pytesseract通過OCR技術將圖像文本轉換為可編輯的文本的示例:
import pytesseract
from PIL import Image
filePath = "path/to/image"
outputPath = "path/to/output/txt"
text = pytesseract.image_to_string(Image.open(filePath))
with open(outputPath, mode='w') as f:
f.write(text)
四、Python PDF轉圖片
有時候需要將PDF文件中的頁面轉換為圖像,以便進行編輯,Python也有一些庫可以幫助實現這個目標。
1. pdf2image
pdf2image是一個Python庫,它使用poppler庫將PDF文件中的頁面轉換為PIL圖像對象。以下是pdf2image的使用示例:
!pip install pdf2image
from pdf2image import convert_from_path
pdfPath = "path/to/pdf"
imagePath = "path/to/image"
images = convert_from_path(pdfPath)
for i, image in enumerate(images):
image.save(f"{imagePath}_{i}.jpg")
2. PyMuPDF
PyMuPDF是一個Python庫,它使用MuPDF庫將PDF文件中的頁面轉換為PIL或Numpy圖像對象。以下是PyMuPDF的使用示例:
!pip install PyMuPDF
import fitz
pdfPath = "path/to/pdf"
imagePath = "path/to/image"
pdf = fitz.open(pdfPath)
for i in range(pdf.page_count):
page = pdf.load_page(i)
pix = page.get_pixmap()
pix.save(f"{imagePath}_{i}.jpg")
五、Python圖片轉Word
有時候需要將圖像插入到Word文檔中,以下是如何使用Python將圖像轉換為可插入到Word中的格式的示例:
from docx import Document
from docx.shared import Inches
from PIL import Image
imagePath = "path/to/image"
docPath = "path/to/doc"
document = Document()
document.add_picture(imagePath, width=Inches(6))
document.save(docPath)
六、Python圖片轉二進位
以下是將圖像轉換為二進位字元串的Python示例:
with open("path/to/image", "rb") as f:
binaryString = f.read()
七、Python圖片轉列表
有時候需要將圖像轉換為可以在Python中操作的列表,以下是一個使用numpy庫將圖像轉換為列表的Python示例:
import numpy as np
from PIL import Image
imagePath = "path/to/image"
image = np.asarray(Image.open(imagePath))
imageList = image.tolist()
八、Python圖片轉黑白
以下是Python中如何將彩色圖像轉換為黑白圖像的示例:
from PIL import Image
imagePath = "path/to/image"
image = Image.open(imagePath).convert("L")
image.show()
九、Python圖片轉表格
將圖像轉換為表格可以讓我們在Python中進行數據處理和分析。以下是使用Python將圖像轉換為表格的示例:
import pandas as pd
from PIL import Image
imagePath = "path/to/image"
image = Image.open(imagePath)
data = pd.DataFrame(list(image.getdata()))
table = data.values.reshape(image.size[1], image.size[0])
以上代碼將打開指定的圖片文件,獲取其大小,並創建一個Pandas數據框,其中包含圖像中的像素值。通過重塑數據框,您將得到一個包含表格數據的numpy數組,供您在Python中使用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/287330.html