Python生成验证码
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,
):
""" :param font_type: 字体文件;必须提供字体文件 :param size: 图片大小 默认(160, 60) :param str_characters: 字符集;默认字母+数字 :param str_len: 绘制的字符数量;默认4个字符 :param font_sizes: 绘制的字符字体大小;默认40-50 :param bg_color: 背景颜色;默认为pink' :param draw_lines_or_no: 是否划干扰线;默认打开 :param line_number: 干扰线的条数范围; :param draw_points_or_no: 是否画干扰点;默认打开 """
self.Image_width, self.Image_height = size
self.str_characters = str_characters
self.str_len = str_len
self.random_str = ''.join(random.sample(self.str_characters, str_len))
self.Image_fonts = font_type
self.Image_font_sizes = font_sizes
self.Image_bg_color = bg_color
self.draw_lines_or_no = draw_lines_or_no
self.line_number = line_number
self.draw_points_or_no = draw_points_or_no
#
self.ImageCaptcha = Image.new(mode='RGB', size=size, color=self.Image_bg_color) # 实例化图片对象
self.ImageDraw = ImageDraw.Draw(self.ImageCaptcha) # 创建画笔
def get_random_Color(self):
"""随机颜色"""
c1 = random.randint(10, 200)
c2 = random.randint(10, 200)
c3 = random.randint(10, 200)
return (c1, c2, c3)
def draw_str_(self):
"""画字符"""
# 提供字体
font = ImageFont.truetype(font=self.Image_fonts, size=random.choice(self.Image_font_sizes))
w_width = self.Image_width / self.str_len # 将图片宽度按字符个数等分
for index, str in enumerate(self.random_str):
font_width, font_height = font.getsize(str) # 获取字符的宽和高
# print(font_width, font_height)
font_space_w = int(w_width - font_width) # 用图片等分的宽减去字符宽,得到字符在这个区域横向活动范围
font_space_h = int(self.Image_height - font_height) # 用图片高减去字符宽,得到字符在这个区域纵向活动范围
# 根据活动范围,计算每个字符绘画的起始位置
xy = (w_width*index + random.choice(range(0, font_space_w)), random.choice(range(0, font_space_h)))
# 传参,画字符
self.ImageDraw.text(xy=xy, text=str, font=font, fill=self.get_random_Color())
def draw_line_(self):
"""画干扰线"""
if self.draw_lines_or_no:
line_num = random.randint(*self.line_number) # 随机干扰线条数
for i in range(line_num):
# 起始点
begin = (random.randint(0, self.Image_width), random.randint(0, self.Image_height))
# 结束点
end = (random.randint(0, self.Image_width), random.randint(0, self.Image_height))
# 线颜色
fill = self.get_random_Color()
# 线粗细
width = random.choice(range(6))
# 传参,画线条
self.ImageDraw.line(xy=[begin, end], fill=fill, width=width)
def draw_points_(self):
"""画干扰点"""
if self.draw_points_or_no:
proints_number = random.choice(range(500, 600))
for n in range(proints_number):
x = int(random.choice(range(self.Image_width)))
y = int(random.choice(range(self.Image_height)))
fill = self.get_random_Color()
self.ImageDraw.point(xy=(x, y), fill=fill)
# # 图片旋转
def rotate_(self):
deg = int(self.Image_height / 3) # 旋转角度
self.ImageCaptcha = self.ImageCaptcha.rotate(random.randint(0, deg), expand=0)
def create_image(self):
self.draw_str_()
self.draw_line_()
self.draw_points_()
return self.ImageCaptcha, self.random_str
if __name__ == '__main__':
# 字体文件
font_type = 'fanxinshu.TTF'
# 实例化创建验证码的类
create = CreateImageCaptcha(font_type=font_type)
imgs, str = create.create_image()
print(str)
imgs.save('my_test.png')
还没有评论,来说两句吧...