<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,验证码生成的更多相关文章

  1. Python随机生成验证码的两种方法

    Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...

  2. python登录验证码生成及自动化测试规避

    在用django写论坛的时候,需要有登录及注册功能. 故就登录界面后端需要生成随机验证码并传值给前端的代码进行编写如下. 验证码生成png需要调用到python的图形库 生成注册码img import ...

  3. Python教程:随机验证码生成和join 字符串

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...

  4. Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成

    random模块--random是一个生成器 首先: import random    #导入模块 print (help(random))    #打印random模块帮助信息 常用function ...

  5. python快速生成注释文档的方法

    python快速生成注释文档的方法 今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可以检查我们写的类方法引用名称是否重复有问题等.一看别人专业的大牛们写的文档多牛多羡慕,不用担 ...

  6. Python验证码6位自动生成器

    Python验证码6位自动生成器

  7. 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...

  8. Flask实战第40天:图片验证码生成技术

    图片验证码生成 安装pillow pip install pillow 在utils下新建python package命名为captcha 把需要需要用到的字体放在captcha下 编辑captcha ...

  9. python 验证码 高阶验证

    python 验证码 高阶验证 标签: 验证码python 2016-08-19 15:07 1267人阅读 评论(1) 收藏 举报  分类: 其他(33)    目录(?)[+]   字符型图片验证 ...

  10. 随机验证码生成和join 字符串

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...

随机推荐

  1. python基础数据类型-练习

    1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...

  2. Win7下“回收站已损坏,是否清空该驱动器上的回收站”解决方法

    最近买的移动硬盘,总是不能进行安全删除,有事还会提示“回收站已损坏,是否清空该驱动器上的回收站”,可以通过下面的命令进行解决: 开始–>运行–>cmd 点确定 在cmd窗口输入rd /s ...

  3. ESP32搭建2.虚拟机与物理机实现文件传输

    为后期操作方便,搭建实现虚拟机和物理机的文件直传. 1.     将安装好的虚拟机打开,点击VMware的虚拟机标签中的更新VMware Tools,等待一小会,虚拟CD盘中弹出文件 2.将压缩包Co ...

  4. C++中_T的用途

    1    C++语言中“_T”是什么意思? Visual C++里边定义字符串的时候,用_T来保证兼容性,VC支持ascii和unicode两种字符类型,用_T可以保证从ascii编码类型转换到uni ...

  5. 表单传值给@Controller

    <form action="springmvc/testModelAttributes" method="post"> <input type ...

  6. C#ATM

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. ViewBag赋值Html格式值

    今天再给自己总结一下,关于ViewBag赋值Html格式值,但是在web页显示不正常; 例如,ViewBag.Content = "<p>你好,我现在测试一个东西.</p& ...

  8. 斐讯 天天牛绑定教程 邀请码:8vozbf

    天天牛邀请码 8vozbf 可以领取4代牛 最近斐讯推出了天天牛养成计划. 不过官方没有任何的指示教程,所以个人分享一个教程给大家. 1. 先把把旧的钱包备份一下 ,切记!! 而且一定要记得自己设的密 ...

  9. L2-014 列车调度 (25 分)

    L2-014 列车调度 (25 分)   火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择 ...

  10. 4--Postman--Request&Response

    //var josndata=JSON.parse(responseBody);//获取body中返回的所有参数//tests["code"]=josndata.code===20 ...