mod letter

This commit is contained in:
l.gabrysiak 2025-03-02 14:05:36 +01:00
parent 81b89a81f5
commit 195e02e41d
1 changed files with 2 additions and 9 deletions

View File

@ -355,34 +355,27 @@ class UsersTable:
image = Image.new('RGB', size, color=background_color) image = Image.new('RGB', size, color=background_color)
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
# Ustawienie koloru czcionki na biały
text_color = (255, 255, 255) text_color = (255, 255, 255)
# Funkcja do obliczania rozmiaru czcionki
def get_font_size(image_size, text, target_percentage=0.8): def get_font_size(image_size, text, target_percentage=0.8):
font_size = 1 font_size = 1
font = ImageFont.load_default().font.copy()
while True: while True:
font.set_size(font_size) font = ImageFont.truetype("arial.ttf", font_size)
bbox = font.getbbox(text) bbox = font.getbbox(text)
if bbox[2] > target_percentage * image_size[0] or bbox[3] > target_percentage * image_size[1]: if bbox[2] > target_percentage * image_size[0] or bbox[3] > target_percentage * image_size[1]:
return font_size - 1 return font_size - 1
font_size += 1 font_size += 1
font_size = get_font_size(size, letter) font_size = get_font_size(size, letter)
font = ImageFont.load_default().font.copy() font = ImageFont.truetype("arial.ttf", font_size)
font.set_size(font_size)
# Obliczanie pozycji tekstu
bbox = font.getbbox(letter) bbox = font.getbbox(letter)
text_width = bbox[2] - bbox[0] text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1] text_height = bbox[3] - bbox[1]
position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2)
# Rysowanie litery
draw.text(position, letter, font=font, fill=text_color) draw.text(position, letter, font=font, fill=text_color)
# Konwersja obrazu do base64
buffered = io.BytesIO() buffered = io.BytesIO()
image.save(buffered, format="PNG") image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode() img_str = base64.b64encode(buffered.getvalue()).decode()