python类随机生成图片验证码

from PIL import Image,ImageDraw,ImageFont
import random,string

class ImageCode:
def get_color(self):
red = random.randint(32,127)
green = random.randint(0,255)
blue = random.randint(60,255)
return red,green,blue
def get_code(self):
code = random.sample(string.ascii_letters+string.digits,6)
return ''.join(code)
def randon_lines(self,draw,num,width,height):
for num in range(num):
x1 = random.randint(0,width/2)
y1 = random.randint(0,width/2)
x2 = random.randint(width/2,width)
y2 = random.randint(height/2,height)
draw.line(((x1,y1),(x2,y2)),fill= self.get_color(),width=2)

def get_image(self):
code = self.get_code()
width,height = 120,60
image = Image.new('RGB',(width,height),'white')
font = ImageFont.truetype('arial.ttf', size=40)
draw = ImageDraw.Draw(image)

for i in range(4):
draw.text((5+random.randint(-3,3)+23*i,5+random.randint(-3,3)),text=code[i],fill=self.get_color(),font=font)
self.randon_lines(draw,6,width,height)
image.show()


ImageCode().get_image()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值