Windows pytesseract image_to_osd Invalid resolution 0 dpi. Using 70 instead. Too few characters报错及解决

236 篇文章 43 订阅
19 篇文章 1 订阅

Windows pytesseract image_to_osd Invalid resolution 0 dpi. Using 70 instead. Too few characters报错及解决

1. 安装

  1. python3.7+

  2. pip install pytesseract==0.1.9

  3. 安装tesseract-ocr(配置path环境变量或者在代码中指定tesseract_cmd)

2.报错及解决

2.1 TesseractNotFoundError

  • 报错: 无可执行的tesseract
  • 解决:配置PATH环境变量或者在代码中指定tesseract_cmd
import pytesseract

# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\tesseract\tesseract.exe'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'

2.2 Error opening data file D:\Tesseract-OCR\fra.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your “tessdata” directory. Failed loading language ‘fra’ Tesseract couldn’t load any languages! Could not initialize tesseract

  • 解决: 把fra.traineddata放到tesseract.exe上一级./tessdata的路径 或者在代码中导入:
import os

import pytesseract

# 语言文件.traindata
os.environ['TESSDATA_PREFIX'] = 'D:\\tesseract\\tessdata'  # 在PATH中配置tesseract环境变量或者此处指定

# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\\tesseract\\tesseract.exe'

2.3 raise TesseractError(proc.returncode, get_errors(error_string)) pytesseract.pytesseract.TesseractError: (1, ‘Estimating resolution as 304 UZN file C:\Users\ADMINI~1\AppData\Local\Temp\tess_oeu8q72f loaded. Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.’)

直接执行命令行ok,pytesseract.image_to_osd(Image.open(‘test.png’))) 报错

tesseract E:\mat\py-demo-22\extreme_points\images\normal.jpg E:\mat\py-demo-22\extreme_points\images\normal_1 -l osd --psm 0
  • 报错:tesseract升级的bug

  • 解决:直接传递照片文件路径,或者切回低版本的tesseract:5.0.0-alpha-20201224 ok。详情可参考
    在这里插入图片描述
    在这里插入图片描述

# 获取方向和文本系统
# print(pytesseract.image_to_osd(Image.open('test.png'))) # happening on tesseract version released after Jan1, 2021.I have tested in tesseract version 5.0.0-alpha-20201224
print(pytesseract.image_to_osd('test.png'))

遇到其他问题了可以去github issues里找解决办法;

3. 源码

# 测试pytesseract的方法
# test_osd.py
import os

import pytesseract
from PIL import Image

# 语言文件.traindata
os.environ['TESSDATA_PREFIX'] = 'D:\\tesseract\\tessdata'  # 在PATH中配置tesseract环境变量或者此处指定

# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\\tesseract\\tesseract.exe'

# 图片转string
print("PIL image_to_string: ", pytesseract.image_to_string(Image.open('test.png')))

# 提供图片的相对或者绝对路径
print('image_to_string: ', pytesseract.image_to_string('test.png'))

# 打印pytesseract支持的所有语言
print('langs: ', pytesseract.get_languages(config=''))

# French语言的识别
print('fra image_to_string: ',pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))

# 批量处理单个文件list
print('batch Images image_to_string: ',pytesseract.image_to_string('images.txt'))

# 超时后结束pyteseract
try:
    print(pytesseract.image_to_string('test.jpg', timeout=2))  # Timeout after 2 seconds
    print(pytesseract.image_to_string('test.jpg', timeout=0.5))  # Timeout after half a second
except RuntimeError as timeout_error:
    # Tesseract processing is terminated
    pass

# 获取预测的边界框
# print(pytesseract.image_to_boxes(Image.open('test.png')))
print('image_to_boxes: ',pytesseract.image_to_boxes('test.png'))

# 获取详细数据,包括方框、置信度、行号和页码
print('image_to_data: ',pytesseract.image_to_data(Image.open('test.png')))

# 获取方向和文本系统
# print(pytesseract.image_to_osd(Image.open('test.png')))  # but this issue is happening on tesseract version released after Jan1, 2021.I have tested in tesseract version 5.0.0-alpha-20201224
print('image_to_osd: ',pytesseract.image_to_osd('test.png'))

# Get a searchable PDF
# pdf = pytesseract.image_to_pdf_or_hocr('test.png', extension='pdf') # 报错
# with open('test.pdf', 'w+b') as f:
#     f.write(pdf) # pdf type is bytes by default

# Get HOCR output
# hocr = pytesseract.image_to_pdf_or_hocr('test.png', extension='hocr') # error tesseract v5.2.0.20220712 

# Get ALTO XML output
xml = pytesseract.image_to_alto_xml('test.png')
print('image_to_alto_xml: ', xml)

参考

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值