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网络爬虫出现乱码问题的解决方法
Jan 05 Python
Python 字符串大小写转换的简单实例
Jan 21 Python
python 读取txt,json和hdf5文件的实例
Jun 05 Python
python 用正则表达式筛选文本信息的实例
Jun 05 Python
python实现单链表中删除倒数第K个节点的方法
Sep 28 Python
Python中asyncio模块的深入讲解
Jun 10 Python
Python之pymysql的使用小结
Jul 01 Python
python实现Pyecharts实现动态地图(Map、Geo)
Mar 25 Python
通过代码实例解析Pytest运行流程
Aug 20 Python
python 常见的排序算法实现汇总
Aug 21 Python
python树莓派通过队列实现进程交互的程序分析
Jul 04 Python
利用Python实现Picgo图床工具
Nov 23 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
php初学者写及时补给skype用户充话费的小程序
2008/11/02 PHP
PHP使用JSON和将json还原成数组
2015/02/12 PHP
PHP邮箱验证示例教程
2016/06/01 PHP
Javascript学习笔记 delete运算符
2011/09/13 Javascript
11个用于提高排版水平的基于jquery的文字效果插件
2012/09/14 Javascript
JavaScript字符串对象substr方法入门实例(用于截取字符串)
2014/10/16 Javascript
js实现适用于素材网站的黑色多级菜单导航条效果
2015/08/24 Javascript
JavaScript编写页面半透明遮罩效果的简单示例
2016/05/09 Javascript
用AngularJS的指令实现tabs切换效果
2016/08/31 Javascript
Javascript 链式作用域详细介绍
2017/02/23 Javascript
jQuery实现贪吃蛇小游戏(附源码下载)
2017/03/04 Javascript
AngularJS基于http请求实现下载php生成的excel文件功能示例
2018/01/23 Javascript
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
vue+echarts实现可拖动节点的折线图(支持拖动方向和上下限的设置)
2019/04/12 Javascript
JS数组push、unshift、pop、shift方法的实现与使用方法示例
2020/04/29 Javascript
python 写的一个爬虫程序源码
2016/02/28 Python
[原创]pip和pygal的安装实例教程
2017/12/07 Python
对Python w和w+权限的区别详解
2019/01/23 Python
对python中的装包与解包实例详解
2019/08/24 Python
pygame实现俄罗斯方块游戏(AI篇2)
2019/10/29 Python
python多线程实现代码(模拟银行服务操作流程)
2020/01/13 Python
Python阶乘求和的代码详解
2020/02/14 Python
Noon埃及:埃及在线购物
2019/11/26 全球购物
JACK & JONES荷兰官网:男士服装和鞋子
2021/03/07 全球购物
Auguste The Label官网:澳大利亚一家精品女装时尚品牌
2020/06/14 全球购物
什么是方法的重载
2013/06/24 面试题
《自选商场》教学反思
2014/02/14 职场文书
个人授权委托书范本
2014/04/03 职场文书
干部考核评语
2014/04/29 职场文书
会计系毕业求职信
2014/08/07 职场文书
先进事迹材料范文
2014/12/29 职场文书
小班教师个人总结
2015/02/05 职场文书
拔河比赛新闻稿
2015/07/17 职场文书
golang elasticsearch Client的使用详解
2021/05/05 Golang
深入理解Vue的数据响应式
2021/05/15 Vue.js
mongodb数据库迁移变更的解决方案
2021/09/04 MongoDB