Python生成验证码

这篇博客介绍了如何利用Python的Pillow库生成验证码。首先,通过导入Pillow的相关模块,然后实例化图片对象和画笔。接着,详细讲解了如何在图片上绘制文字、线条、矩形、椭圆、圆和点。最后,展示了如何结合这些方法生成一个图片验证码。
摘要由CSDN通过智能技术生成

Python生成验证码 需要用到Pillow 模块(pip install pillow);

一. Pillow 模块使用的简单记录

1.导入模块
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
2.实例化
2.1 实例化图片对象
  ImageCaptcha = Image.new(mode, sizee, color)
2.2 创建画笔
  ImageDraw = ImageDraw.Draw(self.ImageCaptcha)
3.画图
3.1.画文字
  ImageDraw .text(xy, text,fill,font)
  xy:字体的坐标(字体的左上角的坐标),
  text:指定的文本内容,
  fill:指定文本的颜色,
  font:指定字体文件(通过ImageFont类来定义)
3.2.画线条
  ImageDraw.line(xy=[begin, end], fill=fill, width=width)
  xy:线条的起始位置,结束位置,
  fill:指定文本的颜色,
  width:线条粗细
3.3.画矩形
  ImageDraw.rectangle( xy, fill=None, outline=None, width=0)
  xy:对角线的起始位置,到结束位置,
  fill:填充颜色,
  outline:指定线条的颜色
3.4.画椭圆
  ImageDraw.arc(xy, start, end, fill=None, width=0)
  xy:椭圆的外切矩形的对角线的起始位置,到结束位置,
  start:起始角度,
  end:结束角度,
  fill:填充颜色,
3.4.画圆
  ImageDraw.pieslice(self, xy, start, end, fill=None, outline=None, width=0)
  xy:圆的外切矩形的对角线的起始位置,到结束位置,
  start:起始角度,
  end:结束角度,
  fill:填充颜色,
  outline:指定线条的颜色
3.4.画点
  ImageDraw.point(self, xy, fill=None)
  xy:坐标,
  fill:填充颜色,

二. 生成一个图片验证码
#!/usr/bin/env python3
#_*_ coding:utf-8 _*_
import string
import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter


class CreateImageCaptcha(object):

    def __init__(self,
                 font_type,  # 必须提供字体文件
                 size=(160, 60),
                 str_characters=string.digits + string.ascii_letters,
                 str_len=4,
                 font_sizes=range(40, 50),
                 bg_color='pink',
                 draw_lines_or_no=True,
                 line_number=(4, 6),
                 draw_points_or_no=True,
                 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成验证码图片可以使用 Python 中的 Pillow 库和随机数库,具体步骤如下: 1. 导入需要的库: ```python from PIL import Image, ImageDraw, ImageFont import random ``` 2. 定义生成随机字符串的函数: ```python def generate_random_str(length): # 定义随机字符串的备选值 candidates = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # 从备选值中随机选择指定长度的字符串 return ''.join(random.choices(candidates, k=length)) ``` 3. 定义生成验证码图片的函数: ```python def generate_verification_code(width, height, length): # 创建画布 image = Image.new('RGB', (width, height), color=(255, 255, 255)) # 创建画笔 draw = ImageDraw.Draw(image) # 设置字体 font = ImageFont.truetype('arial.ttf', size=30) # 生成随机字符串 code = generate_random_str(length) # 在画布上绘制字符串 for i in range(length): draw.text((10 + i * 20, 10), code[i], font=font, fill=random.choice([(0, 0, 0), (255, 0, 0), (0, 0, 255)])) # 添加干扰点 for i in range(100): draw.point((random.randint(0, width), random.randint(0, height)), fill=(0, 0, 0)) # 添加干扰线 for i in range(5): draw.line([(random.randint(0, width), random.randint(0, height)), (random.randint(0, width), random.randint(0, height))], fill=(0, 0, 0)) # 返回验证码图片和对应的字符串 return image, code ``` 4. 调用函数生成验证码图片: ```python image, code = generate_verification_code(120, 40, 4) image.show() ``` 以上代码中,`generate_random_str` 函数用于生成指定长度的随机字符串,`generate_verification_code` 函数用于生成指定宽度、高度和长度的验证码图片,并返回图片对象和对应的字符串。在绘制字符串时,使用了随机的字体颜色,以及添加了干扰点和干扰线,增加了验证码的复杂度。最后调用 `show` 方法显示生成验证码图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值