我有很多PDF文件,基本上是扫描文档,所以每一页都是一个扫描图像。我想要执行OCR并从这些文件中提取文本。我尝试过pytesseract,但它不直接对pdf文件执行OCR,因此,作为一项工作,我希望从PDF文件中提取images,将它们保存在目录中,然后直接在这些图像上使用pytesseract执行OCR。在python中有没有从pdf文件中提取扫描图像的方法?或者有任何方法可以直接对pdf文件执行OCR?
发布于 2018-05-26 16:19:17
这个问题已经在以前的堆栈溢出帖子中得到了解决。
Converting PDF to images automatically
Converting a PDF to a series of images with Python
下面是一个可能有用的脚本:pdfs.html
在问问题之前,请先检查一下以前的帖子。
编辑:
包括工作脚本,供将来参考。程序适用于Windows上的Python3.6:
# coding=utf-8
# Extract jpg's from pdf's. Quick and dirty.
import sys
with open("Link/To/PDF/File.pdf", "rb") as file:
pdf = file.read()
startmark = b"\xff\xd8"
startfix = 0
endmark = b"\xff\xd9"
endfix = 2
i = 0
njpg = 0
while True:
istream = pdf.find(b"stream", i)
if istream < 0:
break
istart = pdf.find(startmark, istream, istream + 20)
if istart < 0:
i = istream + 20
continue
iend = pdf.find(b"endstream", istart)
if iend < 0:
raise Exception("Didn't find end of stream!")
iend = pdf.find(endmark, iend - 20)
if iend < 0:
raise Exception("Didn't find end of JPG!")
istart += startfix
iend += endfix
print("JPG %d from %d to %d" % (njpg, istart, iend))
jpg = pdf[istart:iend]
with open("jpg%d.jpg" % njpg, "wb") as jpgfile:
jpgfile.write(jpg)
njpg += 1
i = iendhttps://stackoverflow.com/questions/50544495
复制相似问题