做一下笔记,以防忘记。
一、准备工作:
1.安装PIL(已改名为Pillow),pytesseract
pip3 install Pillow
pip3 install pytesseract
2.导入以上2个包
from PIL import Image
import pytesseract
3.由于要用到ORC,所以还有安装tesseract
tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/
二、准备完成就开始编程
1.保存一张验证码
2.获取验证码并进行颜色调整
path = 'F:/PycharmProjects/Vcode/image/123456.png'
img = Image.open(path)
# 获取图片类型
print(img.mode)
# 该图片为RGB类型,进行循环更换颜色
w,h = img.size
for x in range(w):
for y in range(h):
r,g,b = img.getpixel((x,y))
# RBG<=(150, 150, 150),变为黑色
if r <= 150 and g <= 150 and b <= 150:
img.putpixel((x,y), (0,0,0))
# 否则变为白色
else:
img.putpixel((x, y), (255, 255, 255))
调色后图片
3.提取图片字体
# 图片提取字体
text = pytesseract.image_to_string(img)
完整代码
from PIL import Image
import pytesseract
path = 'F:/PycharmProjects/Vcode/image/123456.png'
img = Image.open(path)
# 获取图片类型
print(img.mode)
# 该图片为RGB类型,进行循环更换颜色
w,h = img.size
for x in range(w):
for y in range(h):
r,g,b = img.getpixel((x,y))
# RBG<=(150, 150, 150),变为黑色
if r <= 150 and g <= 150 and b <= 150:
img.putpixel((x,y), (0,0,0))
# 否则变为白色
else:
img.putpixel((x, y), (255, 255, 255))
# 图片提取字体
text = pytesseract.image_to_string(img)
print(text)
img.save('F:/PycharmProjects/Vcode/image/123123.png')
结果
做的比较简单,而且识别率也不是很高,后期再继续改进,奥利给!!!