python 用 PIL 模块 画验证码
PIL 简单绘画
def get_code_img(request):
from PIL import Image, ImageDraw, ImageFont
import random def random_color():
'''元组 (255,255,255)'''
return (random.randint(0,255),random.randint(0,255),random.randint(0,255)) def random_char():
'''传入需生成字符的个数,返回列表'''
temp = []
for i in range(4):
a = chr(random.randint(65, 90)) # 生成大写字母
b = chr(random.randint(97, 122)) # 生成小写字母
c = str(random.randint(0, 9)) # 生成数字,注意要转换成字符串类型
temp.append(random.choice([a,b,c]))
return temp def create_lines():
'''干扰线'''
size=(200,40)
for i in range(random.randint(2,7)):
begin = (random.randint(0,size[0]),random.randint(0,size[1]))
end = (random.randint(0,size[0]),random.randint(0,size[1]))
draw_obj.line((begin,end),fill=random_color(),width=random.randint(1,4)) def create_point():
'''干扰点'''
for i in range(80):
draw_obj.point((random.randint(0, 220), random.randint(0, 40)), fill=random_color())
x = random.randint(0,220)
y = random.randint(0,40)
z = random.randint(1,7)
a = random.randint(0,270)
b = random.randint(60,360)
draw_obj.arc((x, y, x+z, y+z), 0, 360, fill=random_color())
# 生成一个图片对象
img_obj = Image.new('RGB',(220,40),random_color())
#生成画笔对象
draw_obj = ImageDraw.Draw(img_obj)
# 生成字体对象
font_obj = ImageFont.truetype('static/fonts/kumo.ttf', 40)
# 将字符画到图片上
char_list = random_char()
# print(''.join(char_list))
request.session["code_img"] = "".join(char_list)
for i in range(len(char_list)):
draw_obj.text((10+50*i,0),char_list[i],fill=random_color(),font=font_obj)
create_lines()
create_point()
# img_obj.show() # 调用默认图片浏览器查看 # 不需要在硬盘上保存文件,直接在内存中加载就可以
from io import BytesIO
io_obj = BytesIO()
# 将生成的图片数据保存在io对象中
img_obj.save(io_obj, "png")
# 从io对象里面取上一步保存的数据
data = io_obj.getvalue()
return HttpResponse(data) <div class="form-group">
<label for="check_code" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-7 row">
<div class="col-sm-4">
<input id="check_code" name="check_code" type="text" class="form-control"/>
</div>
<div style="border-radius: 4px;overflow: hidden;">
<img id="get_code" src="/get_code_img/" alt="验证码加载失败">
</div>
</div>
</div>
<script>
$('#get_code').click(function () {
// 点击图片刷新验证码
$(this)[0].src += "?";
}); $('#check_code').focus(function () {
$('#error_msg').text('')
})
</script>
python 用 PIL 模块 画验证码的更多相关文章
- 基于PIL模块创建验证码图片
def get_valid_img(request): # 方式2:基于PIL模块创建验证码图片 from PIL import Image, ImageDraw, ImageFont from io ...
- 使用Python的PIL模块来进行图片对比
使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- python之pillow模块学习--验证码的生成和破解
一.基础学习 在Python中,有一个优秀的图像处理框架,就是PIL库,pip install pillow 示例1 from PIL import Image # 读取当前图片 im = Image ...
- Python利用PIL生成随机验证码图片
安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...
- Python 使用Pillow模块生成验证码
1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...
- python中PIL模块
Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内.如open.save.conver.show-等功能. open类 Image ...
- 【Python】PIL模块
Python自建库,在爬虫等基础应用中更加简单好记,做整理以备自查. 目录 Image模块 open类.Save类.format类.Mode类.convert类.Size类.Info类.new类.Co ...
- Python——用turtle模块画海龟的第一步
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
随机推荐
- 安卓小程序的一次bug调试,报错:java.lang.NullPointerException,logcat学习
做实验的时候,调试了很久后模拟器执行后,app还是会崩溃并停止运行,错误如下. 因为初学,所以也不知道怎么使用调试工具,也不懂看日志,经过学习后尝试这查看了LogCat日志上面有这样的提示: 其中引起 ...
- Qt之界面(自定义标题栏、无边框、可移动、缩放)
效果 自定义标题栏 titleBar.h #ifndef TITLEBAR_H #define TITLEBAR_H #include <QLabel> #include <QPus ...
- [POI2012]Vouchers
Description 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个. 正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. ...
- 题解报告:hdu 5750 Dertouzos(最大真约数、最小素因子)
Problem Description A positive proper divisor is a positive divisor of a number n, excluding n itsel ...
- 2018年全国多校算法寒假训练营练习比赛(第五场):A题:逆序数(树状数组or归并排序)
题目描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数.比如一个序列为4 5 1 3 2, 那么这个序列 ...
- 441 Arranging Coins 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...
- L 裁纸片 贪心 + 模拟
https://biancheng.love/contest-ng/index.html#/123/problems 如果只是输出最小的值,那么好办,a升序,b降序,这样是最优的. 但是需要次数,这就 ...
- AJPFX关于modifier总结
修饰符总结 Modifiers 函数修饰符始终在返回值类型之前!!! 变量修饰符始终在变量类型之前!!!---------------------------------- ...
- 聊聊mq的使用场景
mq的作用 通过异步方式对系统解耦 增加系统的并发处理能力 通过异步方式对系统解耦 以用户注册为例,一般情况下: 分下一下,上面过程存在的一些问题: 注册过程会调用4个服务(注册服务.邮件服务.短信服 ...
- vuetifyjs简介及其使用
何为 vuetify 一个vue ui库,提供vue组件供使用.根据 Google Material Design 指南实现(https://material.io/).Vuetify支持SSR(服务 ...