Python生成随机验证码

 

Python生成随机验证码,需要使用PIL模块.

安装:

1
pip3 install pillow

基本使用

1. 创建图片

1
2
3
4
5
6
7
8
9
from PIL import Image
img = Image.new(mode='RGB', size=(12030), color=(255255255))
 
# 在图片查看器中打开
# img.show() 
 
# 保存在本地
with open('code.png','wb') as f:
    img.save(f,format='png')

2. 创建画笔,用于在图片上画任意内容

1
2
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')

3. 画点

1
2
3
4
5
6
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示坐标
# 第二个参数:表示颜色
draw.point([100100], fill="red")
draw.point([300300], fill=(255255255))

4. 画线

1
2
3
4
5
6
img = Image.new(mode='RGB', size=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标
# 第二个参数:表示颜色
draw.line((100,100,100,300), fill='red')
draw.line((100,100,300,100), fill=(255255255))

5. 画圆

1
2
3
4
5
6
7
img = Image.new(mode='RGB', size=(12030), color=(255255255))
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=(12030), color=(255255255))
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=(12030), color=(255255255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示字体文件路径
# 第二个参数:表示字体大小
font = ImageFont.truetype("kumo.ttf"28)
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
# 第四个参数:表示颜色
draw.text([00], '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=(255255255))
    draw = ImageDraw.Draw(img, mode='RGB')
 
    def rndChar():
        """
        生成随机字母   
        :return:
        """
        return chr(random.randint(6590))
 
    def rndColor():
        """
        生成随机颜色
        :return:
        """
        return (random.randint(0255), random.randint(10255), random.randint(64255))
 
    # 写文字
    font = ImageFont.truetype(font_file, font_size)
    for in range(char_length):
        char = rndChar()
        code.append(char)
        = random.randint(04)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
 
    # 写干扰点
    for in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
 
    # 写干扰圆圈
    for in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        = random.randint(0, width)
        = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 090, fill=rndColor())
 
    # 画干扰线
    for 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生成随机验证的更多相关文章

  1. Python生成随机数组的方法小结

    Python生成随机数组的方法小结 本文实例讲述了Python生成随机数组的方法.分享给大家供大家参考,具体如下: 研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,今天把Pyt ...

  2. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  3. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  4. python生成随机日期字符串

    python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...

  5. Python生成随机字符串

    利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...

  6. python生成随机整数

    python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...

  7. C#生成随机验证吗例子

    C#生成随机验证吗例子: public class ValidateCode : IHttpHandler, IRequiresSessionState { HttpContext context; ...

  8. Python生成随机验证码,大乐透号码

    实例笔记之生成随机号码 扩展知识 - yield(生成器) 随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__' ...

  9. python 生成随机字符串

    1.生成随机字符串 #数字+字母+符号 def getRandChar(n): l = [] #sample = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^ ...

  10. python 生成随机图片验证码

    1.安装pillow模块 pip install pillow (1)创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的 ...

随机推荐

  1. 《golong入门教程📚》,从零开始入门❤️(建议收藏⭐️)

    Go语言学习笔记 本菜鸟的Go语言学习笔记,历时1个月,包含了Go语言大部分的基本语法(不敢说全部),学习期间参考了各种视频,阅读了各种文章,主要参考名单如下: 点击跳转到参考名单<( ̄︶ ̄)& ...

  2. golang pprof 监控系列(1) —— go trace 统计原理与使用

    golang pprof 监控系列(1) -- go trace 统计原理与使用 服务监控系列文章 服务监控系列视频 关于go tool trace的使用,网上有相当多的资料,但拿我之前初学golan ...

  3. 实现一个CRDT工具库——ORSet

    ORSet 这段代码实现了OR-Set,是一种基于版本向量的CRDT,用于实现集合的合并.OR-Set由两个集合add和remove组成,add集合存储添加的元素,remove集合存储删除的元素.每个 ...

  4. 【实战】SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目

    JavaDog Chat v1.0.0 基于SpringBoot+uniapp简单通讯聊天软件 项目介绍 JavaDog Chat 简单通讯聊天软件是基于SpringBoot+MybatisPlus+ ...

  5. Comic Life - 超棒的漫画制作工具,拥有多种动画模版,创作属于自己的漫画

    Comic Life是一个照片编辑器,能够添加各种效果,并基于它们创建漫画.该工具包包括各种各样的模板,可以很容易地将照片放置在工作表上,还有大量各种形状的标注.除了拼贴画上的标注之外,您还可以添加带 ...

  6. VUE中的next({ ...to, replace: true })

    beforeEach((to, from, next) => { next('/logon') } 上面这串代码我们可以看成为 beforeEach((to, from, next) => ...

  7. Linux中Python自动输入sudo 密码【管道 sudo参数 stdin&stdout】

    一.背景和需求 背景: 由于docker服务进程都是以root帐号的身份运行的,所以用docker跑abpred出来的文件所有者都是root, 而我作为一般用户,操作这个文件不够权限,运行代码时需要s ...

  8. ACM-CodeForces-#685(Div.2)

    好久没见过CF有这么水的contest了,蒟蒻赶紧找找自信 A. Subtract or Divide #include<iostream> using namespace std; in ...

  9. 华为 A800-9000 服务器 离线安装MindX DL

    MindX DL(昇腾深度学习组件)是支持 Atlas 800 训练服务器.Atlas 800 推理服务器的深度学习组件参考设计,提供昇腾 AI 处理器资源管理和监控.昇腾 AI 处理器优化调度.分布 ...

  10. pysimplegui之列表布局

    重点 1通过循环生成一行 2通过循环生成一列 3[]这个代表一行 4需要大量重复布局的时候可以使用 生成的布局(如果你有> 5个重复元素/行,一定要阅读) 本节讨论了 5 种生成布局的特定技术. ...