mod letter

This commit is contained in:
l.gabrysiak 2025-03-02 13:43:02 +01:00
parent dd65611fff
commit 81b89a81f5
1 changed files with 10 additions and 15 deletions

View File

@ -359,24 +359,19 @@ class UsersTable:
text_color = (255, 255, 255)
# Funkcja do obliczania rozmiaru czcionki
def get_font_size(image_size, text, font_path, target_percentage=0.8):
def get_font_size(image_size, text, target_percentage=0.8):
font_size = 1
font = ImageFont.truetype(font_path, font_size)
while font.getbbox(text)[2] < target_percentage * image_size[0] and \
font.getbbox(text)[3] < target_percentage * image_size[1]:
font = ImageFont.load_default().font.copy()
while True:
font.set_size(font_size)
bbox = font.getbbox(text)
if bbox[2] > target_percentage * image_size[0] or bbox[3] > target_percentage * image_size[1]:
return font_size - 1
font_size += 1
font = ImageFont.truetype(font_path, font_size)
return font_size - 1
try:
font_path = "arial.ttf"
font_size = get_font_size(size, letter, font_path)
font = ImageFont.truetype(font_path, font_size)
except IOError:
# Jeśli Arial nie jest dostępny, użyj czcionki domyślnej
font = ImageFont.load_default()
# Dla czcionki domyślnej możemy potrzebować innej metody określania rozmiaru
font_size = min(size) // 2 # To jest przybliżenie, może wymagać dostosowania
font_size = get_font_size(size, letter)
font = ImageFont.load_default().font.copy()
font.set_size(font_size)
# Obliczanie pozycji tekstu
bbox = font.getbbox(letter)