查看python模块captcha的源码

使用captcha模块即可直接生成验证码

码云链接

语言:python
python版本:Python 3.8.3
编译器:vscode
需要的模块:captcha,PIL
  • 安装capthca模块pip install -i https://pypi.douban.com/simple captcha
  • 查看更多验证码的文章请点击传送门
  1. _Captcha类上面没框出来的我也不知道是啥
    在这里插入图片描述
  2. 大概看一下源码的内容
    在这里插入图片描述
  3. 父类_Captcha
    在这里插入图片描述
  4. ImageCaptcha
    在这里插入图片描述
  • 所以实例化ImageCaptcha的时候,可以指定图片的大小和字体以及字体大小
  • 现在指定一个参数看看
# 解析captcha源码
from captcha.image import ImageCaptcha

# 指定仿宋
font_path = r'C:\Windows\Fonts\simfang.ttf'
# 注意fonts参数是个list,font_sizes等会测试下是怎么个回事
img = ImageCaptcha(width=200,
                   height=100,
                   fonts=[font_path],
                   font_sizes=(42, 50, 56))
im = img.create_captcha_image(chars='哈哈哈哈', color='red', background='white')
im.show()
print(im.size)
print(type(im))

在这里插入图片描述

  • 探究font_sizes参数是怎么个意思
# 解析captcha源码
from captcha.image import ImageCaptcha

# 指定仿宋
font_path = r'C:\Windows\Fonts\simfang.ttf'
# 我采用遍历的方法将元祖的第一个参数作为变动,查看验证码的情况时怎样的
for one in range(10, 42):
    img = ImageCaptcha(width=200,
                       height=100,
                       fonts=[font_path],
                       font_sizes=(one, 50, 56))
    im = img.create_captcha_image(chars='哈哈哈哈',
                                  color='red',
                                  background='white')
    # 图片保存的路径
    path = r'images/font_sizes_改变元祖索引0的大小/' + str(one) + '.png'
    im.save(path)
print('验证码生成完成')
  • 可以发现有的哈小,但是还是没有发现啥规律
    在这里插入图片描述
  • 改变索引1看看
# 解析captcha源码
from captcha.image import ImageCaptcha

# 指定仿宋
font_path = r'C:\Windows\Fonts\simfang.ttf'
# 我采用遍历的方法将元祖的第一个参数作为变动,查看验证码的情况时怎样的
for one in range(10, 50):
    img = ImageCaptcha(width=200,
                       height=100,
                       fonts=[font_path],
                       font_sizes=(42, one, 56))
    im = img.create_captcha_image(chars='哈哈哈哈',
                                  color='red',
                                  background='white')
    # 图片保存的路径
    path = r'images/font_sizes_改变元祖索引1的大小/' + str(one) + '.png'
    im.save(path)
print('验证码生成完成')
  • 也是有的ha变小了,还是找不到啥规律
    在这里插入图片描述
  • 改变索引2看看
# 解析captcha源码
from captcha.image import ImageCaptcha

# 指定仿宋
font_path = r'C:\Windows\Fonts\simfang.ttf'
# 我采用遍历的方法将元祖的第一个参数作为变动,查看验证码的情况时怎样的
for one in range(10, 50):
    img = ImageCaptcha(width=200,
                       height=100,
                       fonts=[font_path],
                       font_sizes=(42, 50, one))
    im = img.create_captcha_image(chars='哈哈哈哈',
                                  color='red',
                                  background='white')
    # 图片保存的路径
    path = r'images/font_sizes_改变元祖索引2的大小/' + str(one) + '.png'
    im.save(path)
print('验证码生成完成')
  • 我套阿
    在这里插入图片描述
  • 看下源码里这个font_sizes是要干啥
    在这里插入图片描述
  • 发现是PIL.ImageFont里面的方法
    在这里插入图片描述
  • 点进去查看一下该方法
    在这里插入图片描述
  • 继续点进去查看源码
    在这里插入图片描述
  • 试着打印一下这个方法的值
from captcha.image import ImageCaptcha

# 模拟打印一下truefonts返回什么
img = ImageCaptcha(
    width=200,
    height=100,
    #    fonts=[font_path],
    font_sizes=(42, 50, 52))
print(img.truefonts)
  • 输出一个元组,长度为3
    在这里插入图片描述
  • 重新看了一下列表推导式
    在这里插入图片描述
  • 试着在ipython写了一下
    在这里插入图片描述
  • 那么仔细想一下就知道_truefonts的值就是一个元组,每个元组都是字体和大小就是咱们实例化的时候传进去的字体和大小,而且字体大小还是随机取的
    在这里插入图片描述
  • 嘿,知道是怎么一回事的话,那么font_sizes就好办了
    在这里插入图片描述
  • font_sizes解决
from captcha.image import ImageCaptcha

# 指定仿宋
font_path = r'C:\Windows\Fonts\simfang.ttf'
# 咱们可以只给一个数,注意font_sizes必须是可迭代的
img = ImageCaptcha(fonts=[font_path], font_sizes=(10, ))
im = img.create_captcha_image(chars='哈哈哈哈', color='red', background='white')
im.show()
  • 这样字体的大小就是10咯
    在这里插入图片描述
  • 指定更大一些的字体大小
    在这里插入图片描述
  • font_sizes参数总结,如果长度为1,那么字体的大小就是这个数,如果长度不为>1,那么字体的大小就是随机选择元组里面的值,如果咱们前面才会出现有大有小的"哈"…顺便一提,这个四个的哈,每个字的大小都是随机的哦,看下图就知道了
    在这里插入图片描述
  • 关于ImageCaptcha类的__init__和truefonts就讲解到这吧,剩下的方法另起文章讲解,不然篇幅太长
    在这里插入图片描述
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值