使用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中文網其他相關文章!