资源简介
利用Python生成验证码图片
代码和字体文件压缩包

代码片段和文件信息
# 导入库文件
from PIL import ImageImageFontImageDrawImageFilter
import stringrandomos
# 方法:获得四个随机字母
def getRandomChar():
return [random.choice(string.ascii_letters)for _ in range(4)]
# 方法:获取随机颜色
def getRandomColor():
return (random.randint(30100)random.randint(30100)random.randint(30100))
# 主方法:获取验证码图片
def getCodePicture(fontPathoutPath):
# 图片宽高
width = 240
height = 60
# 创建空白画布
image = Image.new(‘RGB‘(widthheight)(180180180))
# 验证码字体
font = ImageFont.truetype(fontPath40)
# 画出画布,获取画布对象
draw = ImageDraw.Draw(image)
# 创建验证码对象
code = getRandomChar()
# 把验证码画到画布上
for x in range(4):
draw.text((60*x+100)code[x]font=fontfill=getRandomColor())
# 填充噪点
for _ in range(random.randint(15003000)):
draw.point((random.randint(0width)random.randint(0height))fill = getRandomColor())
# 模糊处理
image = image.filter(ImageFilter.BLUR)
# 保存
image.save(outPath+“\\“+““.join(code)+“.jpg“‘jpeg‘)
if __name__ == ‘__main__‘:
getCodePicture(“simsun.ttf“os.getcwd())
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1217 2017-07-14 17:21 codepicture.py
文件 10499104 2012-05-14 11:30 simsun.ttf
- 上一篇:python实现淘宝爬虫
- 下一篇:计算机二级学习资料python
相关资源
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
评论
共有 条评论