当前位置: 代码网 > it编程>前端脚本>Python > 使用Python从PDF中提取图片和图片信息(坐标、宽度和高度等)

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

2025年02月24日 Python 我要评论
引言pdf文件作为一种广泛使用的电子文档格式,不仅包含文字信息,还可能包含各种图片、图表等视觉元素。在某些场景下,我们可能需要从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.width 和 pdfimageinfo.bounds.height 属性。具体步骤如下:

  • 创建 pdfdocument 实例并使用 pdfdocument.loadfromfile() 方法加载 pdf 文档。
  • 创建 pdfimagehelper 实例。
  • 循环遍历文档中的页面。
  • 使用 pdfimagehelper.getimagesinfo(page) 方法获取每个页面中的图片信息。
  • 遍历获取结果,使用 pdfimageinfo.bounds.xpdfimageinfo.bounds.ypdfimageinfo.bounds.width 和 pdfimageinfo.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中提取图片和图片信息(坐标、宽度和高度等)的文章就介绍到这了,更多相关python pdf提取图片信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com