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 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识别验证码——PIL,pytesser,pytesseract的安装
1.使用Python识别验证码需要安装Python的图像处理模块(PIL.pytesser.pytesseract) (安装过程需要pip,在我的Python中已经安装pip了,pip的安装就不在赘述 ...
- python之验证码识别 特征向量提取和余弦相似性比较
0.目录 1.参考2.没事画个流程图3.完整代码4.改进方向 1.参考 https://en.wikipedia.org/wiki/Cosine_similarity https://zh.wikip ...
- 实验楼Python破解验证码
本人大二,因为Python结业考试项目,又想要学习机器学习方向,但是由于接触时间不长,选择了实验楼的Python破解验证码这个项目作为我的项目, 我在原来的基础上加了一些代码用于完善,并且对功能如何实 ...
- python中验证码连通域分割的方法详解
python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...
- 关于利用python进行验证码识别的一些想法
转载:@小五义http://www.cnblogs.com/xiaowuyi 用python加“验证码”为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章.我大体看了一下,主要方法有几类: ...
- python 生成验证码
在工作中经常遇到一些验证码,这些是怎么生成的呢,今天我用Python编写了下 import randomcode = []for i in range(6): if i == random.randi ...
- python图像识别--验证码
1.pip3 install pyocr 2.pip3 install pillow or easy_install Pillow 3.安装tesseract-ocr:http://jaist.dl. ...
- python生成验证码脚本
最近每天都用python写一个小的脚本,练习使用python语法. 验证码的生成: 这里使用了python的图像处理库PIL,安装PIL的过程中出了一个小麻烦,就使用Pillow-win32的一个文件 ...
- Python爬虫入门教程 60-100 python识别验证码,阿里、腾讯、百度、聚合数据等大公司都这么干
常见验证码 之前的博客中已经解决了一些常见验证码的问题,但是验证码是层出不穷的,目前解决验证码除了通过常规手段解决以外,还可以通过人工智能领域的深度学习去解决 深度学习?! 无疑对爬虫coder提高了 ...
- 纯代码系列:Python实现验证码图片(PIL库经典用法用法,爬虫12306思路)
现在的网页中,为了防止机器人提交表单,图片验证码是很常见的应对手段之一.这里就不详细介绍了,相信大家都遇到过. 现在就给出用Python的PIL库实现验证码图片的代码.代码中有详细注释. #!/usr ...
随机推荐
- Yii2 Restful API 原理分析
Yii2 有个很重要的特性是对 Restful API的默认支持, 通过短短的几个配置就可以实现简单的对现有Model的RESTful API 参考另一篇文章: http://www.cnblogs. ...
- 延迟队列DelayQueue
应用场景:有一批广告需要不定时上下架,有可能上下架的时间间隔很长,就没必要用定时器轮询,用延迟队列进行任务执行. public class Test2 { public static void mai ...
- Sql_server四种执行ExecuteReader、ExecuteNonQuery、ExecuteScalar、DataSet.docx
c#数据查询输出 2012-07-17 17:07 1.使用ExecuteReader()操作数据库 2.使用ExecuteNonQuery()操作数据库 3.使用ExecuteScalar()操作数 ...
- 第二百一十四节,jQuery EasyUI,Calendar(日历)组件
jQuery EasyUI,Calendar(日历)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Canlendar(日历)组件的使用方法 ...
- 第一百六十一节,封装库--JavaScript,完整封装库文件
封装库--JavaScript,完整封装库文件 /** *feng_zhuang_ku_1.0版本,js封装库,2016/12/29日:林贵秀 **/ /** 前台调用 * 每次调用$()创建库对象, ...
- (转)ThreadLocal
转自:http://blog.csdn.net/lufeng20/article/details/24314381 Thread同步机制的比较 ThreadLocal和线程同步机制相比有什么优势呢?T ...
- MathType与Origin是怎么兼容的
MathType作为一款常用的公式编辑器,可以与很多的软件兼容使用.Origin虽然是一款专业绘图与数据分析软件,但是在使用过程中也是可以用到MathType.它可以帮助Origin给图表加上标签,或 ...
- ios 时间解析 差8个小时
啥问题也有:小程序中web开发工具里显示时间正常,ios上显示的查8小时 原因: 使用 NSDate *date = [NSDate date]; 获取的时间是标注的UTC时间,和北京时间相差8小时. ...
- CodeForces 663A Rebus
A. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 更新设置api
8.8 更新设置 API Elasticsearch允许在elasticsearch.yml文件中指定各种参数来调优.但你应该把这个文件当做 默认设置,可以在运行时通过Elasticsearch RE ...