From e4ed8a0cbd04159f9875a19cb6bc7dd97334c331 Mon Sep 17 00:00:00 2001 From: "l.gabrysiak" Date: Sun, 2 Mar 2025 12:55:44 +0100 Subject: [PATCH] mod letter --- backend/open_webui/models/users.py | 41 +++++++++++++----------------- backend/open_webui/utils/oauth.py | 11 ++++---- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index 9d0cb93..b8f688a 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -10,7 +10,7 @@ from open_webui.models.groups import Groups from PIL import Image, ImageDraw, ImageFont import random import base64 -from io import BytesIO +import io from pydantic import BaseModel, ConfigDict from sqlalchemy import BigInteger, Column, String, Text, JSON @@ -346,38 +346,31 @@ class UsersTable: users = db.query(User).filter(User.id.in_(user_ids)).all() return [user.id for user in users] - def generate_letter_image(letter: str, image_size=(200, 200), font_size=100, font_path=None): - """ - Generuje obraz z podaną literą i losowym kolorem tła, zwracając go w formacie base64. - - :param letter: Pojedyncza litera do wyświetlenia - :param image_size: Rozmiar obrazu (szerokość, wysokość) - :param font_size: Rozmiar czcionki - :param font_path: Ścieżka do pliku czcionki (jeśli None, używana jest domyślna) - """ - if not isinstance(letter, str) or len(letter) != 1: - raise ValueError("Parametr 'letter' musi być pojedynczym znakiem.") - + def generate_image_base64(letter, size=(200, 200)): + # Tworzenie obrazu z losowym kolorem tła background_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) - text_color = (255, 255, 255) # Biały kolor czcionki + image = Image.new('RGB', size, color=background_color) - image = Image.new("RGB", image_size, background_color) + # Tworzenie obiektu do rysowania draw = ImageDraw.Draw(image) - try: - font = ImageFont.truetype(font_path if font_path else "arial.ttf", font_size) - except IOError: - font = ImageFont.load_default() + # Ładowanie czcionki + font_size = min(size) // 2 + font = ImageFont.truetype("arial.ttf", font_size) + # Obliczanie pozycji tekstu text_width, text_height = draw.textsize(letter, font=font) - text_x = (image_size[0] - text_width) // 2 - text_y = (image_size[1] - text_height) // 2 + position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) - draw.text((text_x, text_y), letter, font=font, fill=text_color) + # 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) - buffered = BytesIO() + # Konwersja obrazu do base64 + buffered = io.BytesIO() image.save(buffered, format="PNG") - img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") + img_str = base64.b64encode(buffered.getvalue()).decode() + return f"data:image/png;base64,{img_str}" def update_user_profile(self, id: str, name: str, profile_image_url: str, role: str, permissions: Optional[dict] = None, subscription: Optional[List[str]] = None) -> bool: diff --git a/backend/open_webui/utils/oauth.py b/backend/open_webui/utils/oauth.py index 25be2db..5835f5f 100644 --- a/backend/open_webui/utils/oauth.py +++ b/backend/open_webui/utils/oauth.py @@ -308,8 +308,9 @@ class OAuthManager: Users.update_user_oauth_sub_by_id(user.id, provider_sub) if user: - generated_image = Users.generate_letter_image('A') - print(generated_image) + letter = "A" + base64_image = Users.generate_image_base64(letter) + print(base64_image) Auths.update_user_profile( user.id, @@ -321,9 +322,9 @@ class OAuthManager: ) if not user: - - generated_image = Users.generate_letter_image('A') - print(generated_image) + letter = "A" + base64_image = Users.generate_image_base64(letter) + print(base64_image) user_count = Users.get_num_users()