python直接生成带干扰线的验证码
使用captcha模块即可直接生成验证码码云链接语言:pythonpython版本:Python 3.8.3编译器:vscode需要的模块:captcha,PIL安装capthca模块pip install -i https://pypi.douban.com/simple captcha简单使用# 生成带干扰线的验证码from captcha.image import ImageCaptcha#
·
使用captcha模块即可直接生成验证码
语言:python
python版本:Python 3.8.3
编译器:vscode
需要的模块:captcha,PIL
- 安装capthca模块
pip install -i https://pypi.douban.com/simple captcha
- 简单使用
# 生成带干扰线的验证码
from captcha.image import ImageCaptcha
# 实例化
img = ImageCaptcha()
# 先生成一个验证码图片
im = img.create_captcha_image('1234', color='red', background='white')
# 然后再使用create_noise_curve方法将上面生成的验证码画上干扰线
im = img.create_noise_curve(im, color='black')
im.show()
- 生成的干扰线位置也是不固定的
- 生成10张带干扰线的验证码
from captcha.image import ImageCaptcha
import string, random
# 实例化
img = ImageCaptcha()
# 生成字母和数字
lettersAndNumbers = string.ascii_letters + string.digits
# 咱们生产10张验证码
for _ in range(10):
# 随机选择4个字符生产验证码
chars = ''.join(random.sample(lettersAndNumbers, k=4))
# 随机颜色,color和background是接受rgb(0,0,0)这样的参数的
# 所以可以使用random生成即可
rgb = (random.randint(0, 256), random.randint(0,
256), random.randint(0, 256))
b_rgb = (random.randint(0,
256), random.randint(0,
256), random.randint(0, 256))
im = img.create_captcha_image(chars=chars, color=rgb, background=b_rgb)
# 开始生成带干扰线的验证码
c_rgb = (random.randint(0,
256), random.randint(0,
256), random.randint(0, 256))
im = img.create_noise_curve(im, color=c_rgb)
# 图片保存的路径
path = r'images/干扰线/' + chars + '.png'
im.save(path)
print('验证码生成完成')
3. 关于captcha模块生成带干扰线的验证码就到这
4. 更多关于captcha的文章请自行查看
更多推荐
已为社区贡献7条内容
所有评论(0)