目录
一、登录-随机生成图片验证码
环境: python10, pillow==10.2.0
pip install pillow
1、创建图片
from PIL import Image, ImageDraw
img = Image.new(mode='RGB', size=(500, 140), color=(255, 255, 255))
with open('code.png', 'wb')as f:
img.save(f, format='png')
参数:
mode=‘RGB’表示用RGB表示颜色。
size=(120, 30)表示坐标
color=(255, 255, 255) 表示白色
2、创建画笔
from PIL import Image, ImageDraw
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB') # 创建画笔对象draw
img.show() # 在图片查看器中打开,这句会调用系统默认的图片管理工具
3、画点-point()方法
from PIL import Image, ImageDraw
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# point()第一个参数:表示坐标, 第二个参数:表示颜色
draw.point([10, 20], fill='red')
draw.point([100, 100], fill=(0, 255, 0))
# 保存在本地
with open('code.png', 'wb') as f:
img.save(f, format='png')
4、画线-line()
from PIL import Image, ImageDraw
img = Image.new(mode='RGB', size=(540, 150), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# line()第一个参数:表示起始坐标和结束坐标,第二个参数:表示颜色
draw.line((50, 50, 50, 150), fill='red')
# 上面一句表示画一条坐标(x=100,y=100)到(x=100,y=300)的直线
with open('code.png', 'wb') as f:
img.save(f, format='png')
5、画圆-arc()方法
from PIL import Image, ImageDraw
img = Image.new(mode='RGB', size=(500, 140), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB') # 创建画笔对象
draw.arc((200,20,300,120), 0, 360, fill='red')
# 保存在本地
with open('code.png', 'wb')as f:
img.save(f, format='png')
arc()方法的参数说明
-
起始坐标和结束左边(圆要画在其中间,两点确定矩形的内切圆)
-
开始角度,
-
结束角度
-
表示颜色
6、写文本-text()方法
draw.text([0,0], 'python', 'red')
参数:
- 起始坐标
- 写入的文本
- 颜色
7、特殊字体文件
from PIL import Image, ImageDraw, ImageFont
img = Image.new(mode='RGB', size=(500, 140), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB') # 创建画笔对象
font = ImageFont.truetype('xxx.ttf', 28)
draw.text([0,0], 'python', 'red', font=font)
# 保存在本地
with open('code.png', 'wb')as f:
img.save(f, format='png')
8、随机生成图片验证码
import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter
def check_code(width=120, height=30, char_length=5, font_file='./static/font/k.ttf', font_size=28):
code = []
img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
def rndChar():
"""
生成随机字符(包括大小写字母和数字)
:return:
"""
ranNum = str(random.randint(0, 9))
ranLower = chr(random.randint(65, 90))
ranUpper = chr(random.randint(97, 120))
return random.choice([ranNum, ranLower, ranUpper])
def rndColor():
"""
生成随机颜色
:return:
"""
return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
# 写文字
font = ImageFont.truetype(font_file, font_size)
for i in range(char_length):
char = rndChar()
code.append(char)
h = ( height - font_size ) / 2
draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
# 写干扰点
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
# 写干扰圆圈
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
# 画干扰线
for i in range(5):
x1 = random.randint(0, width)
y1 = random.randint(0, height)
x2 = random.randint(0, width)
y2 = random.randint(0, height)
draw.line((x1, y1, x2, y2), fill=rndColor())
# 对图像加滤波 - 深度边缘增强滤波
img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
return img, ''.join(code)
if __name__ == '__main__':
# 1. 直接打开,即用图片查看器查看
# img,code = check_code()
# img.show()
# 2. 写入文件
# img,code = check_code()
# with open('code.png','wb') as f: # f是写入磁盘的文件句柄
# img.save(f, format='png')
# data = f.read() # data是读取图片的字节
# 3. 写入内存(Python3)
# img,code = check_code()
# from io import BytesIO # 内存管理的模块
# stream = BytesIO() # stream是写入内存的文件句柄
# img.save(stream, 'png')
# data = stream.getvalue()
标签:draw,code,img,randint,创建,random,验证码,pillow,255
From: https://www.cnblogs.com/journeyer-xsh/p/18070276