From 7a43d01cdfa8bf7eb0ec67a23fc7a89f6bfe58ab Mon Sep 17 00:00:00 2001 From: "l.gabrysiak" Date: Sun, 2 Mar 2025 12:07:41 +0100 Subject: [PATCH] mod user (avatar generator) --- backend/open_webui/models/users.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index 4640ae7..93fe217 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -7,6 +7,8 @@ from open_webui.internal.db import Base, JSONField, get_db from open_webui.models.chats import Chats from open_webui.models.groups import Groups +from PIL import Image, ImageDraw, ImageFont +import random from pydantic import BaseModel, ConfigDict from sqlalchemy import BigInteger, Column, String, Text, JSON @@ -341,7 +343,37 @@ class UsersTable: with get_db() as db: users = db.query(User).filter(User.id.in_(user_ids)).all() return [user.id for user in users] + + def generate_letter_image(letter, image_size=(200, 200), font_size=100, font_path=None): + """ + Generuje obraz z podaną literą i losowym kolorem tła. + :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.") + + 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", image_size, background_color) + draw = ImageDraw.Draw(image) + + try: + font = ImageFont.truetype(font_path if font_path else "arial.ttf", font_size) + except IOError: + font = ImageFont.load_default() + + 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 + + draw.text((text_x, text_y), letter, font=font, fill=text_color) + return image + 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: try: with get_db() as db: