python - 使用 Python 从 pdf 中提取图像
问题描述
我们如何从 PDF 中提取图像(仅图像)。
我使用了很多在线工具,它们都不是通用的。在大多数 PDF 中,它使用整个图像的屏幕截图而不是图像。PDF链接-> sg.inflibnet.ac.in:8080/jspui/bitstream/10603/121661/9/09_chapter 4.pdf
解决方案
这是一些使用 pyPdf 读取 PDF 文件、提取图像并将它们生成为PIL.Image
. 您需要根据需要对其进行修改,这里只是为了演示如何遍历对象树。
import io
import pyPdf
import PIL.Image
infile_name = 'my.pdf'
with open(infile_name, 'rb') as in_f:
in_pdf = pyPdf.PdfFileReader(in_f)
for page_no in range(in_pdf.getNumPages()):
page = in_pdf.getPage(page_no)
# Images are part of a page's `/Resources/XObject`
r = page['/Resources']
if '/XObject' not in r:
continue
for k, v in r['/XObject'].items():
vobj = v.getObject()
# We are only interested in images...
if vobj['/Subtype'] != '/Image' or '/Filter' not in vobj:
continue
if vobj['/Filter'] == '/FlateDecode':
# A raw bitmap
buf = vobj.getData()
# Notice that we need metadata from the object
# so we can make sense of the image data
size = tuple(map(int, (vobj['/Width'], vobj['/Height'])))
img = PIL.Image.frombytes('RGB', size, buf,
decoder_name='raw')
# Obviously we can't really yield here, do something with `img`...
yield img
elif vobj['/Filter'] == '/DCTDecode':
# A compressed image
img = PIL.Image.open(io.BytesIO(vobj._data))
yield img
推荐阅读
- python - 为什么积极的前瞻最终不能与 /$ 一起使用
- r - 如何用ggplot填充geom_line()图下的区域?
- isabelle - 伊莎贝尔/霍尔 - 电路
- java - Java中深度嵌套的哈希图
- kubernetes - 如何从 kubernetes master 访问 etcd 集群端点
- web - 在 IIS 中无法使用端口号访问域名
- r - 在r中按名称和条件合并文件
- django - 在 ModelChoiceField 中添加多个标签 - Django
- javascript - flyTo() Leaflet - 我如何使用 onclick 监听器在我的地图上飞行
- node.js - 从 lambda 函数调用 Spotify API