python识别图片上的文字_python 识别图片中的汉字

1011634-20190602011554199-1829714798.png

我们就识别上面的汉字。

安装软件tesseract和python库

然后新建一个文件夹test,把上面那张图片放在文件夹里面,再新建一个test文件

写入如下内容

#coding=utf-8

fromPIL import Image

import pytesseract

#上面都是导包,只需要下面这一行就能实现图片文字识别

text=pytesseract.image_to_string(Image.open('xxx.png'),lang='chi_sim')

print(text)

目录如下:

1011634-20190602012127691-270813437.png

运行可能会出现错误:

C:\Users\k\Desktop\test>python test.py

Traceback (most recent call last):

File"test.py", line 5, in text=pytesseract.image_to_string(Image.open('xxx.png'),lang='chi_sim')

File"C:\Users\k\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 309, inimage_to_string

}[output_type]()

File"C:\Users\k\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 308, in Output.STRING: lambda: run_and_get_output(*args),

File"C:\Users\k\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 218, inrun_and_get_output

run_tesseract(**kwargs)

File"C:\Users\k\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 194, inrun_tesseract

raise TesseractError(status_code, get_errors(error_string))

pytesseract.pytesseract.TesseractError: (1, 'Error opening data file C:\\Program Files (x86)\\Tesseract-OCR/tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory. Failed loading language \'chi_sim\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')

因为tesseract-ocr默认不支持中文识别。 将下载到的文件:chi_sim.traineddata 放到Tesseract-OCR安装目录 D:\Program Files (x86)\Tesseract-OCR\tessdata 下

链接:https://pan.baidu.com/s/1c-fveIYnm1sQHxX9WRpUZw

提取码:9ovq

1011634-20190602020932071-615666143.png

再次运行

python test.py

下面是输出结果

C:\Users\k\Desktop\test>python test.py

风急天高猿啸衷′ 渚麦冒麦少丑弓飞口。

u边洛木萧萧下′ 不〖长江滚滚来。

万 悲禾火常作畜′ 年多病独登台。

艰难苦恨萦霜 渣倒新停澍酉木不=

参考:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值