import os from PIL import Image, ImageDraw, ImageFont def measure_text(draw, text, font): # اندازه متن با باکس متن (textbbox) یا fallback if text == "": return 0, 0 if hasattr(draw, "textbbox"): bbox = draw.textbbox((0, 0), text, font=font) return bbox[2] - bbox[0], bbox[3] - bbox[1] elif hasattr(draw, "textsize"): return draw.textsize(text, font=font) elif hasattr(font, "getsize"): return font.getsize(text) else: return 0, 0 def create_hero(title="API to PNG", subtitle="Offline to PNG with Pillow on HuggingFace", width=1200, height=630, top_color=(20, 40, 120), bottom_color=(10, 10, 40), out_path="hero.png"): img = create_gradient(width, height, top_color, bottom_color) draw = ImageDraw.Draw(img) try: font_title = ImageFont.truetype("arial.ttf", 72) font_sub = ImageFont.truetype("arial.ttf", 28) except IOError: font_title = ImageFont.load_default() font_sub = ImageFont.load_default() w_title, h_title = measure_text(draw, title, font_title) w_sub, h_sub = measure_text(draw, subtitle, font_sub) x_title = (width - w_title) / 2 y_title = height // 2 - h_title x_sub = (width - w_sub) / 2 y_sub = y_title + h_title + 20 draw.text((x_title, y_title), title, font=font_title, fill=(255, 255, 255)) draw.text((x_sub, y_sub), subtitle, font=font_sub, fill=(230, 230, 230)) img.save(out_path) print(f"Saved {out_path}") if __name__ == "__main__": create_hero()