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


Posted in Python onNovember 21, 2017

Python生成随机验证码,需要使用PIL模块,具体内容如下

安装:

pip3 install pillow

基本使用

1. 创建图片

from PIL import Image
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
 
# 在图片查看器中打开
# img.show()
 
# 保存在本地
with open('code.png','wb') as f:
 img.save(f,format='png')

2. 创建画笔,用于在图片上画任意内容

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')

3. 画点

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示坐标
# 第二个参数:表示颜色
draw.point([100, 100], fill="red")
draw.point([300, 300], fill=(255, 255, 255))

4. 画线

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标
# 第二个参数:表示颜色
draw.line((100,100,100,300), fill='red')
draw.line((100,100,300,100), fill=(255, 255, 255))

5. 画圆

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
draw.arc((100,100,300,300),0,90,fill="red")

6. 写文本

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
draw.text([0,0],'python',"red")

7. 特殊字体文字

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示字体文件路径
# 第二个参数:表示字体大小
font = ImageFont.truetype("kumo.ttf", 28)
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
# 第四个参数:表示颜色
draw.text([0, 0], 'python', "red", font=font)

图片验证码

import random

def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
code = []
img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')

def rndChar():
"""
生成随机字母 
:return:
"""
return chr(random.randint(65, 90))

def rndColor():
"""
生成随机颜色
:return:
"""
return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

# 写文字
font = ImageFont.truetype(font_file, font_size)
for i in range(char_length):
char = rndChar()
code.append(char)
h = random.randint(0, 4)
draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

# 写干扰点
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

# 写干扰圆圈
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

# 画干扰线
for i in range(5):
x1 = random.randint(0, width)
y1 = random.randint(0, height)
x2 = random.randint(0, width)
y2 = random.randint(0, height)

draw.line((x1, y1, x2, y2), fill=rndColor())

img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
return img,''.join(code)


if __name__ == '__main__':
# 1. 直接打开
# img,code = check_code()
# img.show()

# 2. 写入文件
# img,code = check_code()
# with open('code.png','wb') as f:
# img.save(f,format='png')

# 3. 写入内存(Python3)
# from io import BytesIO
# stream = BytesIO()
# img.save(stream, 'png')
# stream.getvalue()

# 4. 写入内存(Python2)
# import StringIO
# stream = StringIO.StringIO()
# img.save(stream, 'png')
# stream.getvalue()

pass

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现定制交互式命令行的方法
Jul 03 Python
python获取一组汉字拼音首字母的方法
Jul 01 Python
python中解析json格式文件的方法示例
May 03 Python
python+mysql实现学生信息查询系统
Feb 21 Python
Python学习笔记之自定义函数用法详解
Jun 08 Python
详解Matplotlib绘图之属性设置
Aug 23 Python
python 生成器和迭代器的原理解析
Oct 12 Python
pytorch GAN生成对抗网络实例
Jan 10 Python
python super用法及原理详解
Jan 20 Python
Tensorflow加载Vgg预训练模型操作
May 26 Python
python打开文件的方式有哪些
Jun 29 Python
PIP和conda 更换国内安装源的方法步骤
Sep 21 Python
Python3中条件控制、循环与函数的简易教程
Nov 21 #Python
Python3 循环语句(for、while、break、range等)
Nov 20 #Python
Python虚拟环境项目实例
Nov 20 #Python
Python插件virtualenv搭建虚拟环境
Nov 20 #Python
使用tensorflow实现AlexNet
Nov 20 #Python
Django在win10下的安装并创建工程
Nov 20 #Python
Python2与python3中 for 循环语句基础与实例分析
Nov 20 #Python
You might like
PHP4之真OO
2006/10/09 PHP
PHP 全角转半角实现代码
2010/05/16 PHP
简单实用的PHP防注入类实例
2014/12/05 PHP
yii2整合百度编辑器umeditor及umeditor图片上传问题的解决办法
2016/04/20 PHP
最新最全PHP生成制作验证码代码详解(推荐)
2016/06/12 PHP
JavaScript编程的10个实用小技巧
2014/04/18 Javascript
jQuery CSS()方法改变现有的CSS样式
2014/08/20 Javascript
JavaScript中document对象使用详解
2015/01/06 Javascript
JS实现点击文字对应DIV层不停闪动效果的方法
2015/03/02 Javascript
javascript实现一个数值加法函数
2015/06/26 Javascript
使用JS动态显示文本
2017/09/09 Javascript
记录一篇关于redux-saga的基本使用过程
2018/08/18 Javascript
vue-cli 3.x 修改dist路径的方法
2018/09/19 Javascript
傻瓜式vuex语法糖kiss-vuex整理
2018/12/21 Javascript
vue实现表单录入小案例
2019/09/27 Javascript
VUE注册全局组件和局部组件过程解析
2019/10/10 Javascript
JS使用正则表达式判断输入框失去焦点事件
2019/10/16 Javascript
详解Vue+elementUI build打包部署后字体图标丢失问题
2020/07/13 Javascript
Python首次安装后运行报错(0xc000007b)的解决方法
2016/10/18 Python
初学python的操作难点总结(新手必看篇)
2017/08/03 Python
对python sklearn one-hot编码详解
2018/07/10 Python
django+tornado实现实时查看远程日志的方法
2019/08/12 Python
python利用openpyxl拆分多个工作表的工作簿的方法
2019/09/27 Python
python 安装移动复制第三方库操作
2020/07/13 Python
django中cookiecutter的使用教程
2020/12/03 Python
CSS3使用transition实现的鼠标悬停淡入淡出
2015/01/09 HTML / CSS
大型会议策划方案
2014/05/17 职场文书
五四青年节演讲稿
2014/05/26 职场文书
揭牌仪式策划方案
2014/05/28 职场文书
2014幼儿园教师师德师风演讲稿
2014/09/10 职场文书
党的群众路线教育实践活动学习笔记
2014/11/05 职场文书
2014年仓库管理工作总结
2014/12/17 职场文书
学习保证书
2015/01/17 职场文书
安全员岗位职责
2015/02/10 职场文书
小学少先队活动总结
2015/05/08 职场文书
Python学习之os包使用教程详解
2022/03/21 Python