制作随机验证码,需要如下知识点:

1、随机验证码的制作(这里用的是random模块随机产生字符)

2、图片的制作

3、随机直线干扰项

4、其他随机干扰项

代码如下:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import random

#随机颜色
def get_random_color():
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
return (r,g,b)

#随机位置
def get_random_position(x,y):
x = random.randint(0,x - 50)
y = random.randint(0,y - 15)
return (x,y)

#线条随机俩个坐标
def get_random_line_position(x,y):
x1 = random.randint(0,x)
y1 = random.randint(0,y)
x2 = random.randint(0,x)
y2 = random.randint(0,y)
return (x1,y1,x2,y2) def get_random_eci_position(x,y):
x1 = random.randint(0,x)
y1 = random.randint(0,y)
x2 = x1 + 4
y2 = y1 + 4
return (x1,y1,x2,y2)

#随机字符
def get_random_str():
num = str(random.randint(0,9))
low_letter = chr(random.randint(97,122))
upper_letter = chr(random.randint(65,90))
#print(num,low_letter,upper_letter)
randomStr = random.choice([num,low_letter,upper_letter])
return randomStr x = 200
y = 60
#创建一个随机颜色的图形
img = Image.new('RGB',(x,y),get_random_color()) #在图形上随机位置显示4个随机颜色的文字
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('segoeuib.ttf',20)
randomStr = ''
for i in range(4):
v = get_random_str()
randomStr += v
#print(randomStr)
draw.text(get_random_position(x,y),randomStr,get_random_color(),font = font) #在图形上随机位置显示随机颜色的直线
for i in range(8):
draw.line(get_random_line_position(x,y),get_random_color())
draw.ellipse(get_random_eci_position(x,y),get_random_color())
draw.arc(get_random_line_position(x,y),0,90,get_random_color()) img.show()

执行效果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAA7CAYAAAA+XsUpAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAfVSURBVHhe7Zp7iFRVHMd/MzuvfO2qbdqutT4yTUvNDMkiQUixMhOLokKQIkj6oygoAv8SIqGwP8JACgkqgqi0Aj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OKHEDyyCmKKgdiRAwmsIE61SbJJIoMQPDIJ4oQULIEQxO2ZASVB/BBFRiF4/BKElcHECSlYlBPEzwHaizRRQQgetwURiYA4LYMIqQXxUwYrnJZERSFYnJDDSgAWL2QQIY0gMspgRT2SqChEpQ28NfUUbEi/Q1f28GvzV4NvgvBCyCqDFdVKonpCjISsJ1hO4YkgKqVDLYgkqVeI1s3P0Qqg+6U3aSUvWpAaCaoMVqAkyMNblhuP9SQEK4eJ7JJoQSoQNhkQq4RwYnhXTZCgy4FULUgYZUBqaZnqlUQLIh8VBWGlCIMMSL0zhJOSuClHdOsBWgEMbFhAq9rQCRIC3DhlcqLdchNWDhO7kgSd0AnihhAiZJZEC1I9gRfEKyFEoCSIbKJoQaoncIL4KYQVMqaJEzNIGFBeEBmFECH7XKIRo5wgqgghQkuiGgD/A0KignGohR+1AAAAAElFTkSuQmCC" alt="" />

python之使用PIL模块制作随机验证码的更多相关文章

  1. Python使用PIL模块生成随机验证码

    PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...

  2. canvas制作随机验证码

    看到人家彩色背景的验证码想测试一下: 创建html代码: <canvas id="myCanvas" width="200" height="1 ...

  3. python之~利用PIL模块在图片上写写画画

    借鉴了教程: http://yxnt.github.io/2016/05/15/Pillow-Python3.5/ 完成作业如下: 后来学着写给自己的图片加了水印. from PIL import I ...

  4. JAVA_SE基础——71.Random类制作随机验证码

    public class Demo5 { public static void main(String[] args) { char[] arr={'s','b','g','h','a','c'}; ...

  5. Python Selenium、PIL、pytesser 识别验证码

    思路: 使用Selenium库把带有验证码的页面截取下来 利用验证码的xpath截取该页面的验证码 对验证码图片进行降噪.二值化.灰度化处理后再使用pytesser识别 使用固定的账户密码对比验证码正 ...

  6. [ Python - 5 ] 通过random模块生成随机字符串

    import random checkcode = '' for i in range(4): if i == random.randint(0,3): current = chr(random.ra ...

  7. Python之路----------random模块

    随机数模块: import random #随机小数 print(random.random()) #随机整数 print(random.randint(1,5))#他会打印5 #随机整数 print ...

  8. python基础11 ---函数模块1

    函数模块 一.函数模块的作用(为什么要有函数模块) 1.函数模块可以减少代码量 2.函数模块方便阅读 3.函数模块维护性强二.函数模块的本质以及调用方法 1.函数模块的本质就是一个.py结尾的文件,该 ...

  9. python模块之PIL模块(生成随机验证码图片)

    PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...

随机推荐

  1. Android 二维码扫描/生成

    先看看实现效果 1.在module的build.gradle中执行compile操作 compile 'cn.yipianfengye.android:zxing-library:2.2' 2.在Ap ...

  2. .net C# 利用Session防重复点击防重复提交

    <body>    <form id="form1" runat="server">    <div>        < ...

  3. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

  4. 【.Net】 大文件可使用的文本分组统计工具(附带源码,原创)

    本工具可实现的效果: 1.读取大文件(大于1GB) 2.根据分隔符分割后的列分组 3.速度快. 4.处理过程中,可以随时停止处理,操作不卡死. 5.有对当前内存的实时监测,避免过多占用内存,影响系统运 ...

  5. Ubuntu部署可视化爬虫Portia2.0环境以及入门

    http://www.cnblogs.com/kfpa/p/Portia.html http://brucedone.com/archives/986

  6. VS中程序包错误,引用错误该如何解决

    1.找到包的文件.packages.config 对应于: 2.删除掉 packages.config 报错的项.然后再重新添加一次.就没有解决的不了的问题. 是不是很爽.....

  7. RNA测序的质量控制

    RNA测序的质量控制 发表评论 3,112 A+ 所属分类:Transcriptomics   收  藏 ENCODE项目向我们揭示,人类基因组中超过70%能得到转录,只不过不会发生在同一个细胞里.为 ...

  8. 【APP测试(Android)】--安全测试

  9. [MACHINE LEARNING] Can we predict voting outcomes?

    1. CART Tree library(rpart)library(rpart.plot)CTree = rpart(Party ~ . -USER_ID, data = train, method ...

  10. s6-2 UDP

    User Datagram Protocol  UDP 是一个无连接的(connectionless)的传输层协议  UDP传输数据段,无须建立连接  UDP 在 RFC 768中描述  很多 ...