tesserocr,PIL验证码生成与识别

1.安装       tesserocr,PIL(pip install pillow)

2.安装       Tesseract-OCR(最好将语言包全部下载)

3.将Tesseract-OCR下的tessdata复制到你的项目下的venv中Scripts下

 

实例:

'''
识别图片
'''
import tesserocr
from PIL import Image
#获得图片
img = Image.open('1.jpg')
img.show()
#图像灰度化处理
image = img.convert('L')
threshold = 125
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
image = image.point(table, '1')
image.show()
#识别中文需指定语言集
#tesserocr.image_to_text(img,lang='chi_sim')
result = tesserocr.image_to_text(image)
print(result)
'''
生成验证码
'''
from PIL import Image, ImageDraw, ImageFont, ImageFilter

import random

# 随机字母:
def rndChar():
    return chr(random.randint(65, 90))

# 随机颜色1:
def rndColor():
    colorArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    color = ""
    for i in range(6):
        color += random.choice(colorArr)
    return "#" + color


# 随机颜色2:
def rndColor2():
    colorArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    color = ""
    for i in range(6):
        color += random.choice(colorArr)
    return "#" + color

# 240 x 60:
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
#SIMYOU.TTF为幼圆字体文件,SIMLI.TTF为隶书字体文件,STXINGKA.TTF为行楷字体文件
font = ImageFont.truetype('arial.ttf', 36)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
    for y in range(height):
        draw.point((x, y), fill=rndColor())
# 输出文字:
text_data=''
for t in range(4):
    text=rndChar()
    draw.text((60 * t + 10, 10), text, font=font, fill=rndColor2())
    text_data+=text
print(text_data)
# 模糊:
'''
图像的滤波ImageFiter:
BLUR	模糊滤波
CONTOUR	轮廓滤波
DETAIL	细节滤波
EDGE_ENHANCE	边界增强滤波
EDGE_ENHANCE_MORE	边界增强滤波(程度更深)
EMBOSS	浮雕滤波
SHARPEN	锐化滤波
SMOOTH_MORE	平滑滤波(程度更深)
GaussianBlur(radius=2)	高斯模糊
'''
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值