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 模块 画验证码的更多相关文章

  1. 基于PIL模块创建验证码图片

    def get_valid_img(request): # 方式2:基于PIL模块创建验证码图片 from PIL import Image, ImageDraw, ImageFont from io ...

  2. 使用Python的PIL模块来进行图片对比

    使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...

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

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

  4. python之pillow模块学习--验证码的生成和破解

    一.基础学习 在Python中,有一个优秀的图像处理框架,就是PIL库,pip install pillow 示例1 from PIL import Image # 读取当前图片 im = Image ...

  5. Python利用PIL生成随机验证码图片

    安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...

  6. Python 使用Pillow模块生成验证码

    1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...

  7. python中PIL模块

    Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内.如open.save.conver.show-等功能. open类 Image ...

  8. 【Python】PIL模块

    Python自建库,在爬虫等基础应用中更加简单好记,做整理以备自查. 目录 Image模块 open类.Save类.format类.Mode类.convert类.Size类.Info类.new类.Co ...

  9. Python——用turtle模块画海龟的第一步

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

随机推荐

  1. 我的ubuntu连vi都没有??那在命令行怎么编辑文件??

    今天弄了个docker下的ubuntu官方镜像,想在镜像里做一点实验,免得把自己的环境写得乱七八糟. 把代码文件mount进去之后,开始编译,但是镜像太干净了,什么工具都没有,于是先装cmake ap ...

  2. 从零开始通过idea插件将一个spring boot项目部署到docker容器里运行

    实操:将一个spring boot项目部署到docker容器里运行 实验需要的环境: 腾讯云+Ubuntu 16.04 x64+idea+插件docker integration+daocloud 第 ...

  3. 递推DP UVA 473 Raucous Rockers

    题目传送门 题意:n首个按照给定顺序存在m张光盘里,每首歌有播放时间ti,并且只能完整的存在一张光盘里,问最多能存几首歌 分析:类似01背包和完全背包,每首歌可存可不存,存到下一张光盘的情况是当前存不 ...

  4. AJPFX关于代码块的总结

    代码块:        {                执行语句;        }(1) 当出现在局部位置时, 为局部代码块.        局部位置: 如语句块中, 函数中, 构造代码块中, 静 ...

  5. Activiti工作流和shiro权限管理关系图

  6. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  7. css3 transform + deviceorientation实现图片旋转效果

    1. 陀螺仪deviceorientation的使用,参考<关于陀螺仪deviceorientation>https://segmentfault.com/a/11900000071838 ...

  8. postgresql update from

    1,update   from   关联表的更新 update table a set name=b.name from table B b  where a.id=b.id; update test ...

  9. jQuery动画处理

    $(selector).hide(speed,callback);隐藏 $(selector).show(speed,callback);显示 $(selector).toggle(speed,cal ...

  10. 拼图游戏源码-swift版项目源码

    作者fanyinan,源码PuzzleProject,公司的项目中需要一个拼图游戏,之前有手动拼图和随机打乱的功能,近期又由于个(xian)人(zhe)爱(dan)好(teng)自己加入了自动拼图功能 ...