Joke Collection Website - Blessing messages - 大蟒怎么批量提交验证码
大蟒怎么批量提交验证码
现在就给出用计算机编程语言的PIL库实现验证码图片的代码。代码中有详细注释. p
#!/usr/bin/env python
# coding = utf-8
从PIL导入随机
导入图像,ImageDraw,ImageFont,Image filter
_ letter _ cases = " abcdefghjkmnpqrstuvwxy " #小写字母,去除可能干扰的I,l,o,z
_ upper _ cases = _ letter _ cases . upper()#大写字母
_numbers = ' '。join(map(str,range(3,1)))数字
init_chars = ' '。join((_letter_cases,_upper_cases,_ numbers))
def create _ validate _ code(size =(12,3),
chars=init_chars,
img_type="GIF ",
mode="RGB ",
bg_color=(255,255),
fg_color=(,,255),
font_size=18,
font _ type = " ae _生成验证码图片
@param size:图片的大小,格式(宽,高),默认为(12,3)@ param chars:允许的字符集合,格式字符串
@param img_type:图片保存的格式,默认为GIF,可选的为GIF、JPEG、TIFF、PNG
@param模式:图片模式,默认为RGB
@参数bg_color:背景颜色,默认为白色
@param fg_color:前景色,验证码字符颜色,默认为蓝色# ff @ param font _ size:验证码字体大小
@param font_type:验证码字体,默认为AE _ alarabiya . TTF
@ param length:验证码字符个数
@ param draw _ lines:是否划干扰线
@param n_lines:干扰线的条数范围,格式元组,默认为(1, 2),只有绘制线条为真实的时有效
@param绘制点数:是否画干扰点
@param point_chance:干扰点出现的概率,大小范围[,1]
@return: []: PIL图像实例
@return: [1]:验证码图片中的字符串' ' '
宽度,高度=尺寸#宽,高
img = Image.new(模式、尺寸、背景颜色)#创建图形
draw = ImageDraw。绘制(img) #创建画笔def get_chars():
' ' '生成给定长度的字符串,返回列表格式' '
return random.sample(chars,length)
def create_lines():
' ' '绘制干扰线' ' ' '
line _ num = random . randint(* n _ line)#干扰线条数
对于范围内的I(line _ num):
#起始点
begin = (random.randint(,size[]),random.randint(,size[1])
#结束点
end = (random.randint(,size[]),random.randint(,size[1])
draw . line([begin,end],fill=(,,))
def create_points():
' ' '绘制干扰点' '
chance = min(1,max(,int(point_chance))) #大小限制在[,1]
对于xrange中的w(宽度):
对于xrange中的h(高度):
tmp = random.randint(,1)
如果tmp & gt1 - chance:
draw.point((w,h),fill=(,,))
def create_strs():
' ' '绘制验证码字符' '
c _ chars = get _ chars()
strs = ' % s ' % ' '。join(c_chars) #每个字符前后以空格隔开
font = image font . truetype(font _ type,font_size)
font_width,font _ height = font . getsize(strs)
draw . text((width-font _ width)/3,(height - font_height) / 3),
strs,font=font,fill=fg_color)
return ' '。join(c _ chars)
if draw _ lines:
create _ lines()
if draw _ points:
create _ points()
strs = create _ strs()
#图形扭曲参数
params =[1-float(random . randint(1,2)) / 1,
,
,
1 - float(random.randint(1,1)) / 1,
float(random.randint(1,2)) / 5,
.1,
float(random.randint(1,2))/5
]
img = img . transform(size,Image透视,参数)#创建扭曲
img = img.filter(ImageFilter。EDGE_ENHANCE_MORE) #滤镜,边界加强(阈值更大)
return img,strs
if _ _ name _ _ = = " _ _ main _ _ ":
code _ img = create _ validate _ code()
code _ img . save(" validate . GIF "," GIF")
最后结果返回一个元组,第一个返回值是图像类的实例,第二个参数是图片中的字符串(比较是否正确的作用)。
最后结果返回一个元组,第一个返回值是图像类的实例,第二个参数是图片中的字符串(比较是否正确的作用)。
需要提醒的是,如果在生成ImageFont.truetype实例的时候抛出IOError异常,有可能是运行代码的电脑没有包含指定的字体,需要下载安装p
生成的验证码图片效果:
这时候,细心的同学可能要问,如果每次生成验证码,都要先保存生成的图片,再显示到页面。这么做让人太不能接受了。这个时候,我们需要使用大蟒内置的斯特林乔模块,它有着类似文件对象的行为,但是它操作的是内存文件。于是,我们可以这么写代码:
尝试:
将cStringIO作为StringIO导入
除了import error:
import StringIO
mstream = StringIO。StringIO()
img = create _ validate _ code()[]
img . save(mstream," GIF")
这样,我们需要输出的图片的时候只要使用" mstream.getvalue()"即可。比如在姜戈里,我们首先定义这样的url:
从django.conf.urls.defaults导入*
URL patterns = patterns('example.views',
url(r'^validate/$','验证',name= '验证'),
)
在视图中,我们把正确的字符串保存在会议中,这样当用户提交表单的时候,就可以和会议中的正确字符串进行比较. from django.shortcuts导入HttpResponse
from validate导入create _ validate _ code
def validate(request):
mstream = StringIO。StringIO()
validate _ code = create _ validate _ code()
img = validate _ code[]
img . save(mstream," GIF ")
request . session[' validate ']= validate _ code[1]
return HttpResponse(mstream . getvalue()," image/gif ")
- Previous article:What is Sichuan Thousand Lines SMS Verification?
- Next article:Do Japanese send and receive short messages by email?
- Related articles
- What does Dolby of iQiyi do?
- How to turn on caller ID? (mobile phone)
- Vb.net hangs when the serial port is closed.
- Can Xiaomi air conditioner switch function push messages?
- In the morning, I received a text message saying that I won the second prize of China Talent, but I can only pay the bonus by dialing 5000 to the account of the chief financial officer first. If I don
- Is Liu's funeral good or bad? Why did he say that Wu Xie killed the second uncle?
- Micro-loan repayment notification SMS
- The composition "Function of SMS" is about 8 words.
- Dynamic of Happy Dragon Boat Festival greetings
- How to set Xiaomi mobile phone during the call? Notify me of other calls.