修改python的CAPTCHA包里面的image.py,不改变字符形状旋转角度生成无背景的和无粘连的拉伸的无背景验证码

本文介绍了如何修改python的CAPTCHA包中的image.py,以生成无背景、无粘连、拉伸的验证码。首先展示了原始验证码样式,然后通过去除干扰项和调整字符形状,实现了尺寸为160x80的无背景验证码。进一步,通过在字符间插入空白实现拉伸效果,将尺寸扩展到200x80,确保字符无粘连。完整代码和详细步骤在文中提供。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要生成的目的验证码

完整代码见最后

  • 目的是要生成下面的三组验证码
    1、下面是captcha未经修改生成的样式,尺寸为160x80
    在这里插入图片描述
    2、下面是保持原来字符形状旋转监督不变,去除所有干扰生成的样式,尺寸160x80,
    这个步骤参考这位老哥的
    在这里插入图片描述

    3、下面是在上面的基础上拉伸之后,使得字符无粘连的样式。通过在字符间绘制一个空白达到拉伸效果,
    拉伸后尺寸为200x80
    在这里插入图片描述
    方法就是这样,很简单,下面是步骤和代码。

步骤

  1. 通过导入包可以发现 from captcha.image import ImageCaptcha 验证码是通过captcha包里面的image.py生成的。

  2. 从image.py里面最终返回验证码图片这里可以发现,是通过如下的create_captcha_image()这个方法产生的没有干扰的验证码图片,而干扰是由后面的create_noise_dots(), create_noise_curve()这两个方法进行另外加入的。

        def generate_image(self, chars):
            """Generate the image of the given characters.
    
            :param chars: text to be generated.
            """
            background = random_color(238, 255)
            color = random_color(10, 200, random.randint(220, 255))
            im = self.create_captcha_image(chars, color, background)
            self.create_noise_dots(im, color)
            self.create_noise_curve(im, color)
            im = im.filter(ImageFilter.SMOOTH)
            return im
    
  3. 通过上面可以发现生成第二种验证码(去除噪声和背景)就很简单了,下面是一个最简单的方法,就是在还没加干扰之前复制一份就行了,如下加一行代码,想得到白色背景的验证码需要传入背景颜色,见上面的参考,或者本文最后面的完整代码处。

        im = self.create_captcha_image(chars, color, background)
        
        # 在这里复制一份,多返回一份
        im2 = im.copy()
        im2 = im2.filter(ImageFilter.SMOOTH)
        
        self.create_noise_dots(im, color)
        self.create_noise_curve(im, color)
        im = im.filter(ImageFilter.SMOOTH)
        return im, im2	
    

    但是这样得来的验证码背景还不是白色,而是和原来的一样的背景,如下:
    在这里插入图片描述 在这里插入图片描述

  4. 下面是得到白色背景,以及拉伸之后的验证码修改后的完整代码

    # coding: utf-8
    """
        captcha.image
        ~~~~~~~~~~~~~
    
        Generate Image CAPTCHAs, just the normal image CAPTCHAs you are using.
    """
    
    import os
    import random
    from PIL import Image
    from PIL import ImageFilter
    from PIL.ImageDraw import Draw
    from PIL.ImageFont import truetype
    
    try:
        from cStringIO import StringIO as BytesIO
    except ImportError:
        from io import BytesIO
    try:
        from wheezy.captcha import image as wheezy_captcha
    except ImportError:
        wheezy_captcha = None
        
    # 默认字体
    DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
    DEFAULT_FONTS = [os.path.join(DATA_DIR, 'DroidSansMono.ttf')]
    
    if wheezy_captcha:
        __all__ = ['ImageCaptcha', 'WheezyCaptcha']
    else:
        __all__ = ['ImageCaptcha']
    
    table = []
    for i in range(256):
        table.append(i * 1.97)
    
    
    class _Captcha(object):
        def generate(self, chars, format='png'):
            """Generate an Image Captcha of the given characters.
    
            :param chars: text to be generated.
            :param format: image file format
            """
            im = self.generate_image(chars)
            out1 = BytesIO()
            out2 = BytesIO()
            out3 = BytesIO()
            im[0].save(out1, format=format)
            im[1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值