gonzopi

git clone https://git.tarina.org/gonzopi
Log | Files | Refs | README | LICENSE

titlemaker.py (2391B)


      1 #!/usr/bin/env python3
      2 # -*- coding: utf-8 -*-
      3 
      4 from PIL import Image, ImageDraw, ImageFont
      5 import os
      6 
      7 def create_title_png(text, output_path, font_path=None, font_size=100, text_color="white"):
      8     """
      9     Create a Full HD (1920x1080) PNG image with centered text and transparent background.
     10     
     11     Parameters:
     12     text (str): Text to display on the image
     13     output_path (str): Path to save the PNG file
     14     font_path (str): Path to TTF font file (optional, uses default if None)
     15     font_size (int): Font size for the text
     16     text_color (str): Color of the text (e.g., "white", "#FFFFFF")
     17     """
     18     try:
     19         # Create a new Full HD image with transparent background (RGBA mode)
     20         image = Image.new('RGBA', (1920, 1080), (0, 0, 0, 0))  # Fully transparent background
     21         draw = ImageDraw.Draw(image)
     22         
     23         # Load font (use default PIL font if none provided)
     24         if font_path and os.path.exists(font_path):
     25             font = ImageFont.truetype(font_path, font_size)
     26         else:
     27             font = ImageFont.load_default()
     28             # Scale default font size (Pillow's default is small)
     29             if font_size != 100:
     30                 print("Warning: Custom font size ignored when using default font")
     31         
     32         # Get text size to center it (use textsize for older Pillow versions)
     33         text_width, text_height = draw.textsize(text, font=font)
     34         
     35         # Calculate centered position
     36         text_x = (1920 - text_width) // 2
     37         text_y = (1080 - text_height) // 2
     38         
     39         # Draw the text
     40         draw.text((text_x, text_y), text, fill=text_color, font=font)
     41         
     42         # Save the image as PNG to preserve transparency
     43         image.save(output_path, 'PNG')
     44         print(f"Transparent title image saved to {output_path}")
     45         
     46     except Exception as e:
     47         print(f"An error occurred: {str(e)}")
     48 
     49 # Example usage
     50 if __name__ == "__main__":
     51     title_text = "GONZO PI IS AWESOME"
     52     output_file = "title001.png"
     53     
     54     # Optional: Specify a TTF font file (e.g., Arial, downloaded TTF, etc.)
     55     # font_path = "path/to/your/font.ttf"  # Uncomment and set path if you have a TTF font
     56     font_path = None  # Use default font if None
     57     
     58     create_title_png(
     59         text=title_text,
     60         output_path=output_file,
     61         font_path='roberta.ttf',
     62         font_size=100,
     63         text_color="white"
     64     )