首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python从PDF中提取扫描页面

使用python从PDF中提取扫描页面
EN

Stack Overflow用户
提问于 2018-05-26 15:48:44
回答 1查看 4.6K关注 0票数 0

我有很多PDF文件,基本上是扫描文档,所以每一页都是一个扫描图像。我想要执行OCR并从这些文件中提取文本。我尝试过pytesseract,但它不直接对pdf文件执行OCR,因此,作为一项工作,我希望从PDF文件中提取images,将它们保存在目录中,然后直接在这些图像上使用pytesseract执行OCR。在python中有没有从pdf文件中提取扫描图像的方法?或者有任何方法可以直接对pdf文件执行OCR

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-26 16:19:17

这个问题已经在以前的堆栈溢出帖子中得到了解决。

Converting PDF to images automatically

Converting a PDF to a series of images with Python

下面是一个可能有用的脚本:pdfs.html

另一种方法:https://www.daniweb.com/programming/software-development/threads/427722/convert-pdf-to-image-with-pythonmagick

在问问题之前,请先检查一下以前的帖子。

编辑:

包括工作脚本,供将来参考。程序适用于Windows上的Python3.6:

代码语言:javascript
复制
# 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 = iend
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50544495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档