Python3 使用pillow库生成随机验证码
作者:haeasringnar 时间:2021-08-30 02:54:10
Python3 使用pillow库生成随机验证码的代码如下所示:
import random
# pillow 包的使用
from PIL import Image,ImageDraw,ImageFont,ImageFilter
#Image 负责处理图片
#ImageDraw 负责处理画笔
#ImageFont 负责处理文字
#ImageFilter负责处理路径
img=Image.new('RGB',(150,50),(255,255,255)) #建立一个图片
'''
RGB:表示采用RGB方式新建的图片
第二个:表示图片的宽度和高度
第三个:表示具体图片的颜色
'''
#创建画笔
draw=ImageDraw.Draw(img)
#绘制线
for i in range(random.randint(1,10)):
draw.line(
[
(random.randint(1,150),random.randint(1,50)),
(random.randint(1,150),random.randint(1,50))
],
fill=(0,0,0)
)
#绘制点
for i in range(1000):
draw.point(
[
random.randint(1,150),
random.randint(1,150)
],
fill=(0,0,0)
)
fontlist=list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
c_char=' '.join(random.sample(fontlist,5))#在指定的列表中随机取出指定个数的元素
#绘制字体 需要先定制一下字体
font=ImageFont.truetype('simsun.ttc',30)
draw.text((5,5),c_char,font=font,fill='green')
'''
第一个:代表文字的位置,距离上和左的位置
第二个:代表文字的内容
第三个:代表字体,字形和大小
第四个:字体颜色
'''
#扭曲字体
# params=[1-float(random.randint(1,2))/100,
# 0,0,0,
# 1-float(random.randint(1,2))/100,
# float(random.randint(1,2))/500,
# 0.001,
# float(random.randint(1,1))/500,
# ]
# img=img.transform((150,50),Image.PERSPECTIVE,params)
# img=img.filter(ImageFilter.EDGE_ENHANCE_MORE)
# 展示图片
img.show()
# 将图片保存到本地
img.save('blur.jpg', 'jpeg')
总结
以上所述是小编给大家介绍的Python3 使用pillow库生成随机验证码网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/haeasringnar/article/details/100062516
标签:python3,pillow库,随机,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL权限详解
2011-02-16 12:20:00
Python如何实现强制数据类型转换
2022-10-18 10:08:02
asp MD5加密方式使用建议
2011-03-30 11:17:00
IIS6.0下ASP的新增功能
2007-08-22 14:55:00
对Python发送带header的http请求方法详解
2022-02-06 11:44:59
理解Python数据离散化手写if-elif语句与pandas中cut()方法实现
2023-02-24 10:33:33
SQL中Groupby和Having的使用方法
2008-12-29 13:54:00
Python+unittest+requests+excel实现接口自动化测试框架
2021-09-08 07:04:49
![](https://img.aspxhome.com/file/2023/2/64732_0s.jpg)
CentOS下php使用127.0.0.1不能连接mysql的解决方法
2023-11-15 08:25:52
Python 2/3下处理cjk编码的zip文件的方法
2022-08-05 17:53:40
CSS兼容性(IE和Firefox)技巧大全
2010-07-29 12:29:00
echarts柱状图坐标轴内容显示不全的两种解决办法
2023-09-06 03:57:34
![](https://img.aspxhome.com/file/2023/2/56122_0s.png)
Python解析多帧dicom数据详解
2022-08-13 21:16:13
正确认识MySQL对服务器端光标的限制
2008-12-03 15:52:00
SQL Server密码管理的六个危险判断
2008-10-23 13:57:00
PHP PDOStatement::fetchColumn讲解
2023-06-06 09:17:20
如何上传一个.SQL文件到远程主机并且执行它来部署一个SQL数据库
2007-09-23 13:03:00
![](https://img.aspxhome.com/file/uploadpic/20079/23/200792313732111.jpg)
java.sql.SQLException: 内部错误: Unable to construct a Datum from the specified input
2010-07-16 13:23:00
Python爬虫之批量下载喜马拉雅音频
2022-09-25 20:18:27
![](https://img.aspxhome.com/file/2023/8/75938_0s.png)
简单了解pytest测试框架setup和tearDown
2022-09-12 07:26:56