Created
August 16, 2025 16:26
-
-
Save motebaya/2eb7ab4b9179aed9afc0fff0c695a405 to your computer and use it in GitHub Desktop.
draw frames statistic into PNG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# @github.com/motebaya - 16/08/2025 | |
# frames statistic into PNG!!! | |
import os | |
from PIL import Image, ImageDraw, ImageFont | |
def draw_stat(img_path: str, text: str, font: str, font_size: int) -> None: | |
img = Image.open(img_path).convert("RGBA") | |
overlay = Image.new( | |
"RGBA", | |
img.size, | |
(255, 255, 255, 0) | |
) | |
draw = ImageDraw.Draw(overlay) | |
text = text.upper() | |
font = ImageFont.truetype(font, font_size) | |
lines = text.split("\n") | |
px, py = 50, 20 #< -- padding left/right, up/down | |
line_spaces = 30 | |
# | text1 | | |
# |....text2....| | |
lboxs = [draw.textbbox((0, 0), line, font=font) for line in lines] | |
lw = [x[2] - x[0] for x in lboxs] | |
lh = [x[3] - x[1] for x in lboxs] | |
th = sum(lh) + (len(lines) - 1) * line_spaces | |
y = img.height - th - 50 # < --padding bottom | |
for i, line in enumerate(lines): | |
w, h = lw[i], lh[i] | |
lx = (img.width - w) // 2 | |
ly = y | |
spacing = 1.7 | |
lwh = sum(font.getlength(x) + spacing for x in line) - spacing | |
lht = font.getbbox(line)[3] - font.getbbox(line)[1] # < -- right - left | |
# background | |
bg_x1, bg_y1 = lx - px, ly - py | |
bg_x2, bg_y2 = lx + lwh + px, ly + lht + py | |
draw.rounded_rectangle( | |
[(bg_x1, bg_y1), (bg_x2, bg_y2)], | |
radius=15, | |
fill=(255, 255, 255, 180) # < -- 70% alpha, 255 * 0.7 = 178.5, i rounded to 180 | |
) | |
for char in line: | |
draw.text( | |
(lx, ly), | |
char, | |
font=font, | |
fill=(255, 255, 255, 255), | |
stroke_width=5, | |
stroke_fill=(0, 0, 0, 255), | |
) | |
lx += font.getlength(char) + 1.7 | |
y += h + line_spaces | |
img = Image.alpha_composite(img, overlay) | |
img.convert("RGB").save("oguri_stat.png", format="PNG") | |
print("ok") | |
return | |
def get_statistic() -> int: | |
stat = 0 | |
for f in os.listdir("frames"): | |
cache = f"frames/{f}/uploaded.txt" | |
if os.path.exists(cache): | |
cache = open(cache, "r").read().strip().splitlines() | |
if len(cache) > 0: | |
stat += len(cache) | |
return stat | |
if __name__ == "__main__": | |
stat = get_statistic() | |
text = f"{stat} FRAMES\nSUCCESSFULLY UPLOADED!" | |
print(text) | |
draw_stat( | |
img_path="oguri.png", | |
text=text, | |
font="public/fonts/HelveticaLTStd-Comp.otf", | |
font_size=108 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result will be different depending on the type of image your edit :P, my code works with image:
PNG 2940x1652 2940x1652+0+0 8-bit sRGB 3.79528MiB 0.001u 0:00.000
. feel free to adjust the padding and size yourself, or just follow my image info :).