Python 学习之生成图形验证码

一、 如何生成图形验证码?

新建一个captcha 的python 文件包,在__init__.py 文件中写入生成图形验证码的代码,将字体文件也放入这个文件包中 。

import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import string



class Captcha(object):
    # 生成几位的验证码
    number = 4
    # 验证码图片的宽度和高度
    size = (110, 38)
    # 验证码字体的大小
    fontsize = 25
    # 加入干扰线的条数
    line_number = 2
    # 构建一个验证码源文本
    source = list(string.ascii_letters)
    source.extend(map(lambda x: str(x), range(10)))

    # 用户绘制干扰线
    @classmethod
    def _gene_line(cls, draw, width, height):
        begin = (random.randint(0, width), random.randint(0, height))
        end = (random.randint(0, width), random.randint(0, height))
        draw.line([begin, end], fill=cls._gene_random_color(), width=2)

    # 用于绘制干扰点
    @classmethod
    # cls, draw, point_chance, width, height
    def _gene_points(cls, draw, width, height, rate):
        # chance = min(100, max(0, int(point_chance)))  #大小限制在0-100
        # for w in range(width):
        #     for h in range(height):
        #         tmp = random.randint(0, 100)
        #         if tmp > chance:
        #             draw.point((w, h), fill=cls._gene_random_color())
        # 因为width为图形验证码的宽,height为图形验证码的高,整个图都是由点组成的
        # 点的x坐标范围:[0, 图形的宽度], y的坐标范围:[0, 图形的高度], 这样就能遍历图像的每一个像素点
        # rate 用来控制点生成的概率,大约100个点有rate个点被选中
        # point方法用来画点,参数1:点的坐标, 参数2:点的颜色
        for x in range(width):
            for y in range(height):
                if random.randint(1, 100) <= rate:
                    draw.point((x, y), fill=cls._gene_random_color())

    # 随机生成颜色
    @classmethod
    def _gene_random_color(cls, start=0, end=255):
        random.seed()
        return (random.randint(start, end), random.randint(start, end), random.randint(start, end))

    # 随机选择一个字体
    @classmethod
    def _gene_random_font(cls):
        fonts = [
            "AlimamaShuHeiTi-Bold.ttf",
            "AlimamaDaoLiTi.ttf",
            "AlimamaDongFangDaKai-Regular.ttf",
        ]
        font = random.choice(fonts)
        return f"utils/captcha/{font}"

    #     随机生成一个字符串(包括英文和数字)
    @classmethod
    def gene_text(cls, number):
        return ''.join(random.sample(cls.source, number))

    #     生成验证码
    @classmethod
    def gene_graph_captcha(cls):
        # 验证码图片的宽和高
        width, height = cls.size
        # 创建图片
        image = Image.new('RGBA', (width, height), cls._gene_random_color(0, 100))
        # 验证码字体
        font = ImageFont.truetype(cls._gene_random_font(), cls.fontsize)
        # 创建画笔
        draw = ImageDraw.Draw(image)
        # 生成字符串
        text = cls.gene_text(cls.number)
        # font.getsize(text)获取字体的尺寸
        # pillow 版本10 会报这个错误, AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
        # 降低版本为9.5,但是安装一直报Read timed out,所以直接取消掉
        # font_width, font_height = font.getsize(text)
        # 填充字符串
        draw.text((30, 5), text, font=font,
                  fill=cls._gene_random_color(150, 255))
        # 绘制干扰线
        for x in range(0, cls.line_number):
            cls._gene_line(draw, width, height)
        # 绘制噪点
        cls._gene_points(draw, width, height, 20)
        with open('utils/captcha/captcha.png', 'wb') as fp:
            image.save(fp)
        return text, image


# if __name__ == '__main__':
    # Captcha.gene_graph_captcha()

二、 后端如何将图形验证码传给前端?

from utils.captcha import Captcha
from io import BytesIO
from flask import Blueprint, make_response
bp = Blueprint('common', __name__, url_prefix='/common')
@bp.route('/graph_captcha/')
def graph_captcha():
    # 获取验证码
    text, image = Captcha.gene_graph_captcha()
    # BytesIO 二进制流数据
    out = BytesIO()
    # 将image 指定保存在out 二进制流里面
    image.save(out, 'PNG')
    # 由于添加了图片数据进去,指针会指向文件流最后,所以需要将文件流指针指到0,跟操作文件类似
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = "image/png"
    cache.set(text.lower(), text.lower())
    return resp

三、 前端如何使用后端传递过来的图形验证码?

<div class="mb-3 input-group">
            <input class="form-control" type="text" name="graph_code" placeholder="图形验证码">
            <span id="graph_captcha_box" class="input-group-text graph_captcha_box">
                <img id="graph_captcha" src="{{ url_for('common.graph_captcha') }}" alt="">
            </span>
</div>
window.onload = function () {
    let graph_captcha_box = document.getElementById('graph_captcha_box')
    let graph_captcha = document.getElementById('graph_captcha')
  

    graph_captcha_box.addEventListener('click', function () {
        let timeStamps = new Date().getTime();
        graph_captcha.src = `/common/graph_captcha/?timeStamps=${timeStamps}`
    })
}

Python中,生成随机图形验证码通常涉及到图像处理库,如PIL(Python Imaging Library)或其更新版本Pillow,以及可能用到的随机数生成库如random和numpy。以下是一个简单的步骤概述: 1. 导入所需的库: ```python from PIL import Image, ImageDraw, ImageFont import random import numpy as np ``` 2. 定义图形元素,如线条、圆形、矩形、字母和数字: ```python def draw_random_shape(size, shapes): shape = random.choice(shapes) if shape == 'line': draw.line((random.randint(0, size), random.randint(0, size)), (random.randint(0, size), random.randint(0, size)), fill=(0, 0, 0)) elif shape == 'circle': center = (random.randint(0, size), random.randint(0, size)) draw.ellipse((center-10, center-10, center+10, center+10), fill=(0, 0, 0)) # 添加其他形状,如矩形等 def generate_text(): return ''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=random.randint(4, 6))) ``` 3. 创建验证码图像: ```python size = 100 image = Image.new('RGB', (size, size), color='white') draw = ImageDraw.Draw(image) shapes = ['line', 'circle', 'rectangle'] # 可自定义形状列表 for _ in range(random.randint(4, 6)): draw_random_shape(size, shapes) text = generate_text() font = ImageFont.truetype("arial.ttf", size=random.randint(20, 30)) # 加载字体 text_position = (random.randint(10, size-10), random.randint(10, size-10)) draw.text(text_position, text, font=font, fill=(0, 0, 0)) # 保存验证码图片 image.save('captcha.png') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值