以原始解析度和格式從PDF 文件中擷取影像
處理PDF 文件時,可以使用原始解析度和格式擷取影像至關重要的。這可確保擷取的影像保留與來源文件相同的品質和完整性。在本文中,我們提出了一種使用 Python 從 PDF 文件中提取圖像而無需重新採樣的解決方案,使您能夠獲得原始格式的高品質圖像。
用於影像擷取的 PyMuPDF
用於 PDF 操作的最流行的 Python 模組之一是 PyMuPDF。該模組提供了一種從 PDF 文件中提取圖像的強大方法,同時保留其原始解析度和格式。以下是使用 PyMuPDF 的程式碼片段:
<code class="python">import fitz # Open the PDF document doc = fitz.open("file.pdf") # Iterate through pages and images for i in range(len(doc)): for img in doc.getPageImageList(i): xref = img[0] # Convert picture object to PNG pix = fitz.Pixmap(doc, xref) if pix.n <p>此程式碼迭代 PDF 文件中的所有頁面和圖像,並將它們提取為 PNG 檔案。它保留了每個影像的原始解析度和格式,確保您獲得高品質的影像。 </p> <p><strong>更新 PyMuPDF 的修改版本</strong></p> <p>如果您使用的是較新版本PyMuPDF 版本(例如 1.19.6),您可能需要稍微修改上面的程式碼。以下程式碼片段反映了必要的變更:</p> <pre class="brush:php;toolbar:false"><code class="python">import os import fitz from tqdm import tqdm # Set working directory workdir = "your_folder" # Process PDF files 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))) # Iterate through pages and images for i in tqdm(range(len(doc)), desc="pages"): for img in tqdm(doc.get_page_images(i), desc="page_images"): xref = img[0] # Extract the image and save it as PNG 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))) # Print a completion message print("Done!")</code>
此修改後的程式碼使用 get_page_images() 方法取得映像並將其儲存為指定工作目錄中的 PNG 檔案。
以上是如何在Python中以本機解析度和格式從PDF文件中擷取影像?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ArraySareAryallyMoremory-Moremory-forigationDataDatueTotheIrfixed-SizenatureAntatureAntatureAndirectMemoryAccess.1)arraysStorelelementsInAcontiguxufulock,ReducingOveringOverheadHeadefromenterSormetormetAdata.2)列表,通常

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Python列表可以存儲不同類型的數據。示例列表包含整數、字符串、浮點數、布爾值、嵌套列表和字典。列表的靈活性在數據處理和原型設計中很有價值,但需謹慎使用以確保代碼的可讀性和可維護性。

Pythondoesnothavebuilt-inarrays;usethearraymoduleformemory-efficienthomogeneousdatastorage,whilelistsareversatileformixeddatatypes.Arraysareefficientforlargedatasetsofthesametype,whereaslistsofferflexibilityandareeasiertouseformixedorsmallerdatasets.

theSostCommonlyusedModuleForCreatingArraysInpyThonisnumpy.1)NumpyProvidEseffitedToolsForarrayOperations,Idealfornumericaldata.2)arraysCanbeCreatedDusingsnp.Array()for1dand2Structures.3)

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。