Python3实现验证码

Python3 实现创建验证码图片

一:准备工作,需要安装PIL,安装方式,pip install Pillow

二:具体实现

#!/usr/bin/env python3
# coding:UTF-8
""""
文件说明:
"""
from PIL import Image, ImageDraw, ImageFont
import random
import string
import os


def get_code(width=100, height=40, fontSize=35):
    """
    width: 背景图片的宽度
    height:背景图片的高度
    fontsize:验证码的字体大小
    """
    img = Image.new("RGB", (width, height), getColor())  # 创建指定大小,背景颜色,模式的图片
    draw = ImageDraw.Draw(img)  # 创建画刷
    path = get_Font()  # 得到一个系统下的随机字体路径
    # 获取指定路径的字体
    font = ImageFont.truetype(font=path, size=fontSize)
    content = myrandom()  # 获取随机生成的验证码的值
    # 将验证码画到图片上
    draw.text((width * 0.1, height * 0.15), content,
              fill=getColor(), font=font)
    # 画干扰线
    for i in range(5):
        x = random.randint(0, 20)
        y = random.randint(0, height)
        z = random.randint(width - 20, width)
        w = random.randint(0, height)
        draw.line(((x, y), (z, w)), fill=getColor())
    # 返回验证码图片与文本内容
    return img, content


def getColor():
    '''随机生成一个元组类型的 RGB颜色'''
    color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256))
    return color


def myrandom(count=5):
    myList = list(string.ascii_letters + string.digits)  # 指定要生成验证码的集合,数字,大小写字母
    # 在指定的mylist集合中随机取出count个集合
    lists = random.sample(myList, count)
    # 用指定的字符串连接集合中的内容
    return "".join(lists)


def get_Font(split='.ttf'):
    """返回操作系统下的指定后缀的字体"""
    if os.name == 'nt':
        path = 'C:\Windows\Fonts'.replace("\\", '/') + '/'
    else:
        # 根据不同的操作系统,系统字体在不同的文件下,Ubuntu下可以在/usr/share/fonts/ 下找到很多文件类型的字体
        path = '/usr/share/fonts/truetype/freefont/'
    listFont = os.listdir(path)
    # 获取指定后缀的字体,默认是.ttf类型的后缀
    fontList = [path + x for x in listFont if os.path.splitext(x)[1] == split]
    # 返回系统字体列表,以及所在的路径
    # return fontList, path
    # 随机返回一个字体
    path = random.sample(fontList, 1)
    return path[0]


if __name__ == '__main__':
    img, content = get_code()
    img.show()


三:上述代码可以直接运行

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值