Skip to content

Instantly share code, notes, and snippets.

@paalbra
Created February 4, 2025 09:48
Show Gist options
  • Select an option

  • Save paalbra/a55e8e32ab40b0cf143df44ec2405771 to your computer and use it in GitHub Desktop.

Select an option

Save paalbra/a55e8e32ab40b0cf143df44ec2405771 to your computer and use it in GitHub Desktop.
Scrolling banner text
import os
import numpy as np
from PIL import Image, ImageDraw, ImageFont
def gettextwidth(font, text):
x_min, y_min, x_max, y_max = font.getbbox(text)
return (x_max - x_min, y_max - y_min)
text = "AKKURAT NÅ"
font_path = "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans.ttf"
font_size = 48
image_width = 2500
image_height = 100
output_folder = "output_frames"
text_padding = 80
font = ImageFont.truetype(font_path, font_size)
text_x, text_y = gettextwidth(font, text)
y_padding = (image_height - text_y) // 2
text_unit_width = text_x + text_padding
frame_count = text_unit_width
move_pixels = 2
for frame_num in range(frame_count//move_pixels):
img = Image.new("RGB", (image_width, image_height), "red")
draw = ImageDraw.Draw(img)
x_offset = -frame_num * move_pixels
text_coordinate = 0
for text_coordinate in range(0, image_width + text_unit_width, text_unit_width):
draw.text((x_offset + text_coordinate, y_padding), text, font=font, fill="white")
img.save(os.path.join(output_folder, f"frame_{frame_num:04d}.png"))
print(f"Frames saved in {output_folder}/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment