python奇妙旅行之4行代码生成图像验证码

在学习的路上,永无止境。就好比人掉进"深渊",永远无法自拔 !   ~  ~!我没有开车,我没有开车~~~

今天空闲时间再看某大佬得论坛,被点了一下,就想起来了2种方法,生成图片验证码,简约而不失优雅~~

代码上来~~

一、graphic-verification-code库

 我建议的安装方式都是 pip方式,简单,省时

  1.安装

      pip install   graphic-verification-code  

  2. 撸代码(4行代码生成图片):

import gvcode
a,m = gvcode.generate()  #序列解包
a.show() #显示生成的验证码图片
print(m)

运行结果:

 

二、captcha库

1.安装

 pip install   captcha

2.撸代码(3行生成图片验证码):

from captcha.image import ImageCaptcha
img = ImageCaptcha().generate_image("176AK0")
img.show()

运行结果:

还可以在指定范围内生成随机图像验证码,

上代码:

from captcha.image import ImageCaptcha
from random import randint

list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

data = ''
for i in range(4):
    data += list[randint(0,62)]
img= ImageCaptcha().generate_image(data)
img.show()

运行结果:

运行完,瞬间感觉自己牛牪犇了,有没有~~ 

学习的路上,还得继续,不要停!!!

原创文章 54 获赞 66 访问量 4万+

猜你喜欢

转载自blog.csdn.net/wuyoudeyuer/article/details/104947134