1、 首先安装 tesserocr 包, 具体的安装网上是有教程的, 在这里不详细讲
链接地址: https://blog.csdn.net/chang995196962/article/details/91039138
import tesserocr
from PIL import Image
image = Image.open ("code.jpg")
result = tesserocr.image_to_text(image)
2、 降噪处理
# 降噪处理
import tesserocr
from PIL import Image
image = Image.open('code.jpg' )
image = image.convert ("L")
threshold = 127
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
# image.show()
image = image.point(table, '1')
result = tesserocr.image_to_text(image)
print(result)
3、验证码不用 tesserocr的 降噪处理, 这个降噪就是改变 rgb 的值, 这里面是那个127, 自己调整吧,自己识别
import pytesseract
from PIL import Image
from io import BytesIO
def get_captch(captcha_content):
"""
简单验证码的识别
:param captcha_content: 传输过来的保证以字节的方式传输
:return:
"""
image = Image.open(BytesIO(captcha_content))
# 转化为灰度图
imgry = image.convert('L')
table = [0 if i < 127 else 1 for i in range(256)]
# 使字体更加突出的显示
out = imgry.point(table, '1')
# out.show()
captcha = pytesseract.image_to_string(out)
captcha = captcha.strip()
captcha = captcha.upper()
print(captcha)
if __name__ == '__main__':
with open("code.jpg", "rb") as f:
captcha_content = f.read()
get_captch(captcha_content)
4、 https://github.com/xugaopeng1/cnn_captcha 这个是很好的网站, 以后自己要研究一下, 我也不会, 哈哈哈
import cv2
import numpy as np
import pytesseract
from PIL import Image
from io import BytesIO
def writ_cv():
img = cv2.imread(r'Y:\xugaopeng\test\cnn_captcha-master\sample\local\8.jpg')
h, w, c = img.shape
for i in range(h):
for j in range(w):
if np.var(img[i, j, :]) < 500:
img[i, j, :] = 255
# cv2.imshow('img', img)
# cv2.waitKey(0)
cv2.imwrite(r'Y:\xugaopeng\test\cnn_captcha-master\sample\local\9.jpg', img)
def get_captch(captcha_content):
"""
简单验证码的识别
:param captcha_content: 传输过来的保证以字节的方式传输
:return:
"""
image = Image.open(BytesIO(captcha_content))
# 转化为灰度图
imgry = image.convert('L')
table = [0 if i < 110 else 1 for i in range(256)]
# 使字体更加突出的显示
out = imgry.point(table, '1')
out.show()
captcha = pytesseract.image_to_string(out)
captcha = captcha.strip()
captcha = captcha.upper()
print(captcha)
if __name__ == '__main__':
r_img = r'66.jpg'
img_file = open(r_img, 'rb').read()
get_captch(img_file)
# writ_cv()