mod letter
This commit is contained in:
parent
0791553ee4
commit
e4ed8a0cbd
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue