基于PIL模块创建验证码图片
def get_valid_img(request):
# 方式2:基于PIL模块创建验证码图片
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO def get_random_color():
import random
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # img=Image.new("RGB",(350,38),get_random_color())
# f=open("valid.png","wb")
# img.save(f,"png")
# with open("valid.png","rb") as f:
# data=f.read() # 方式3:
# img=Image.new("RGB",(350,38),get_random_color())
# f=BytesIO()
# img.save(f,"png")
# data=f.getvalue() # # 方式4:完善文本
#
# img=Image.new("RGB",(350,38),get_random_color())
# draw=ImageDraw.Draw(img)
# font=ImageFont.truetype("static/font/kumo.ttf",32)
# draw.text((0,0),"python!",get_random_color(),font=font)
#
# # 写与读
# f=BytesIO()
# img.save(f,"png")
# data=f.getvalue() # 方式5:
#
img = Image.new("RGB", (170, 35), get_random_color())
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("static/font/kumo.ttf", 32)
keep_str = ""
for i in range(4):
random_num = str(random.randint(0, 9))
random_lowalf = chr(random.randint(97, 122))
random_upperalf = chr(random.randint(65, 90))
random_char = random.choice([random_num, random_lowalf, random_upperalf])
draw.text((i * 30 + 30, 0), random_char, get_random_color(), font=font)
keep_str += random_char # 加噪点
width = 170
height = 35
for i in range(6):
x1 = random.randint(0, width)
x2 = random.randint(0, width)
y1 = random.randint(0, height)
y2 = random.randint(0, height)
draw.line((x1, y1, x2, y2), fill=get_random_color()) for i in range(6):
draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())
# 写与读
f = BytesIO()
img.save(f, "png")
data = f.getvalue()
print('keep_str', keep_str)
# 将验证码存在各自的session中
request.session['keep_str'] = keep_str return HttpResponse(data)
基于PIL模块创建验证码图片的更多相关文章
- 使用Python的PIL模块来进行图片对比
使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...
- Python利用PIL生成随机验证码图片
安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- python 用 PIL 模块 画验证码
PIL 简单绘画 def get_code_img(request): from PIL import Image, ImageDraw, ImageFont import random def ra ...
- C# 创建验证码图片
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...
- 纯代码系列:Python实现验证码图片(PIL库经典用法用法,爬虫12306思路)
现在的网页中,为了防止机器人提交表单,图片验证码是很常见的应对手段之一.这里就不详细介绍了,相信大家都遇到过. 现在就给出用Python的PIL库实现验证码图片的代码.代码中有详细注释. #!/usr ...
- python模块之PIL模块(生成随机验证码图片)
PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...
- 使用模块PIL 生成 随机验证码
--------------默认自己无能,无疑是给失败制造机会!你认为自己是什么样的人,就将成为什么样的人. 要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创 ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
随机推荐
- 吴裕雄 python 神经网络——TensorFlow训练神经网络:不使用指数衰减的学习率
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...
- java实现文字转语音功能(仅Windows)
一.pom.xml引入jar包依赖 <!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字转语音 --> <depend ...
- HDU1875 畅通工程再续
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...
- Python学习第二十六课——PyMySql(python 链接数据库)
Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...
- 蓝桥杯-铺瓷砖(dfs)
问题描述 有一长度为N(1< =N< =10)的地板,给定两种不同瓷砖:一种长度为1,另一种长度为2,数目不限.要将这个长度为N的地板铺满,一共有多少种不同的铺法? 例如,长度为4的地面一 ...
- [ DLPytorch ] 线性回归&Softmax与分类模型&多层感知机
线性回归 基础知识 实现过程 学习笔记 批量读取 torch_data = Data.TensorDataset(features, labels) dataset = Data.DataLoader ...
- 微信跳一跳辅助JAVA 自动模拟点击
工具:ADB 原理: 开始游戏后,使用ADB工具让手机截屏发送到电脑 分析图像中小人与目标中心点间的距离,根据一定比例计算出需要触屏的时间 使用ADB进行模拟点击(触屏)相应的时间,完成精准跳跃 程序 ...
- python 顺序执行任务
#!/usr/bin/python import os import time start_command="sh start-etl.sh " es_mac_confPath = ...
- 【网寻】mui - 点击事件
//本来是需要这样的,结果点击无响应 $(".mui-control-item").on("click",function(event){ alert(1) } ...
- Python学习笔记002
字符编码:把二进制字符翻译成字符 ASCII码表 256 一个字节,8个比特 支持中文: GB2312 GBK1.0 GB18030 BIG5(台湾) unicode UTF-8 开头定义 ...