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=(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之验证码的更多相关文章

  1. python识别验证码——PIL,pytesser,pytesseract的安装

    1.使用Python识别验证码需要安装Python的图像处理模块(PIL.pytesser.pytesseract) (安装过程需要pip,在我的Python中已经安装pip了,pip的安装就不在赘述 ...

  2. python之验证码识别 特征向量提取和余弦相似性比较

    0.目录 1.参考2.没事画个流程图3.完整代码4.改进方向 1.参考 https://en.wikipedia.org/wiki/Cosine_similarity https://zh.wikip ...

  3. 实验楼Python破解验证码

    本人大二,因为Python结业考试项目,又想要学习机器学习方向,但是由于接触时间不长,选择了实验楼的Python破解验证码这个项目作为我的项目, 我在原来的基础上加了一些代码用于完善,并且对功能如何实 ...

  4. python中验证码连通域分割的方法详解

    python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...

  5. 关于利用python进行验证码识别的一些想法

    转载:@小五义http://www.cnblogs.com/xiaowuyi 用python加“验证码”为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章.我大体看了一下,主要方法有几类: ...

  6. python 生成验证码

    在工作中经常遇到一些验证码,这些是怎么生成的呢,今天我用Python编写了下 import randomcode = []for i in range(6): if i == random.randi ...

  7. python图像识别--验证码

    1.pip3 install pyocr 2.pip3 install pillow or easy_install Pillow 3.安装tesseract-ocr:http://jaist.dl. ...

  8. python生成验证码脚本

    最近每天都用python写一个小的脚本,练习使用python语法. 验证码的生成: 这里使用了python的图像处理库PIL,安装PIL的过程中出了一个小麻烦,就使用Pillow-win32的一个文件 ...

  9. Python爬虫入门教程 60-100 python识别验证码,阿里、腾讯、百度、聚合数据等大公司都这么干

    常见验证码 之前的博客中已经解决了一些常见验证码的问题,但是验证码是层出不穷的,目前解决验证码除了通过常规手段解决以外,还可以通过人工智能领域的深度学习去解决 深度学习?! 无疑对爬虫coder提高了 ...

  10. 纯代码系列:Python实现验证码图片(PIL库经典用法用法,爬虫12306思路)

    现在的网页中,为了防止机器人提交表单,图片验证码是很常见的应对手段之一.这里就不详细介绍了,相信大家都遇到过. 现在就给出用Python的PIL库实现验证码图片的代码.代码中有详细注释. #!/usr ...

随机推荐

  1. 技巧JS

    1.     document.referrer可以获得上一页的地址,使用document.anchors获得页面上面所有的链接元素,而不必使用document.getElementsByTagNam ...

  2. .NET开发笔记--对config文件的操作(1)

    1先写一些常用的公共类: 在Web.config文件中的配置: <!-- appSettings网站信息配置--> <appSettings> <add key=&quo ...

  3. 【vijos】1892 树上的最大匹配(树形dp+计数)

    https://vijos.org/p/1892 这个必须得卡评测机+手动开栈才能卡过QAQ 手动开栈我百度的... int size=256<<20; //256MB char *p=( ...

  4. 【BZOJ】3203: [Sdoi2013]保护出题人(几何+三分+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3203 wa无数次QAQ,犯sb错....一是数组没有引用...二是输出转成了int(越界了sad). ...

  5. combox使用要点

    <select id="node_type" name="node_type" class="easyui-combobox" req ...

  6. db2 sequence

    我的上两个专栏中已经介绍到了与版本 8 功能相关的主题.我们可能需要在今天设计的数据库和应用程序中考虑这些功能.我们已经谈论了新的数据分区的辅助索引和附加的索引修改.在上一期中,我们了解了 DSSIZ ...

  7. Dependency Property 依赖属性

    依赖属性就是一种可以自己没有值,并能通过使用Binding从数据源获得值(依赖在别人身上)的属性.拥有依赖属性的对象称为“依赖对象”. WPF开发中,必须使用依赖对象作为依赖属性的宿主,使二者结合起来 ...

  8. c# 可变数目参数params实例

    一般来说,参数个数都是固定的,定义为集群类型的参数可以实现可变数目参数的目的,但是.NET提供了更灵活的机制来实现可变数目参数,这就是使用params修饰符 一般来说,参数个数都是固定的,定义为集群类 ...

  9. VC++显示文件或文件夹属性

    When you select a file or folder in Explorer window, and choose 'Properties' from the menu, you get ...

  10. 好用的 Visual Studio插件

    Image Optimizer(图片优化)插件 使用行业标准优化任何JPEG,PNG和gif(包括动画gif).可以做有损和无损的优化.