使用模块PIL 生成 随机验证码
--------------默认自己无能,无疑是给失败制造机会!你认为自己是什么样的人,就将成为什么样的人。
要使用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 |
使用模块PIL 生成 随机验证码的更多相关文章
- python模块之PIL模块(生成随机验证码图片)
PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...
- Python利用PIL生成随机验证码图片
安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- Django中生成随机验证码(pillow模块的使用)
Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...
- 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 ...
- pillow实例 | 生成随机验证码
1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
随机推荐
- 报错org.apache.hadoop.mapreduce.lib.input.FileSplit cannot be cast to org.apache.hadoop.mapred.FileSplit
报错 java.lang.Exception: java.lang.ClassCastException: org.apache.hadoop.mapreduce.lib.input.FileSpli ...
- 自动化测试基础篇--Selenium简介
摘自https://www.cnblogs.com/sanzangTst/p/7452636.html 一.软件开发的一般流程 二.什么叫软件测试? 软件测试(英语:Software Testing) ...
- CentOS 7.0安装
CentOS 7.0安装 本次通过虚拟机的方法安装CentOS 7.0操作系统,开启虚拟机后会出现以下界面 1.选择第一项,Install CentOS 7 (安装CentOS 7),进入下面的界面 ...
- MATLAB常微分方程数值解——欧拉法、改进的欧拉法与四阶龙格库塔方法
MATLAB常微分方程数值解 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1.一阶常微分方程初值问题 2.欧拉法 3.改进的欧拉法 4.四阶龙格库塔 ...
- Java访问级别修饰符
用途 控制其他类可以访问的字段或方法 修饰符 public.protected.no modifier(未声明).private 访问级别 修饰符 当前类 包 子类 其他包 public √ √ √ ...
- 你好,我是梁桐铭,.NET程序员,啰嗦下过去几年来的感悟吧
序 所有的文章都会有序言,我的当然也不例外. 因为职业和工作的关系,很少有时间陪伴家人,感谢妻子10年以来的容忍和支持,感谢女儿给我生活带来的乐趣. 希望孩子长大了之后能热爱编程(可以不用以它谋生). ...
- 【BZOJ4298】[ONTAK2015]Bajtocja
[BZOJ4298][ONTAK2015]Bajtocja Description 给定d张无向图,每张图都有n个点.一开始,在任何一张图中都没有任何边.接下来有m次操作,每次操作会给出a,b,k,意 ...
- 词云wordcloud入门示例
整体简介: 词云图,也叫文字云,是对文本中出现频率较高的“关键词”予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨. 基于Python的词云生成类库 ...
- Nginx处理请求过程
1. worker进程工作机制 现在我们了解了当我们在操作nginx的时候,nginx内部做的一些事情,那么worker进程又是如何处理请求的呢? 我们前面有提到,worker进程之间是平等的, ...
- Android-SpinKit 进度条 (ProgressBar)
项目地址: https://github.com/ybq/Android-SpinKit 类别: 进度条 (ProgressBar) 打分: ★★★★★ 更新: 2016-03-28 11:17 大小 ...