python生成验证码
来源:互联网 发布:js转换日期格式 编辑:程序博客网 时间:2024/05/29 14:31
代码:
#coding:utf-8from PIL import Image,ImageDraw,ImageFont,ImageFilterimport string,randomdef getRandomChar(): return [random.choice(string.letters)for _ in range(4)]def getRandomColor(): return (random.randint(30,100),random.randint(30,100),random.randint(30,100))def getCodePicture(): width=240 height=60 #创建画布 image=Image.new('RGB',(width,height),(180,180,180)) font=ImageFont.truetype('./1.ttf',40) draw=ImageDraw.Draw(image) #创建验证码对象 code=getRandomChar() #把验证码放在画布上 for t in range(4): draw.text((60*t+10,0),code[t],font=font,fill=getRandomColor()) #填充噪点 for _ in range(random.randint(1500,3000)): draw.point((random.randint(0,width),random.randint(0,height)),fill=getRandomColor()) #模糊处理 image=image.filter(ImageFilter.BLUR) #保存名字为验证码的图片 image.save("".join(code)+'.jpg','jpeg');if __name__=='__main__': getCodePicture()
1、random模块
random.choice
random.choice从序列中获取一个随机元素。其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。这里要说明一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple,字符串都属于sequence。下面是使用choice的一些例子:
print random.choice(“学习Python”)
print random.choice([“JGood”,”is”, “a”, “handsome”, “boy”])
print random.choice((“Tuple”,”List”, “Dict”))random.randint
random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b,下面是一些liz
print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20
print random.randint(20, 20) #结果永远是20
2、ImageDraw对象draw的draw.text(),draw.point()方法
3、image.filter(ImageFilter.BLUR)模糊处理
阅读全文
0 0
- python生成验证码
- Python生成验证码
- Python生成验证码
- python生成验证码
- python生成验证码
- Python生成验证码
- python生成验证码
- python生成验证码
- python生成图片验证码
- python 生成验证码
- python生成图片验证码
- 用python生成验证码
- python代码生成验证码
- 使用python生成验证码
- Python 生成随机验证码
- 【Python】生成图片验证码
- python生成随机验证码
- python 随机生成验证码
- InnoDB行锁的实现方式
- MySQL之innodb_flush_log_at_trx_commit和sync_binlog参数详解
- HTML (二)—— 图片标签
- java_继承中的构造方法
- 霍夫变换的原理 及 一种参数迭代的自适应寻找最佳霍夫直线的代码(在条码识别中的应用)
- python生成验证码
- zookeeper安装教程(伪分布式和分布式)
- 好玩的CMD命令行
- leetcode 28. Implement strStr()
- 2184: 不是防AK题
- 内存管理--程序的装载
- 前端<ul>分页实现
- SQLi-Labs 练习总结 Page-1 Less-1
- keepavlived的搭建与问题