【问题标题】:Problem with printing to console with pytessaract in spyder在 spyder 中使用 pytessaract 打印到控制台的问题
【发布时间】:2020-11-17 14:44:55
【问题描述】:

我目前在 Windows 10 上通过 anaconda 和 python 3.8.5 使用 spyder,当我运行此代码时:

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"

img_path ='img/gotta-go-fast.jpg'

img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

result = pytesseract.image_to_string(img, lang='eng')
print(result)

spyder 中的 IPython 控制台会自动被清除,但如果我只写入这样的文本文档,则不会发生同样的情况:

result = pytesseract.image_to_string(img, lang='eng')

with open('text_result.txt', mode ='w') as file:
    file.write(result)

有没有办法解决这个问题?

【问题讨论】:

    标签: python anaconda spyder tesseract python-tesseract


    【解决方案1】:

    发现发生了什么。当 pytessaract 读取文本时,在最后一个读取行之后的新行中添加了 \x0c 命令。 通过删除这样的命令解决了这个问题:

    result = pytesseract.image_to_string(img, lang='eng')
    arr = result.split('\n')[0:-1]
    result = '\n'.join(arr)
    

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2015-11-05
      • 2019-02-11
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2015-07-05
      • 2017-11-30
      • 2013-01-27
      相关资源
      最近更新 更多