From c592b0263647ef15f6e47f567dbea1f6d0fc912c Mon Sep 17 00:00:00 2001 From: shufeng2012 <277665039@qq.com> Date: Tue, 28 Jul 2026 20:17:28 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=BA=20=E6=9B=B4=E6=96=B0=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E6=B7=B7=E6=B7=86=E7=A8=8B=E5=BA=A6=EF=BC=88?= =?UTF-8?q?=E6=88=91=E8=87=AA=E5=B7=B1=E9=83=BD=E8=BF=87=E4=B8=8D=E4=BA=86?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- captcha/captcha_service.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/captcha/captcha_service.py b/captcha/captcha_service.py index 054749b..c602303 100644 --- a/captcha/captcha_service.py +++ b/captcha/captcha_service.py @@ -56,28 +56,34 @@ def generate_text(): def generate_image(text): width, height = 160, 50 - bg = random.choice([(255, 255, 255), (240, 240, 240), (230, 240, 250)]) + bg = (random.randint(1,256), random.randint(1,256), random.randint(1,256)) img = Image.new('RGB', (width, height), bg) draw = ImageDraw.Draw(img) for i, ch in enumerate(text): - color = random.choice([(30, 30, 30), (50, 80, 150), (150, 50, 50), (50, 130, 50)]) + color = (random.randint(1,256), random.randint(1,256), random.randint(1,256)) y = random.randint(2, 12) x = 15 + i * 32 + random.randint(-3, 3) # 4 位字符在 160px 宽度内均匀分布 try: font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", - random.choice([28, 30, 32, 34])) + random.choice(range(20,40,2))) except (IOError, OSError): font = ImageFont.load_default() - draw.text((x, y), ch, fill=color, font=font) + + char = Image.new("RGBA", (64, 64), (0,0,0,0)) + draw_char = ImageDraw.Draw(char) + draw_char.text((20,20), ch, fill=color, font=font) + rotated_char = char.rotate(random.randint(-30, 30), expand=True) + img.paste(rotated_char, (x-32, y-32), mask=rotated_char) + # draw.text((x, y), ch, fill=color, font=font) for _ in range(4): draw.line([(random.randint(0, width), random.randint(0, height)), (random.randint(0, width), random.randint(0, height))], - fill=random.choice([(30, 30, 30), (50, 80, 150)]), width=1) + fill=(random.randint(1,256), random.randint(1,256), random.randint(1,256))) for _ in range(50): draw.point((random.randint(0, width - 1), random.randint(0, height - 1)), - fill=random.choice([(30, 30, 30), (50, 80, 150)])) + fill=(random.randint(1,256), random.randint(1,256), random.randint(1,256))) buf = io.BytesIO() img.save(buf, format='PNG')