Python图像转文字

8
我正在使用Python 3.x,并使用以下代码将图像转换为文本:
from PIL import Image
from pytesseract import image_to_string

image = Image.open('image.png', mode='r')
print(image_to_string(image))

我遇到了以下错误:
Traceback (most recent call last):
  File "C:/Users/hp/Desktop/GII/Image_to_text.py", line 12, in <module>
    print(image_to_string(image))
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

请注意,我已将图像放在与我的Python文件相同的目录中。同时,在image = Image.open('image.png', mode='r')这一行并没有出现错误,但是在print(image_to_string(image))这一行却出现了错误。
有什么想法是可能出了什么问题吗?谢谢。

这段代码在我文件夹中同时拥有两个文件并且图像包含一些文字时可以正常运行。可能与绝对路径和相对路径有关... - Ohumeronen
你也可以尝试:import os.path; os.path.exists('image.png') - Ohumeronen
1
我现在使用的代码是:if (os.path.exists('image.png')): image = Image.open('image.png') print(image_to_string(image)) else: print('不存在') 但是遇到了相同的错误,这意味着文件存在,但尝试读取文本时会引发错误。 - muazfaiz
5个回答

8
你需要安装并在路径中设置tesseract
根据源代码pytesseract只是一个使用tesseract二进制文件作为要运行的二进制文件的subprocess.Popen的包装器。它本身不执行任何OCR操作。
源代码的相关部分:
def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
    '''
    runs the command:
        `tesseract_cmd` `input_filename` `output_filename_base`

    returns the exit status of tesseract, as well as tesseract's stderr output
    '''
    command = [tesseract_cmd, input_filename, output_filename_base]

    if lang is not None:
        command += ['-l', lang]

    if boxes:
        command += ['batch.nochop', 'makebox']

    if config:
        command += shlex.split(config)

    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)
    return (proc.wait(), proc.stderr.read())

引用其他来源的部分内容:

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'

更改tesseract路径的快速方法如下:

import pytesseract
pytesseract.tesseract_cmd = "/absolute/path/to/tesseract"  # this should be done only once 
pytesseract.image_to_string(img)

我认为你是对的,但我已经安装了tesseract,但它仍然会出现相同的错误。事实上,最残酷的部分是,当我使用image.show()方法打开图像时,它确实打开了图像,但在下一行处理图像时,它会抛出FileNotFoundError。我完全被卡住了 :( - muazfaiz
FileNotFoundError 是由于缺少 tesseract 而不是缺少图像文件本身。请参见我答案的编辑。 - Łukasz Rogalski

2
请安装以下软件包以从图像中提取文本 pnf/jpeg
pip install pytesseract

pip install Pillow 

使用Python Pytesseract OCR(光学字符识别)是从图像中电子提取文本的过程。
PIL可用于从简单的读写图像文件到科学图像处理、地理信息系统、遥感等任何领域。
from PIL import Image
from pytesseract import image_to_string 
print(image_to_string(Image.open('/home/ABCD/Downloads/imageABC.png'),lang='eng'))

1

您需要下载tesseract OCR设置。使用此链接下载设置:http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe

然后,在您的代码中包含以下行以使用tesseract可执行文件: pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract'

这是tesseract将被安装的默认位置。

就是这样。我也按照这些步骤在我的端上运行了代码。

希望这会有所帮助。


0

你的“当前”目录不是你想要的目录。

==> 你可以指定图像的完整路径,例如: image = Image.open(r'C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\image.png', mode='r')


0
你可以尝试使用这个Python库:https://github.com/prabhakar267/ocr-convert-image-to-text 正如包的README所述,使用非常简单。
usage: python main.py [-h] input_dir [output_dir]

positional arguments:
  input_dir
  output_dir

optional arguments:
  -h, --help  show this help message and exit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接