From db9ed8f87843fc1d29c8830d55a67f80c6053114 Mon Sep 17 00:00:00 2001 From: "l.gabrysiak" Date: Sun, 2 Mar 2025 13:18:50 +0100 Subject: [PATCH] mod letter --- backend/open_webui/models/users.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index 7934b20..d600a77 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -348,35 +348,29 @@ class UsersTable: @staticmethod def generate_image_base64(letter, size=200): - # Upewniamy się, że size jest krotką if isinstance(size, int): size = (size, size) - elif not isinstance(size, (list, tuple)) or len(size) != 2: - raise ValueError("Size must be an integer or a tuple of two integers") - # Tworzenie obrazu z losowym kolorem tła background_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) image = Image.new('RGB', size, color=background_color) - - # Tworzenie obiektu do rysowania draw = ImageDraw.Draw(image) - # Ładowanie czcionki font_size = min(size) // 2 try: font = ImageFont.truetype("arial.ttf", font_size) except IOError: font = ImageFont.load_default() - # Obliczanie pozycji tekstu - text_width, text_height = draw.textsize(letter, font=font) + # Użyj font.getbbox() zamiast draw.textsize() + bbox = font.getbbox(letter) + text_width = bbox[2] - bbox[0] + text_height = bbox[3] - bbox[1] + position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) - # Rysowanie litery text_color = (255 - background_color[0], 255 - background_color[1], 255 - background_color[2]) draw.text(position, letter, font=font, fill=text_color) - # Konwersja obrazu do base64 buffered = io.BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode()