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

要生成的目的验证码

完整代码见最后

  • 目的是要生成下面的三组验证码
    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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Python中的验证码来增加网站的安全性,防止垃圾邮件机器人的滥用和DDOS攻击。其中一个常用的Python库是django-simple-captcha,您可以使用pip命令进行安装(pip install django-simple-captcha)。然后,在您的代码中导入captcha.py模块(import captcha.py)并使用它来生成验证码(captcha.CAPTCHA()) [1。 在您的表单类中,您可以将验证码字段添加为CaptchaField,这样用户就需要输入正确的验证码才能提交表单。例如,在Django中,您可以这样设置captcha字段: from django import forms from captcha.fields import CaptchaField class UserRegisterForm(forms.Form): email = forms.EmailField(required=True) password = forms.CharField(required=True, min_length=3, max_length=15, error_messages={ 'required': '密码必须填写', 'min_length': '密码不得小于3位', 'max_length': '密码不得大于15位' }) captcha = CaptchaField(error_messages={ 'invalid': '验证码错误' }) [3] 这样,用户在填写表单时需要输入正确的验证码才能通过验证。这有助于防止自动化程序对您的网站进行恶意操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python-captcha:python中的验证码](https://download.csdn.net/download/weixin_42131728/16144121)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python通过captcha实现验证码的功能](https://blog.csdn.net/weixin_40970987/article/details/92783459)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值