website2/image_poster.py

152 lines
5.3 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
import argparse
2023-09-01 11:44:12 +00:00
import freetype
import io
from PIL import Image, ImageDraw, ImageFont
import csv
import datetime as dt
from dateutil import relativedelta
2024-06-01 15:02:14 +00:00
from cairosvg import svg2png
2023-09-01 11:44:12 +00:00
CURRENT_TIME = dt.date.today()
NEXT_MONTH = CURRENT_TIME + relativedelta.relativedelta(months=1, day=1)
2023-09-01 11:44:12 +00:00
DAYS_OF_WEEK_SR = ("PON", "UTO", "SRE", "ČET", "PET", "SUB", "NED")
MONTHS_SR = ("Januar", "Februar", "Mart", "April", "Maj", "Jun", "Jul", "Avgust",\
2023-09-27 15:40:11 +00:00
"Septembar", "Oktobar", "Novembar", "Decembar")
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
def parseArgs(parser):
"""
Parse all arguments and return the list of argument values
"""
parser.add_argument("month", metavar = "MM", help = "two digit number representing the month for which to generate poster", default = "empty", nargs = "?")
return parser.parse_args()
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
def load_events(csv_path:str, month:int) -> list[dict]:
monthafter = month + relativedelta.relativedelta(months=1, day=1)
2023-09-01 11:44:12 +00:00
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 >= month and event_date_parsed < monthafter:
2023-09-01 11:44:12 +00:00
events.append(current_event)
return events
2023-09-27 15:40:11 +00:00
def drawPoster(events, bg, fg, month:int):
2024-06-01 13:16:21 +00:00
fontFacade = ImageFont.truetype('./site/font/Facade-Sud.woff', size=365)
fontIosevka = ImageFont.truetype('./site/font/iosevka-regular.woff', size=200)
fontIosevkaSmall = ImageFont.truetype('./site/font/iosevka-regular.woff', size=150)
2023-09-01 11:44:12 +00:00
2024-06-01 13:16:21 +00:00
W = 3508
H = 4960
2023-09-01 11:44:12 +00:00
img = Image.new('RGB', (W, H), bg)
draw = ImageDraw.Draw(img)
header = "DECENTRALA"
_, _, w, _ = draw.textbbox((0, 0), header, font=fontFacade)
2024-06-01 13:16:21 +00:00
draw.text(((W-w)/2, 165), header, font=fontFacade, fill=fg)
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
subheader = f"Plan za {MONTHS_SR[month.month - 1]}"
2023-09-01 11:44:12 +00:00
_, _, w, _ = draw.textbbox((0, 0), subheader, font=fontIosevka)
2024-06-01 13:16:21 +00:00
draw.text(((W-w)/2, 560), subheader, font=fontIosevka, fill=fg)
2023-09-01 11:44:12 +00:00
2024-06-01 13:16:21 +00:00
height = 990
2023-09-01 11:44:12 +00:00
2024-06-01 13:16:21 +00:00
draw.text((165, height), "Radionice pocinju u 19h u DC Krovu", font=fontIosevkaSmall, fill=fg)
height += 200
2024-06-01 13:16:21 +00:00
draw.text((165, height), "Svi dogadjaji su uvek besplatni", font=fontIosevkaSmall, fill=fg)
height += 300
# Write list of events to sperate text file as well
textfile=open("poster.txt","w")
2023-12-29 19:44:23 +00:00
textfile.write(f"Plan za {MONTHS_SR[month.month - 1]}\n\n")
textfile.write("Radionice pocinju u 19h u DC Krovu\n")
2023-12-29 19:44:23 +00:00
textfile.write("Svi dogadjaji su uvek besplatni\n\n")
# Loop to write events both to poster image and text file
2023-09-01 11:44:12 +00:00
for event in events:
# Add event to image poster
2023-09-01 11:44:12 +00:00
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}"
2024-06-01 13:16:21 +00:00
draw.text((165, height), eventText, font=fontIosevkaSmall, fill=fg)
height += 200
2023-09-01 11:44:12 +00:00
# Add event to textfile
textfile.write(eventText + "\n")
textfile.close()
2023-09-01 11:44:12 +00:00
def drawCircle(x, y):
2024-06-01 13:16:21 +00:00
r = 50
2023-09-01 11:44:12 +00:00
draw.ellipse((x - r, y - r, x + r, y+r), fill=fg, outline=(0, 0, 0), width=0)
2024-06-01 15:02:14 +00:00
LCX = 415 # logo center x
2024-06-01 13:16:21 +00:00
LCY = 4350 # logo center y
d = 190 # delta
2023-09-01 11:44:12 +00:00
drawCircle(LCX - d, LCY)
drawCircle(LCX, LCY)
drawCircle(LCX, LCY - d)
drawCircle(LCX, LCY + d)
drawCircle(LCX + d, LCY)
2024-06-01 13:16:21 +00:00
draw.line([(LCX - d, LCY), (LCX + d, LCY)], fill=fg, width=20, joint=None)
draw.line([(LCX, LCY), (LCX, LCY + d), (LCX + d, LCY), (LCX, LCY - d)], fill=fg, width=20, joint=None)
2023-09-01 11:44:12 +00:00
draw.text((LCX - 1.7*d, LCY + 1.5*d), "dmz.rs", font=fontIosevka, fill=fg)
2024-06-01 15:18:36 +00:00
mesh_svg = svg2png(url='site/img/mesh-light.svg')
2024-06-01 15:02:14 +00:00
mesh_svg_bytes = io.BytesIO(mesh_svg)
mesh_img = Image.open(mesh_svg_bytes)
2024-06-01 15:18:36 +00:00
if bg == (0,0,0):
pixdata = mesh_img.load()
for y in range(mesh_img.size[1]):
for x in range(mesh_img.size[0]):
if pixdata[x,y] != (0,0,0,0):
pixdata[x, y] = (0, 100, 0, 255)
2024-06-01 15:02:14 +00:00
mesh_img = mesh_img.resize((W,H))
mesh_img.thumbnail((W,H), Image.Resampling.LANCZOS)
mesh_w, mesh_h = mesh_img.size
mesh_position = (W - mesh_w, H - mesh_h)
img.paste(mesh_img, mesh_position, mesh_img)
2023-09-01 11:44:12 +00:00
return img
2023-09-27 15:40:11 +00:00
def main():
# Parse arguments
parser = argparse.ArgumentParser(description="Generate images of the poster")
args = parseArgs(parser)
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
# Set month based on user input
month = NEXT_MONTH
if args.month.isdigit():
month = dt.date(CURRENT_TIME.year, int(args.month), 1)
elif args.month != "empty":
print("Month has to be specified as a number. I will use next month as the default")
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
# Load events and draw a poseter
events = load_events("dogadjaji.csv", month)
2023-09-01 11:44:12 +00:00
2023-09-27 15:40:11 +00:00
img = drawPoster(events, (0, 0, 0), (20, 250, 50), month)
img.save('poster_dark.png')
img = drawPoster(events, (255, 255, 255), (0, 0, 0), month)
img.save('poster_light.png')
if __name__ == "__main__":
main()