使用 Python 从 PDF 文件中提取图像

当您需要复用嵌入的图形、归档图像资源或在 PDF 之外处理图像内容时,请使用此页面。

  1. 使用加载源 PDF ap.Document(infile).
  2. 选择目标页面和图像资源索引。
  3. 将图像对象保存到输出流。
import aspose.pdf as ap
from io import FileIO


def extract_image(infile, outfile):
    document = ap.Document(infile)
    x_image = document.pages[1].resources.images[1]
    with FileIO(outfile, "wb") as output_image:
        x_image.save(output_image)

从 PDF 的特定区域提取图像

此示例提取位于 PDF 页面上指定矩形区域内的图像,并将它们另存为单独的文件。

  1. 加载源 PDF。
  2. 创建 ImagePlacementAbsorber 并在目标页面上接受它。
  3. 定义目标矩形。
  4. 遍历图像放置项,并检查每个图像的边界是否适合该区域。
  5. 将匹配的图像保存到输出文件。
import aspose.pdf as ap
from io import FileIO


def extract_image_from_specific_region(infile, outfile):
    document = ap.Document(infile)
    rectangle = ap.Rectangle(0, 0, 590, 590, True)
    absorber = ap.ImagePlacementAbsorber()
    document.pages[1].accept(absorber)

    index = 1
    for image_placement in absorber.image_placements:
        point1 = ap.Point(image_placement.rectangle.llx, image_placement.rectangle.lly)
        point2 = ap.Point(image_placement.rectangle.urx, image_placement.rectangle.ury)

        if rectangle.contains(point1, True) and rectangle.contains(point2, True):
            with FileIO(outfile.replace("index", str(index)), "wb") as output_image:
                image_placement.image.save(output_image)
            index += 1

相关图像主题