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. PhpStorm Xdebug远程调试环境搭建原理分析及问题排查

    2017年05月26日  经验心得 目录   一. 环境介绍 二. 远程环境配置 2.2 Xdebug安装 2.3 配置 三. 本地phpstorm配置 3.1 下载远程代码 3.2 添加php解释器 ...

  2. POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)

    Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...

  3. Guilty Prince LightOJ - 1012

    Guilty Prince LightOJ - 1012 #include<cstdio> #include<cstring> ][]; int ans,h,w,T,TT; ] ...

  4. 【转载】WebConfigurationManager和ConfigurationManager

    原文链接 今天在写程序时偶然发现有的示例代码中是用WebConfigurationManager获取web.config中的配置信息的,因为之前一直都是用的ConfigurationManager,所 ...

  5. jmeter(十五)Jmeter默认报告优化

    一.本文目的: 之前写了两篇文章搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)和ANT批量执行Jmeter脚本,功能实现上都没有什么问题,但是最后生成的报告有一点小问题,虽然不影响使 ...

  6. C#菜鸟正则表达式一

    LZ菜鸟,仅整理笔记,顺带记录一下,谓之增加印象. LZ认为,没必要太纠结原理,模型, 屌丝能用就对了,剩下的事情用多了自然会去探索. 中文:正则表达式,英文:Regular  ExPression, ...

  7. CF792C Divide by Three

    思路: dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. jsp 页面获取当前路径

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  9. 浅谈2015新版 U-Boot

    过了挺长一断时间没有看U-BOOT了,这两天下载了新版的UBOOT源码(之前看的一些书都是基于早好多年的源码来讲述,总感觉心里有点不对劲,也许是我比较喜新的原因吧,不过小弟我并没有厌旧哈),好了不多扯 ...

  10. (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务

    http://blog.csdn.net/yerenyuan_pku/article/details/72514034 Restful就是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格 ...