from io import BytesIO from pathlib import Path from random import randint, sample from typing import List from huepaper import generate from PIL import Image from classes.captcha import Captcha def get_captcha_image(emojis: List[str]) -> Captcha: emojis_all = sample(emojis, 12) emojis_correct = sample(emojis_all, 6) output = BytesIO() image_options = [ (randint(400, 490), randint(90, 280), randint(105, 125)), (randint(60, 180), randint(180, 240), randint(75, 95)), (randint(320, 440), randint(170, 210), randint(35, 45)), (randint(150, 240), randint(240, 320), randint(80, 105)), (randint(350, 450), randint(280, 380), randint(40, 60)), (randint(180, 350), randint(100, 300), randint(45, 65)), ] base_img = generate( width=500, height=500, hue_max=1.0, lum_min=0.3, lum_max=0.6, sat_min=0.8, sat_max=1.0, ) for options, emoji in zip(image_options, emojis_correct): width, height, angle = options base_img.paste( Image.open(Path(f"assets/emojis/{emoji}.png")) .resize((120, 120)) .rotate(angle), ((base_img.width - width), (base_img.height - height)), Image.open(Path(f"assets/emojis/{emoji}.png")) .resize((120, 120)) .rotate(angle), ) base_img.save(output, format="jpeg") return Captcha(output, emojis_all, emojis_correct)