From 4eae5c08b8282a975e8c2877d36f68a025719f30 Mon Sep 17 00:00:00 2001 From: Hugo Date: Fri, 1 Sep 2023 13:44:12 +0200 Subject: [PATCH] Png poster maker --- .gitignore | 2 ++ image_poster.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 image_poster.py diff --git a/.gitignore b/.gitignore index 09d3036..cfa07cf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,7 @@ site/atom_blog.xml site/atom_events.xml poster.html poster.pdf +poster_light.png +poster_dark.png http.access.log http.error.log diff --git a/image_poster.py b/image_poster.py new file mode 100644 index 0000000..551e829 --- /dev/null +++ b/image_poster.py @@ -0,0 +1,89 @@ +#! /usr/bin/env python + +import freetype +import io +from PIL import Image, ImageDraw, ImageFont +import csv +import datetime as dt +from dateutil import relativedelta + +CURRENT_TIME = dt.date.today() +NEXT_MONTH = CURRENT_TIME +DAYS_OF_WEEK_SR = ("PON", "UTO", "SRE", "ČET", "PET", "SUB", "NED") +MONTHS_SR = ("Januar", "Februar", "Mart", "April", "Maj", "Jun", "Jul", "Avgust",\ + "Septembar", "Oktobar", "Novembar", "Decembar") + + +def load_events(csv_path:str) -> list[dict]: + events = [] + with open(csv_path) as csv_file: + csv_reader = csv.reader(csv_file) + next(csv_reader, None) + for event in csv_reader: + event_date = event[0] + event_date_parsed = dt.datetime.strptime(event_date, "%d-%m-%Y").date() + event_time = event[1] + event_title = event[3] + current_event = {"date":event_date_parsed, + "time":event_time, + "title":event_title.strip()} + if event_date_parsed >= NEXT_MONTH: + events.append(current_event) + return events + +def drawPoster(events, bg, fg): + fontFacade = ImageFont.truetype('./site/font/Facade-Sud.woff', size=110) + fontIosevka = ImageFont.truetype('./site/font/iosevka-regular.woff', size=60) + fontIosevkaSmall = ImageFont.truetype('./site/font/iosevka-regular.woff', size=45) + + W = 1200 + H = 1500 + img = Image.new('RGB', (W, H), bg) + draw = ImageDraw.Draw(img) + + header = "DECENTRALA" + _, _, w, _ = draw.textbbox((0, 0), header, font=fontFacade) + draw.text(((W-w)/2, 120), header, font=fontFacade, fill=fg) + + subheader = f"Plan za {MONTHS_SR[NEXT_MONTH.month - 1]}" + _, _, w, _ = draw.textbbox((0, 0), subheader, font=fontIosevka) + draw.text(((W-w)/2, 240), subheader, font=fontIosevka, fill=fg) + + height = 410 + + for event in events: + date = DAYS_OF_WEEK_SR[event["date"].weekday()] + day = event["date"].day + title = event["title"] + pad = " " if event["date"].day < 10 else "" + eventText = f"{date} {day}. {pad} {title}" + draw.text((120, height), eventText, font=fontIosevkaSmall, fill=fg) + height += 70 + + def drawCircle(x, y): + r = 10 + draw.ellipse((x - r, y - r, x + r, y+r), fill=fg, outline=(0, 0, 0), width=0) + + LCX = 950 # logo center x + LCY = 1200 # logo center y + d = 50 # delta + drawCircle(LCX - d, LCY) + drawCircle(LCX, LCY) + drawCircle(LCX, LCY - d) + drawCircle(LCX, LCY + d) + drawCircle(LCX + d, LCY) + + draw.line([(LCX - d, LCY), (LCX + d, LCY)], fill=fg, width=5, joint=None) + draw.line([(LCX, LCY), (LCX, LCY + d), (LCX + d, LCY), (LCX, LCY - d)], fill=fg, width=5, joint=None) + draw.text((LCX - 1.7*d, LCY + 1.5*d), "dmz.rs", font=fontIosevka, fill=fg) + + return img + + +events = load_events("dogadjaji.csv") + +img = drawPoster(events, (0, 0, 0), (20, 250, 50)) +img.save('poster_dark.png') + +img = drawPoster(events, (255, 255, 255), (0, 0, 0)) +img.save('poster_light.png') \ No newline at end of file