python3.6+pytesseract实现图像中文字识别

开始前的准备工作

1.安装pytesseract库

在Scripts目录下使用一下命令下载安装pytesseract库

pip install pytesseract
pip install PIL

在刚下载安装之后我们还不能够识别文字。然后要安装一个Tesseract-OCR软件。这个软件是由Google维护的开源的OCR软件。下载地址:百度网盘(密码:5m3d)。然后需要配置一下。

具体的找到Python\Python36\Lib\site-packages\pytesseract目录下的pytesseract.py这个文件,打开更改为下满的配置,参数为你安装的程序的路径

在配置成功后就可以使用这个库了

实现简单的图像识别代码

from PIL import Image 
import pytesseract

def shibie(file_path):
    image=Image.open(file_path) #获取image对象
    text = pytesseract.image_to_string(file_path,lang='chi_sim')
    print(text)

但是针对一些稍微有点混乱的代码还是经常出错,所以我们接下来写稍微有点优化的代码。

具体思想是将图片变成灰度图进行识别

from PIL import Image 
import pytesseract
def getMessage(path_name):
    text = pytesseract.image_to_string(path_name,lang='chi_sim')
    print(text)
def get_bin_table(threshold = 230):
	# 获取灰度转二值的映射table
	table = []
	for i in range(256):
		if i < threshold:
			table.append(0)
		else:
			table.append(1)
	return table
 
image = Image.open('D:/e.jpg')
imgry = image.convert('L')  # 转化为灰度图
table = get_bin_table()
out = imgry.point(table, '1')
getMessage(out)

能够识别一些比较纯粹额图片重点 文字,但是还是不能够满足使用。

思考着能不能使用百度的sdk进行文字识别,然后再获得值返回,我们只用其中返回的值就行了,应该是比较好用的。。后续更新

 

 

 

 

 

 

 

 

你可能感兴趣的:(python基础)