使用Python3怎么获取图片文字中的中文

使用Python3怎么获取图片文字中的中文?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、运行环境

(1) win10

(2) pycharm

(3) python 3.5

(4) pillow与pytesseract库安装:

pip3 install pillow
pip3 install pytesseract

(5)  识别引擎tesseract-ocr ,下载之后解压安装,下载地址:https://www.jb51.net/softs/538925.html

二、 运行代码

# -*- coding: utf-8 -*-
from PIL import Image
import pytesseract
#上面都是导包,只需要下面这一行就能实现图片文字识别
text=pytesseract.image_to_string(Image.open('show.jpg'),lang='chi_sim') #设置为中文文字的识别
#text=pytesseract.image_to_string(Image.open('test.png'),lang='eng')  #设置为英文或阿拉伯字母的识别
print(text)

三、报错解决

1.FileNotFoundError:[WinError 2]系统找不到指定文件。

解决办法:

打开文件pytesseract.py,找到如下代码,将tesseract_cmd的值修改为全路径,再次使用就不会报这个错了。

tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'

2.pytesseract.pytesseract.TesseractError:(1,'Error opening data file\\Progr

解决办法:

打开文件pytesseract.py,找到image_to_string,指定config的参数,如下:

tessdata_dir_config = '--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'
def image_to_string(image, lang=None, boxes=False, config=tessdata_dir_config):

看完上述内容,你们掌握使用Python3怎么获取图片文字中的中文的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!