[Python]图像二值化

https://blog.csdn.net/qq_35531549/article/details/96134760

 

# 识别前处理
# 图片二值化
from PIL import Image
import os
os.chdir('D:\OCR')
img = Image.open('test.png')

# 模式L”为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。
Img = img.convert('L')
Img.save("test1.png")

# 自定义灰度界限,大于这个值为黑色,小于这个值为白色
threshold = 200

table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)

# 图片二值化
photo = Img.point(table, '1')
photo.save("test2.png")
# 识别图片内容
import pytesseract
img_path = 'test2.png'

text=pytesseract.image_to_string(Image.open(img_path))

print(text)

————————————————
版权声明:本文为CSDN博主「寸草心2130」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35531549/article/details/96134760

posted @ 2020-03-03 18:35  shanlinghan  阅读(1021)  评论(0编辑  收藏  举报