把验证码变大

This commit is contained in:
2026-06-12 19:46:35 +08:00
parent 31c4d460db
commit 4fbb4d5cde
3 changed files with 750 additions and 5 deletions

View File

@@ -90,7 +90,7 @@ def generate_captcha_text(length: int = 4) -> str:
def generate_captcha_image(text: str) -> Image.Image:
"""生成验证码图片(含干扰线和噪点)"""
width, height = 140, 48
width, height = 220, 70
# 浅色随机背景
bg_color = (random.randint(235, 255), random.randint(235, 255), random.randint(235, 255))
image = Image.new('RGB', (width, height), bg_color)
@@ -104,20 +104,20 @@ def generate_captcha_image(text: str) -> Image.Image:
draw.line([(x1, y1), (x2, y2)], fill=line_color, width=1)
# 噪点
for _ in range(80):
for _ in range(120):
x, y = random.randint(0, width - 1), random.randint(0, height - 1)
dot_color = (random.randint(140, 200), random.randint(140, 200), random.randint(140, 200))
draw.point((x, y), fill=dot_color)
# 绘制文字
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 50)
except OSError:
font = ImageFont.load_default()
for i, char in enumerate(text):
x = 12 + i * 30
y = random.randint(4, 10)
x = 16 + i * 50
y = random.randint(4, 12)
color = (random.randint(0, 80), random.randint(0, 80), random.randint(0, 80))
draw.text((x, y), char, font=font, fill=color)