【问题标题】:Is there a way to extract images from a pdf in Python while preserving the location of the image in the pdf?有没有办法在 Python 中从 pdf 中提取图像,同时保留 pdf 中图像的位置?
【发布时间】:2021-09-07 07:56:17
【问题描述】:

我需要从 pdf 中提取图像而不会丢失其在 pdf 中的位置。我需要知道图片在哪个页面,图片在文本中的位置,然后将pdf中的文本和图片保存到一个json文件中,数据的顺序保持不变。

【问题讨论】:

标签: python image pdf pdf-scraping


【解决方案1】:

您可以使用pdfplumber 并运行以下代码:

import pdfplumber

pdf_obj = pdfplumber.open(doc_path)
page = pdf_obj.pages[page_no]
images_in_page = page.images
page_height = page.height
image_bbox = (image['x0'], page_height - image['y1'], image['x1'], page_height - image['y0'])
cropped_page = page.crop(image_bbox)
image_obj = cropped_page.to_image(resolution=400)
image_obj.save(path_to_save_image)

来源链接here

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2019-10-15
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多