import pytesseractfrom PIL import Image ImageFilter# 读取验证码图片img = ImageopenrDexcelyzmpng# 去除蓝色干扰线img = imgconvertRGBpixels = imgloadfor i in rangeimgsize0 for j in rangeimgsize1 r g b = pixe
import pytesseract from PIL import Image, ImageFilter
def recognize_captcha(img): # 去除蓝色干扰线 img = img.convert('RGB') pixels = img.load() for i in range(img.size[0]): for j in range(img.size[1]): r, g, b = pixels[i, j] if b > 150 and r < 100 and g < 100: pixels[i, j] = (255, 255, 255)
# 预处理图片
img = img.filter(ImageFilter.SHARPEN)
img = img.convert('L')
# 识别验证码数字
captcha = pytesseract.image_to_string(img, config='--psm 10')
captcha = ''.join(filter(str.isdigit, captcha))
# 判断验证码是否为4位数字,如果不是,重新识别
if len(captcha) != 4 or not captcha.isdigit():
captcha = recognize_captcha(img)
return captcha
读取验证码图片
img = Image.open(r'D:\excel\yzm.png')
识别验证码数字
captcha = recognize_captcha(img) print(captcha
原文地址: https://www.cveoy.top/t/topic/fFUT 著作权归作者所有。请勿转载和采集!