python,验证码生成
<pre>import string
import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from datetime import *
import uuid
import time
from PIL import ImageFilter
# 创建图片
image = Image.new("RGB", (180, 60), "white")
# 创建对象
draw = ImageDraw.Draw(image)
def rand_txt():
chars = string.ascii_letters + string.digits
s = [random.choice(chars) for j in range(5)]
m = ('{0}\n'.format(''.join(s)))
return m def rand_color():
randcolor=(random.randint(129, 255), random.randint(129, 255), random.randint(129, 255))
return randcolor def rand_txtcolor():
txtclor=(random.randint(32, 128), random.randint(32, 128), random.randint(32, 128))
return txtclor def rand_font():
f6 = ImageFont.truetype("C:\Windows\Fonts\STXINGKA.TTF", 48)
f5 = ImageFont.truetype("C:\Windows\Fonts\LetterGothicStd-BoldSlanted.otf", 48)
f4 = ImageFont.truetype("C:\Windows\Fonts\ONYX.TTF", 48)
f3 = ImageFont.truetype("C:\Windows\Fonts\LHANDW.TTF", 48)
f2 = ImageFont.truetype("C:\Windows\Fonts\LithosPro-Regular.otf", 48)
f1 = ImageFont.truetype("C:\Windows\Fonts\FRSCRIPT.TTF", 48)
randfont = random.choice([f1,f2,f3,f4,f5,f6])
return randfont def rand_name():#产生随机文件名
uuid_name = uuid.uuid4().time
randname ='F:\python\项目实战\验证码\yanzhenma\%s.png'%uuid_name
return randname def rand_huabu():#遍历画布随机填充颜色
for x in range(180):
for y in range(60):
draw.point((x, y), fill=rand_color()) def rand_yanzhengma():
x = random.randint(0, 90)
for i in range(x):# 随机产生干扰线
x = random.randint(0, 180)
y = random.randint(0, 60)
xl = random.randint(0, 60)
yl = random.randint(0, 60)
draw.line((x, y, x + xl, y + yl), fill=rand_txtcolor())
final_font = rand_font()
draw.text((15, 10), rand_txt(), font=final_font, fill=rand_txtcolor())
image.save(rand_name(), 'PNG')#随机存在png文件中 if __name__ == '__main__':
for m in range(20): # 随机生成验证码
# 随机画布
rand_huabu()
rand_yanzhengma()
#image.show()#show验证码</pre>
[gallery ids="51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66" type="rectangular"]
python,验证码生成的更多相关文章
- Python随机生成验证码的两种方法
Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...
- python登录验证码生成及自动化测试规避
在用django写论坛的时候,需要有登录及注册功能. 故就登录界面后端需要生成随机验证码并传值给前端的代码进行编写如下. 验证码生成png需要调用到python的图形库 生成注册码img import ...
- Python教程:随机验证码生成和join 字符串
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...
- Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成
random模块--random是一个生成器 首先: import random #导入模块 print (help(random)) #打印random模块帮助信息 常用function ...
- python快速生成注释文档的方法
python快速生成注释文档的方法 今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可以检查我们写的类方法引用名称是否重复有问题等.一看别人专业的大牛们写的文档多牛多羡慕,不用担 ...
- Python验证码6位自动生成器
Python验证码6位自动生成器
- 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...
- Flask实战第40天:图片验证码生成技术
图片验证码生成 安装pillow pip install pillow 在utils下新建python package命名为captcha 把需要需要用到的字体放在captcha下 编辑captcha ...
- python 验证码 高阶验证
python 验证码 高阶验证 标签: 验证码python 2016-08-19 15:07 1267人阅读 评论(1) 收藏 举报 分类: 其他(33) 目录(?)[+] 字符型图片验证 ...
- 随机验证码生成和join 字符串
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...
随机推荐
- ansj分词
本文转载至:https://blog.csdn.net/bitcarmanlee/article/details/53607776 最近的项目需要使用到分词技术.本着不重复造轮子的原则,使用了ansj ...
- (简单)华为Nova3 PAR-AL00的USB调试模式在哪里开启的步骤
当我们使用Pc链接安卓手机的时候,如果手机没有开启Usb调试模式,Pc则没能够成功检测到我们的手机,有时候我们使用的一些功能比较强的的APP如之前我们使用的一个APP引号精灵,老版本就需要开启Usb调 ...
- linux - man 提示:-bash: man: command not found
在执行man命令时,提示:-bash: man: command not found 原因1:没有配置path环境 解决:vi /etc/profile JAVA_HOME=/usr/java/jdk ...
- centos 6.5 安装 tomcat8 及性能优化_虚拟主机
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomcat和Nginx.Apa ...
- SQL Server分页查询的万能存储过程
CREATE proc [dbo].[p_paging]@tableName varchar(8000), --表名.视图名@indexCol varchar(50) = 'id', --标识列名(如 ...
- 国内第一本cbuilder开发web的宝典震撼登场(delphi也可参考)
uniGUI入门到精通cBuilder版 众所周知,cbuilder和delphi是曾经风靡多年的强悍的万能的开发工具,岂奈大浪淘沙,B/S时代的到来让这对孪生兄弟隐姓埋名.然,无数风流人物依然对他们 ...
- VMSTAT监控CPU使用率,内存使用,虚拟内存交换情况
Linux性能监控分析命令(一)—vmstat命令详解 一.vmstat介绍 语法格式: vmstat [-V] [-n] [-S unit] [delay [count]] -V prints ve ...
- openstack常用命令-neutron篇
列出当前租户所有的网络 neutron net-list 列出所有租户的所有网络(需要管理员权限) neutron net-list --all-tenants 创建一个网络(vlan/flat) n ...
- js 防抖 debounce 与 节流 throttle
debounce(防抖) 与 throttle(节流) 主要是用于用户交互处理过程中的性能优化.都是为了避免在短时间内重复触发(比如scrollTop等导致的回流.http请求等)导致的资源浪费问题. ...
- c语言实现两个单链表的交叉合并
#include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; struc ...