From c79680ec2d8548c9e4a150ae1b7b647e6c6d40da Mon Sep 17 00:00:00 2001 From: "l.gabrysiak" Date: Sun, 2 Mar 2025 14:13:18 +0100 Subject: [PATCH] mod letter --- backend/open_webui/models/users.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index 0369e1b..295562a 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -355,27 +355,39 @@ class UsersTable: image = Image.new('RGB', size, color=background_color) draw = ImageDraw.Draw(image) + # Ustawienie koloru czcionki na biały text_color = (255, 255, 255) - def get_font_size(image_size, text, target_percentage=0.8): + # Funkcja do obliczania rozmiaru czcionki + def get_font_size(image_size, text, font_path, target_percentage=0.8): font_size = 1 - while True: - font = ImageFont.truetype("arial.ttf", font_size) - bbox = font.getbbox(text) - if bbox[2] > target_percentage * image_size[0] or bbox[3] > target_percentage * image_size[1]: - return font_size - 1 + font = ImageFont.truetype(font_path, font_size) + while font.getbbox(text)[2] < target_percentage * image_size[0] and \ + font.getbbox(text)[3] < target_percentage * image_size[1]: font_size += 1 + font = ImageFont.truetype(font_path, font_size) + return font_size - 1 - font_size = get_font_size(size, letter) - font = ImageFont.truetype("arial.ttf", font_size) + try: + font_path = "arial.ttf" + font_size = get_font_size(size, letter, font_path) + font = ImageFont.truetype(font_path, font_size) + except IOError: + # Jeśli Arial nie jest dostępny, użyj czcionki domyślnej + font = ImageFont.load_default() + # Dla czcionki domyślnej możemy potrzebować innej metody określania rozmiaru + font_size = 20 #min(size) // 2 # To jest przybliżenie, może wymagać dostosowania + # Obliczanie pozycji tekstu 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 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()