Python生成随机验证
Python生成随机验证码
Python生成随机验证码,需要使用PIL模块.
安装:
|
1
|
pip3 install pillow |
基本使用
1. 创建图片
|
1
2
3
4
5
6
7
8
9
|
from PIL import Imageimg = 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. 创建画笔,用于在图片上画任意内容
|
1
2
|
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))draw = ImageDraw.Draw(img, mode='RGB') |
3. 画点
|
1
2
3
4
5
6
|
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. 画线
|
1
2
3
4
5
6
|
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. 画圆
|
1
2
3
4
5
6
7
|
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. 写文本
|
1
2
3
4
5
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. 特殊字体文字
|
1
2
3
4
5
6
7
8
9
10
|
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) |
图片验证码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import randomdef 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生成随机数组的方法小结
Python生成随机数组的方法小结 本文实例讲述了Python生成随机数组的方法.分享给大家供大家参考,具体如下: 研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,今天把Pyt ...
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- python生成随机日期字符串
python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...
- Python生成随机字符串
利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...
- python生成随机整数
python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...
- C#生成随机验证吗例子
C#生成随机验证吗例子: public class ValidateCode : IHttpHandler, IRequiresSessionState { HttpContext context; ...
- Python生成随机验证码,大乐透号码
实例笔记之生成随机号码 扩展知识 - yield(生成器) 随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__' ...
- python 生成随机字符串
1.生成随机字符串 #数字+字母+符号 def getRandChar(n): l = [] #sample = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^ ...
- python 生成随机图片验证码
1.安装pillow模块 pip install pillow (1)创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的 ...
随机推荐
- Java里if和else的用法
前言 在上一篇文章中,壹哥给大家讲解了Java里的输入与输出语句,现在你知道怎么用了吗?接下来我们继续往下学习Java里的流程控制语句,今天先给大家讲一下if和else这一对好基友,这个知识点可以说是 ...
- node-sass与node版本对照图
- opencv-python 4.9.2. 轮廓特征
矩 图像的矩可帮助你计算某些特征,如对象的质心,对象的面积等特征.函数cv.moments()给出了计算的所有矩值的字典. 从这一刻起,你可以提取有用的数据,如面积,质心等.质心由关系给出, $$ C ...
- Django笔记十六之aggregate聚合操作
本文首发于微信公众号:Hunter后端 原文链接:Django笔记十六之aggregate聚合操作 这一篇笔记介绍一下关于聚合的操作,aggregate. 常用的聚合操作比如有平均数,总数,最大值,最 ...
- 《花雕学AI》13:早出对策,积极应对ChatGPT带来的一系列风险和挑战
ChatGPT是一款能和人类聊天的机器人,它可以学习和理解人类语言,也可以帮人们做一些工作,比如翻译.写文章.写代码等.ChatGPT很强大,让很多人感兴趣,也让很多人担心. 使用ChatGPT有一些 ...
- JUC(一)JUC简介与Synchronized和Lock
1 JUC简介 JUC就是java.util.concurrent的简称,这是一个处理线程的工具包,JDK1.5开始出现的. 进程和线程.管程 进程:系统资源分配的基本单位:它是程序的一次动态执行过程 ...
- 【Python爬虫(一)】XPath
解析方式:XPath XPath的基本使用 1 安装lxml库 conda install lxml 下载慢的话可以试一下热点或切换下载源 2 导入etree from lxml import etr ...
- 最新版本 Stable Diffusion 开源 AI 绘画工具之中文自动提词篇
目录 标签生成器 提示词自动补全 标签生成器 由于输入正向提示词 prompt 和反向提示词 negative prompt 都是使用英文,所以对学习母语的我们非常不友好 使用网址:https://t ...
- 在void 中使用return的意思
在定义的void函数里如果想要提前终止函数 格式为 return; 如果 在有返回值的函数中 格式为: return+值(0 -1 ......) 但如果在void 函数中写return 0 ; 则会 ...
- Java中方法的定义及注意事项
一.方法 什么是方法: 方法(method)是程序中最小的执行单元 实际开发中,什么时候用到方法: 重复的代码.具有独立功能的代码可以抽取到方法中 实际开发中,方法有什么好处: 可以提高代码的复用性 ...