<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

    #!/usr/bin/python'''凯撒密码'''a="gmbhqwertghjkcvbzn"s=[""]*len(a)for j in range(26) ...

  2. html5滑动事件代码

    $(".header").on("touchstart", function(e) { // 判断默认行为是否可以被禁用 if (e.cancelable) { ...

  3. websocket的属性readyState

    webSocket的readyState属性用来定义连接状态,该属性的值有下面几种: 0 :对应常量CONNECTING (numeric value 0), 正在建立连接连接,还没有完成.The c ...

  4. List<T>.ForEach 调用异步方法的意外

    有这么个异步方法 private static async Task<int> Compute(int s) { return await Task<int>.Run(() = ...

  5. VS添加WebService工具

    最近在做和WebService相关的项目,因为只是在学校里面用过,出来工作一直没有用到,所以不是很熟悉,于是自己配置了一个WebService工具给添加到了VS(VisualStudio)里面,其实就 ...

  6. 石头的Xcode学习列表

    http://www.cnblogs.com/JangoJing/tag/xcode/

  7. 2019-04-28-day042-HTML初识

    GET / HTTP/1.1 Host: 127.0.0.1:9001 Upgrade-Insecure-Requests: 1 Accept: text/html,application/xhtml ...

  8. JS Math方法

  9. CSV空行问题

    当写入CSV时生成的数据会有空行如果加入 newline =‘’ 不会新增空行 writefile = open('result.csv','w',newline =‘’) 原贴 https://bl ...

  10. Asp.net MVC Form认证,IIS改成集成模式后,FormsAuthentication.SetAuthCookie无效,Request.IsAuthenticated值,始终为false,页面提示HTTP 错误 401.0 - Unauthorized,您无权查看此目录或页面

    最近公司领导要求,IIS网站要由经典模式改为集成模式,以提高性能.改完之后,登录成功跳转到主页之后,页面提示“”HTTP 错误 401.0 - Unauthorized“,“您无权查看此目录或页面”, ...