通過Python從PDF中提取圖像

从 PDF 文档中提取图像。使用 Aspose.PDF for Python for .NET 以编程方式修改 PDF 文件

使用 Python 工具从 PDF 文档中提取图像

为了从 PDF 中提取图像,我们将使用 Aspose.PDF for .NET API,这是一款功能丰富、强大且易于使用的文档操作 API,适用于 python-net 平台。打开 NuGet 软件包管理器,搜索 Aspose.pdf 并安装。您也可以使用包管理器控制台中的以下命令。

Python Package Manager Console

pip install aspose-pdf

通過Python從PDF中提取圖像


您需要用於 .NET 庫的 [Aspose.PDF](https://releases.aspose.com/pdf/net)才能在您的環境中嘗試代碼。

  1. 開啟 PDF 文件。
  2. 提取特定圖像。 保存輸出圖像。
  3. 儲存更新的 PDF 檔。

从 PDF 文件中提取图像-Python

此示例代码说明如何从 PDF 中提取图像-Python

    import aspose.pdf as ap 

    input_file = DIR_INPUT + "sample_with_image.pdf"
    output_image = DIR_OUTPUT + "extract_image.jpg"
    # Open document
    document = ap.Document(input_file)

    # Extract a particular image
    xImage = document.pages[2].resources.images[1]
    outputImage = io.FileIO(output_image, "w")

    # Save output image
    xImage.save(outputImage)
    outputImage.close()
    # Save updated PDF file
    document.save(DIR_OUTPUT + "output.pdf")