如何使用 Python 從 PDF 中提取圖像,同時保留其原始解析度?

DDD
發布: 2024-10-22 07:52:30
原創
582 人瀏覽過

How Can You Extract Images from PDFs Using Python While Preserving Their Original Resolution?

使用Python 從PDF 中提取圖像而無需重新採樣

高效地從PDF 文件中提取所有圖像,同時保留其原始分辨速率和格式而無需重新取樣,您可以使用PyMuPDF 模組。此模組為影像擷取提供了有效的解決方案,將影像輸出為PNG檔案。

使用PyMuPDF:

<code class="python">import fitz

# Open the PDF document
doc = fitz.open("file.pdf")

# Iterate through the pages
for i in range(len(doc)):
    # Extract images from the current page
    for img in doc.getPageImageList(i):
        # Retrieve the image's XREF and create a Pixmap
        xref = img[0]
        pix = fitz.Pixmap(doc, xref)

        # Check if the image is grayscale or RGB
        if pix.n < 5:
            # Save the image in PNG format
            pix.writePNG("p%s-%s.png" % (i, xref))

        # If the image is CMYK, convert it to RGB and save
        else:
            pix1 = fitz.Pixmap(fitz.csRGB, pix)
            pix1.writePNG("p%s-%s.png" % (i, xref))
            pix1 = None

        # Release the Pixmaps
        pix = None</code>
登入後複製

增強功能:

支援fitz 1.19.6 的腳本更新版本:

<code class="python">import os
import fitz
from tqdm import tqdm

# Specify the work directory
workdir = "your_folder"

# Iterate through the PDFs in the directory
for each_path in os.listdir(workdir):
    if ".pdf" in each_path:
        # Open the PDF document
        doc = fitz.Document(os.path.join(workdir, each_path))

        for i in tqdm(range(len(doc)), desc="pages"):
            for img in tqdm(doc.get_page_images(i), desc="page_images"):
                # Extract the image and save as PNG
                xref = img[0]
                image = doc.extract_image(xref)
                pix = fitz.Pixmap(doc, xref)
                pix.save(os.path.join(workdir, "%s_p%s-%s.png" % (each_path[:-4], i, xref)))</code>
登入後複製

此增強的腳本提供進度條以增加可見性,並使用一致的檔案命名約定保存提取的影像。

以上是如何使用 Python 從 PDF 中提取圖像,同時保留其原始解析度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!