python生成简单的验证码

python生成简单的验证码


最近因为需要用深度学习做一些简单的验证码识别工作,网上爬取验证码的效率比较慢,于是决定采用自己写脚本生成验证码来使用。
python版本: python3.6

代码如下:

#!/usr/bin/env python3
# coding: utf-8

import sys
import os
import random
import shutil
import uuid
import itertools
import threading
from queue import Queue
# 需要pillow库,安装方法: pip3 install ${name}  (terminal下)
from PIL import Image, ImageDraw, ImageFont, ImageFilter

DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'ttf')     #字体文件夹放在与脚本同目录下,可以自行添加和修改字体
DEFAULT_FONTS = [os.path.join(DATA_DIR, 'DroidSansMono.ttf')]


class Captcha():

    def __init__(self, width=128, height=32, fonts=DEFAULT_FONTS, font_sizes=None):
        self._width = width
        self._height = height
        self._fonts = fonts
        self._font_sizes = font_sizes or (26, 28, 30)
        self._true_fonts = tuple((ImageFont.truetype(f, s)
                                  for f in self._fonts
                                  for s in self._font_sizes))

    @staticmethod
    def create_noise_dots(image, number=50):
        color = Captcha.random_color(100, 238, random.randint(220, 255))
        w, h = image.size
        draw = ImageDraw.Draw(image)
        for i in range(number):
            x1 = random.randint(0, w)
            y1 = random.randint(0, h)
            draw.point((x1, y1), fill=color)
        return image

    @staticmethod
    def create_noise_curve(image):
        color = Captcha.random_color(100, 255, random.randint(220, 255))
        w, h = image.size
        x1 = random.randint(0, int(w / 5))
        x2 = random.randint(w - int(w / 5), w)
        y1 = random.randint(int(h / 5), h - int(h / 5))
        y2 = random.randint(y1, h - int(h / 5)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值