提取图像
from ironpdf import *

# Open a 128 bit encrypted PDF
pdf = PdfDocument.FromFile("encrypted.pdf", "password")

# Get all images
all_images = pdf.ExtractAllImages()

for i, image in enumerate(all_images):
    image.SaveAs(f"output_image_{i}.png")

# Or even find the precise text and images for each page in the document
for index in range(pdf.PageCount):
    page_number = index + 1
    images = pdf.ExtractBitmapsFromPage(index)
pip install ironpdf
提取图像

提取图像

IronPDF for Python 可以提取 PDF 文档中嵌入的所有图像 - 无论是作为已保存的图像文件还是内存中的位图对象 - 同时能够定位特定页面。

开始

使用ExtractBitmapsFromPage。

理解代码

  • PdfDocument.FromFile(path, password):打开PDF文件。第二参数是解密密码; 对于未加密文件可以省略它。
  • ExtractAllImages():返回文档中所有页面上的所有图像的列表。 列表中的每个图像都是一个具有可将其写入磁盘的SaveAs方法的对象。
  • SaveAs(path):将提取的图像保存到指定的文件路径。 文件格式由扩展名确定(.jpg,等等)。
  • AnyBitmap对象列表。 当需要在内存中处理图像数据而不是写入文件时,使用此方法。

迭代模式

该示例使用ExtractAllImages(),为每个图像文件名分配唯一的顺序索引,以防止当多个图像从同一文档中提取时被覆盖。

了解如何使用 IronPDF for Python 处理 PDF 图像!

准备开始了吗?

版本:2026.11刚刚发布