Python 从PDF中提取图片和图片信息(坐标、宽度和高度等)

目录

使用工具

Python从PDF的特定页面中提取图片

Python从PDF文档中提取图片

Python从PDF中提取图片的坐标、宽度和高度等信息


PDF文件作为一种广泛使用的电子文档格式,不仅包含文字信息,还可能包含各种图片、图表等视觉元素。在某些场景下,我们可能需要从PDF文件中提取这些图片,用于其他用途,比如插入到演示文稿中,或者进行进一步的编辑和处理。手动从PDF中提取图片是一项耗时的工作,尤其是当需要处理大量PDF文档时。而使用Python自动化这一过程,可以大幅节省时间和精力。这篇博客将探讨如何使用Python从PDF中提取图片以及图片的相关信息如坐标、宽度和高度等

使用工具

本文使用的是Spire.PDF for Python库来实现从PDF中提取图片和图片信息。

你可以通过在终端运行以下命令来从PyPI安装Spire.PDF for Python:

pip install Spire.PDF

Python从PDF的特定页面中提取图片

要从PDF的特定页面中提取图片,首先需要使用PdfDocument.Pages[page_index]属性访问目标页面。之后,使用PdfImageHelper.GetImagesInfo(page) 方法获取该页面上的图片信息。最后使用PdfImageInfo.Image.Save() 方法将每个图片保存为独立的图片文件。具体步骤如下:

  • 创建 PdfDocument 类的实例并使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。
  • 使用PdfDocument.Pages[page_index]属性访问目标页面,这里的page_index表示页面的索引,从0开始计数。
  • 创建 PdfImageHelper 实例。
  • 使用 PdfImageHelper.GetImagesInfo(page) 方法获取目标页面中的图片信息。
  • 循环遍历获取结果,使用 PdfImageInfo.Image.Save() 方法将每张图片保存为独立的图片文件。
from spire.pdf.common import *
from spire.pdf import *
import os

def extract_images_from_pdf_page(pdf_path, page_index, output_dir):
    """
    从 PDF 文件的指定页面中提取图片,并将其保存到指定的输出目录中。
    
    参数:
        pdf_path (str): PDF 文件的路径。
        page_index (int): 要提取图片的页面的索引值。
        output_dir (str): 输出图片文件的目录。
    """
    # 创建 PdfDocument 实例并加载 PDF 文件
    doc = PdfDocument()
    doc.LoadFromFile(pdf_path)

    # 获取需要提取图片的目标页面
    page = doc.Pages[page_index]

    # 创建 PdfImageHelper 实例
    image_helper = PdfImageHelper()

    # 获取目标页面的图片信息
    image_infos = image_helper.GetImagesInfo(page)

    image_count = 1
    # 提取并保存图片
    for image_index in range(len(image_infos)):
        # 指定输出文件名
        output_file = os.path.join(output_dir, f"Image-{image_count}.png")
        # 将图片保存为图片文件
        image_infos[image_index].Image.Save(output_file)
        image_count += 1

    doc.Close()

# 使用示例
extract_images_from_pdf_page("示例.pdf", 1, "C:/Users/Administrator/Desktop/图片")

Python从PDF文档中提取图片

要从整个PDF文档中提取图片,只需要循环遍历文档中的页面,然后重复上面的步骤,从每个页面上提取图片信息,最后将图片保存为独立的图片文件即可。具体步骤如下:

  • 创建 PdfDocument 实例并使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。
  • 创建 PdfImageHelper 实例。
  • 循环遍历文档中的页面。
  • 使用 PdfImageHelper.GetImagesInfo(page) 方法获取每个页面中的图片信息。
  • 遍历获取结果,使用 PdfImageInfo.Image.Save() 方法将每张图片保存为图片文件。
from spire.pdf.common import *
from spire.pdf import *

def extract_images_from_pdf(pdf_path, output_dir):
    """
    从 PDF 文件中提取所有图片,并将其保存到指定的输出目录中。
    
    参数:
        pdf_path (str): 输入 PDF 文件的路径。
        output_dir (str): 输出图片文件的目录。
    """
    # 创建 PdfDocument 实例并加载 PDF 文件
    doc = PdfDocument()
    doc.LoadFromFile(pdf_path)

    # 创建 PdfImageHelper 实例
    image_helper = PdfImageHelper()

    image_count = 1
    # 循环遍历每个页面
    for page_index in range(doc.Pages.Count):
        page = doc.Pages[page_index]
        # 获取页面的图片信息
        image_infos = image_helper.GetImagesInfo(page)

        # 提取并保存图片
        for image_index in range(len(image_infos)):
            # 指定输出文件名
            output_file = os.path.join(output_dir, f"Image-{image_count}.png")
            # 将图片保存为图片文件
            image_infos[image_index].Image.Save(output_file)
            image_count += 1

    doc.Close()

# 使用示例
extract_images_from_pdf("示例.pdf", "C:/Users/Administrator/Desktop/图片")

Python从PDF中提取图片的坐标、宽度和高度等信息

要提取 PDF 文件中图片的信息,例如位置(X和Y坐标)、宽度和高度,可以使用 PdfImageInfo.Bounds.XPdfImageInfo.Bounds.YPdfImageInfo.Bounds.WidthPdfImageInfo.Bounds.Height 属性。具体步骤如下:

  • 创建 PdfDocument 实例并使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。
  • 创建 PdfImageHelper 实例。
  • 循环遍历文档中的页面。
  • 使用 PdfImageHelper.GetImagesInfo(page) 方法获取每个页面中的图片信息。
  • 遍历获取结果,使用 PdfImageInfo.Bounds.XPdfImageInfo.Bounds.YPdfImageInfo.Bounds.WidthPdfImageInfo.Bounds.Height 属性获取图片的坐标、宽度和高度。
from spire.pdf.common import *
from spire.pdf import *

def print_pdf_image_info(pdf_path):
    """
    打印 PDF 文件中图片的坐标、宽度和高度。
    
    参数:
        pdf_path (str): 输入 PDF 文件的路径。
    """
    # 创建 PdfDocument 实例并加载 PDF 文件
    doc = PdfDocument()
    doc.LoadFromFile(pdf_path)

    # 创建 PdfImageHelper 实例
    image_helper = PdfImageHelper()

    # 循环遍历每个页面
    for page_index in range(doc.Pages.Count):
        page = doc.Pages[page_index]
        # 获取页面的图片信息
        image_infos = image_helper.GetImagesInfo(page)

        # 打印图片的坐标位置、宽度和高度
        for image_index, image_info in enumerate(image_infos):
            print(f"第 {page_index + 1} 页, 第 {image_index + 1} 个图片:")
            print(f"  图片位置: ({image_info.Bounds.X}, {image_info.Bounds.Y})")
            print(f"  图片大小: {image_info.Bounds.Width} x {image_info.Bounds.Height}")

    doc.Close()

# 使用示例
print_pdf_image_info("示例.pdf")

Python 提取PDF图片信息

以上就是使用Python从PDF中提取图片和图片坐标、宽度和高度等信息的全部内容。本文完。

相关推荐

  1. 使用 Python PDF 文件提取、转换图像

    2024-07-17 12:30:03       37 阅读
  2. Python应用—pdf保存图片

    2024-07-17 12:30:03       21 阅读
  3. flutter获取本地图片高度宽度

    2024-07-17 12:30:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 12:30:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 12:30:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 12:30:03       58 阅读
  4. Python语言-面向对象

    2024-07-17 12:30:03       69 阅读

热门阅读

  1. 聚合支付+分账系统体系

    2024-07-17 12:30:03       24 阅读
  2. 解释 Git 的基本概念和使用方式。

    2024-07-17 12:30:03       25 阅读
  3. SQL Error: 1406, SQLState: 22001

    2024-07-17 12:30:03       25 阅读
  4. cn.hutool.core.util.IdUtil.getSnowflake

    2024-07-17 12:30:03       25 阅读
  5. redistemplate介绍与演示

    2024-07-17 12:30:03       18 阅读
  6. Fixing a Binary String

    2024-07-17 12:30:03       27 阅读
  7. vue搜索框过滤--- computed、watch区别

    2024-07-17 12:30:03       25 阅读
  8. 洛阳建筑设计资质延期续期流程与所需材料

    2024-07-17 12:30:03       18 阅读
  9. ETG2000 5.3.9.2 Offline Dictionary DictionaryFile路径

    2024-07-17 12:30:03       23 阅读
  10. 数学归纳法

    2024-07-17 12:30:03       18 阅读
  11. Python基础:register buffer

    2024-07-17 12:30:03       23 阅读
  12. android.app.application can not be cast to android.app.Activity

    2024-07-17 12:30:03       20 阅读
  13. 优化Conda环境:深入掌握conda clean命令的清理艺术

    2024-07-17 12:30:03       22 阅读
  14. 探索Conda的搜索能力:挖掘Python包的宝藏

    2024-07-17 12:30:03       27 阅读