mod letter

This commit is contained in:
l.gabrysiak 2025-03-02 16:07:08 +01:00
parent 2282f39d38
commit 48de5871e3
1 changed files with 27 additions and 30 deletions

View File

@ -9,8 +9,9 @@ from open_webui.models.groups import Groups
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
import random import random
import base64
import io import io
import base64
import requests
import requests import requests
@ -347,7 +348,7 @@ class UsersTable:
with get_db() as db: with get_db() as db:
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]
@staticmethod @staticmethod
def generate_image_base64(letter, size=200): def generate_image_base64(letter, size=200):
if isinstance(size, int): if isinstance(size, int):
@ -356,55 +357,51 @@ class UsersTable:
background_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) background_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
image = Image.new('RGB', size, color=background_color) image = Image.new('RGB', size, color=background_color)
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
# Ustawienie koloru czcionki na biały
text_color = (255, 255, 255) text_color = (255, 255, 255)
# Funkcja do obliczania rozmiaru czcionki def get_font_size(image_size, text, font_source, target_percentage=0.8):
def get_font_size(image_size, text, font_path, target_percentage=0.8):
font_size = 1 font_size = 1
font = ImageFont.truetype(font_path, font_size) while True:
while font.getbbox(text)[2] < target_percentage * image_size[0] and \ if isinstance(font_source, bytes):
font.getbbox(text)[3] < target_percentage * image_size[1]: font_file = io.BytesIO(font_source)
font = ImageFont.truetype(font_file, font_size)
elif isinstance(font_source, io.BytesIO):
font_data = font_source.getvalue()
font_file = io.BytesIO(font_data)
font = ImageFont.truetype(font_file, font_size)
else:
font = ImageFont.truetype(font_source, font_size)
bbox = font.getbbox(text)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
if (text_width >= target_percentage * image_size[0] or
text_height >= target_percentage * image_size[1]):
break
font_size += 1 font_size += 1
font = ImageFont.truetype(font_path, font_size)
return font_size - 1 return font_size - 1
try: try:
font_path = "arial.ttf" font_path = "arial.ttf"
font_size = get_font_size(size, letter, font_path) font_size = get_font_size(size, letter, font_path)
print(f"1) {font_size}")
font = ImageFont.truetype(font_path, font_size) font = ImageFont.truetype(font_path, font_size)
except IOError: except IOError:
# Jeśli Arial nie jest dostępny, użyj czcionki domyślnej font_url = "https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2"
#font = ImageFont.load_default()
# Dla czcionki domyślnej możemy potrzebować innej metody określania rozmiaru
#font_size = min(size) * 10 # To jest przybliżenie, może wymagać dostosowania
#print(f"2) {font_size}")
# Pobierz czcionkę z Google Fonts (np. Roboto)
font_url = "https://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxP.ttf"
response = requests.get(font_url) response = requests.get(font_url)
font_file = io.BytesIO(response.content) font_data = response.content
font_size = get_font_size(size, letter, font_file) font_size = get_font_size(size, letter, font_data)
font_file = io.BytesIO(font_data)
font = ImageFont.truetype(font_file, font_size)
# Użyj pobranej czcionki
font = ImageFont.truetype(font_file, size=font_size)
#print(f"2) {font_size}")
# Obliczanie pozycji tekstu
bbox = font.getbbox(letter) bbox = font.getbbox(letter)
text_width = bbox[2] - bbox[0] text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1] text_height = bbox[3] - bbox[1]
position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) position = ((size[0] - text_width) // 2, (size[1] - text_height) // 2)
# Rysowanie litery
draw.text(position, letter, font=font, fill=text_color) draw.text(position, letter, font=font, fill=text_color)
# Konwersja obrazu do base64
buffered = io.BytesIO() buffered = io.BytesIO()
image.save(buffered, format="PNG") image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode() img_str = base64.b64encode(buffered.getvalue()).decode()
return f"data:image/png;base64,{img_str}" 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: 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: