mod user (avatar generator)

This commit is contained in:
l.gabrysiak 2025-03-02 12:07:41 +01:00
parent 977e47cd72
commit 7a43d01cdf
1 changed files with 32 additions and 0 deletions

View File

@ -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.chats import Chats
from open_webui.models.groups import Groups from open_webui.models.groups import Groups
from PIL import Image, ImageDraw, ImageFont
import random
from pydantic import BaseModel, ConfigDict from pydantic import BaseModel, ConfigDict
from sqlalchemy import BigInteger, Column, String, Text, JSON from sqlalchemy import BigInteger, Column, String, Text, JSON
@ -342,6 +344,36 @@ class UsersTable:
users = db.query(User).filter(User.id.in_(user_ids)).all() users = db.query(User).filter(User.id.in_(user_ids)).all()
return [user.id for user in users] 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: 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: try:
with get_db() as db: with get_db() as db: