/
android113
/
schedule_tg_bot
Обзор
Документация
Войти
/
android113
/
schedule_tg_bot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/apps/image_generator.py
104 строки
4 KB
Скороходов Андрей Андреевич
added interactive buttons
13 окт 2025, 17:50
13 окт 2025, 17:50
59efa0d
Код
Авторство
О чём код?
import io import math from PIL import Image, ImageDraw, ImageFont def create_image_v5_final( text: str, font_path: str, text_color: tuple = (255, 255, 255, 230), text_offset_x: int = 15, image_size: int = 800, ) -> io.BytesIO | None: # --- (Твой код без изменений) bg_color = (255, 255, 255) image = Image.new("RGBA", (image_size, image_size), color=bg_color) draw = ImageDraw.Draw(image) color_stops = [(0.0, (255, 165, 0)), (0.5, (255, 0, 0)), (1.0, (139, 0, 0))] center, radius = image_size / 2, image_size / 2 * 0.95 for y in range(int(center - radius), int(center + radius)): progress = (y - (center - radius)) / (2 * radius) start_stop, end_stop = color_stops[0], color_stops[1] for i in range(len(color_stops) - 1): if color_stops[i][0] <= progress < color_stops[i + 1][0]: start_stop, end_stop = color_stops[i], color_stops[i + 1] break local_progress = (progress - start_stop[0]) / (end_stop[0] - start_stop[0]) r, g, b = [ int( start_stop[1][c] * (1 - local_progress) + end_stop[1][c] * local_progress ) for c in range(3) ] draw.line( ( center - math.sqrt(radius**2 - (y - center) ** 2), y, center + math.sqrt(radius**2 - (y - center) ** 2), y, ), fill=(r, g, b), ) try: font_size = 150 font = ImageFont.truetype(font_path, font_size) padding = 80 while font.getbbox(text)[2] > radius * 2 - padding and font_size > 10: font_size -= 2 font = ImageFont.truetype(font_path, font_size) _, _, text_w, text_h = draw.textbbox((0, 0), text, font=font) pos_x, pos_y = ( (image_size - text_w) / 2 + text_offset_x, (image_size - text_h) / 2, ) shadow_offset = font_size // 25 draw.text( (pos_x + shadow_offset, pos_y + shadow_offset), text, font=font, fill=(0, 0, 0, 50), ) draw.text((pos_x, pos_y), text, font=font, fill=text_color) except IOError: print(f"Не удалось загрузить шрифт: {font_path}") return None image_stream = io.BytesIO() image.convert("RGB").save(image_stream, format="PNG") image_stream.seek(0) return image_stream def create_image_with_gradient_sun(*args, **kwargs): pass # Эта функция у нас больше не используется def create_text_image( text: str, font_path: str, background_path: str, text_color: tuple = (255, 255, 255, 230), font_size: int = 50, # Этот font_size нужно будет сделать адаптивным, как в v5 ) -> io.BytesIO | None: try: font = ImageFont.truetype(font_path, font_size) except IOError: print(f"Не удалось загрузить шрифт: {font_path}") font = ImageFont.load_default() try: image = Image.open(background_path).convert("RGB") image_width, image_height = image.size except FileNotFoundError: print(f"Фон не найден: {background_path}") return None draw = ImageDraw.Draw(image) _, _, text_w, text_h = draw.textbbox((0, 0), text, font=font) position = ((image_width - text_w) / 2, (image_height - text_h) / 2) draw.text(position, text, font=font, fill=text_color[:3]) image_stream = io.BytesIO() image.save(image_stream, format="PNG") image_stream.seek(0) return image_stream