Python生成随机验证码

Python生成随机验证码,需要使用PIL模块.

安装:

1
pip3 install pillow

基本使用

1. 创建图片

1
2
3
4
5
6
7
8
9
from PIL import Image
img = Image.new(mode='RGB', size=(12030), color=(255255255))
 
# 在图片查看器中打开
# img.show() 
 
# 保存在本地
with open('code.png','wb') as f:
    img.save(f,format='png')
 
#保存在内存
from io import BytesIO
f=BytesIO()
img.save(f,'png')

2. 创建画笔,用于在图片上画任意内容

1
2
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')

3. 画点

1
2
3
4
5
6
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示坐标
# 第二个参数:表示颜色
draw.point([100100], fill="red")
draw.point([300300], fill=(255255255))

4. 画线

1
2
3
4
5
6
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标
# 第二个参数:表示颜色
draw.line((100,100,100,300), fill='red')
draw.line((100,100,300,100), fill=(255255255))

5. 画圆

1
2
3
4
5
6
7
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
draw.arc((100,100,300,300),0,90,fill="red")

6. 写文本

1
2
3
4
5
6
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
draw.text([0,0],'python',"red")

7. 特殊字体文字

1
2
3
4
5
6
7
8
9
10
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示字体文件路径
# 第二个参数:表示字体大小
font = ImageFont.truetype("kumo.ttf"28)
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
# 第四个参数:表示颜色
draw.text([00], 'python'"red", font=font)

图片验证码

#基于PIL模块创建随机验证码
from PIL import Image,ImageDraw,ImageFont
from io import BytesIO

def get_valid_img(request):
    #通过一个函数取到随机颜色
    def get_random_color():
        import random
        return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
    #创建图片
    img = Image.new("RGB", (350, 38), get_random_color())
    #创建画笔
    draw = ImageDraw.Draw(img)
    #引入特殊字体文字   32号字体
    font = ImageFont.truetype("static/font/kumo.ttf", 32)
    
    #生成6位随机数字,字母验证码,并记录在keep_str中
    keep_str=""
    for i in range(6):
        random_num = str(random.randint(0, 9))
        random_lowalf = chr(random.randint(97, 122))
        random_upperalf = chr(random.randint(65, 90))                     
     random_char=random.choice([random_num,random_lowalf,random_upperalf]) draw.text((i*30+50,0),random_char, get_random_color(), font=font) keep_str+=random_char #画干扰点和干扰线 # width=350 # height=38 # for i in range(5): # x1=random.randint(0,width) # x2=random.randint(0,width) # y1=random.randint(0,height) # y2=random.randint(0,height) # draw.line((x1,y1,x2,y2),fill=get_random_color()) # # for i in range(50): # draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color()) # x = random.randint(0, width) # y = random.randint(0, height) # draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color()) # 写与读 f = BytesIO() img.save(f, "png") data = f.getvalue() # 将验证码存在各自的session中 request.session['keep_str']=keep_str return HttpResponse(data)
原文地址:https://www.cnblogs.com/fengchong/p/9917816.html