1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| def generate_image(code, width, height, font_size): img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255)) draw = ImageDraw.Draw(img) font = ImageFont.truetype('arial.ttf', font_size) bbox = draw.textbbox((0, 0, width, height), code, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1]
x = (width - text_width) // 2 y = (height - text_height) // 2 draw.text((x, y), code, font=font, fill=(0, 0, 0)) 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=(0, 0, 0), width=2) for i in range(50): x = random.randint(0, width) y = random.randint(0, height) draw.point((x, y), fill=(0, 0, 0)) def generate_image(code, width, height, font_size): img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255)) draw = ImageDraw.Draw(img) font = ImageFont.truetype('arial.ttf', font_size) bbox = draw.textbbox((0, 0, width, height), code, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1]
x = (width - text_width) // 2 y = (height - text_height) // 2 draw.text((x, y), code, font=font, fill=(0, 0, 0)) 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=(0, 0, 0), width=2) for i in range(50): x = random.randint(0, width) y = random.randint(0, height) draw.point((x, y), fill=(0, 0, 0)) return img
|