Fotobox2/Event.py
2025-06-27 22:42:20 +02:00

62 lines
1.9 KiB
Python

import json
from datetime import datetime
from pathlib import Path
from PIL import Image
from config import pil_image_to_surface, WIDTH, HEIGHT, SCREEN_RECT, DOMAIN, PHOTO_WIDTH, PHOTO_HEIGHT
class Event:
def __init__(self, data: json):
self.code = data['code']
self.title = data['title']
self.date = datetime.strptime(data['date'], '%Y-%m-%d')
self.password = data['password']
self.gallery = bool(data['gallery'])
self.frame_factor = float(data['frame_factor']) if 'frame_factor' in data else 1.0
self.frame_photo_sized = shrink_cover(Image.open("./data/frames/" + data['frame']), PHOTO_WIDTH, PHOTO_HEIGHT)
self.frame = pil_image_to_surface(shrink_cover(self.frame_photo_sized, int(WIDTH * self.frame_factor), int(HEIGHT * self.frame_factor)))
self.frame_rect = self.frame.get_rect(center=SCREEN_RECT.center)
self.url_without_protocol = "%s/e/%s" % (DOMAIN, self.code)
def load_event() -> Event | None:
try:
with Path("./data/event.json").open("r") as f:
data = json.load(f)
return Event(data)
except Exception as e:
print(e)
return None
def shrink_inside(img: Image, width: float, height: float):
if img.width <= width and img.height <= height:
return img
r = img.width / img.height
w = width
h = width / r
if h > height:
h = height
w = height * r
return img.resize((int(w), int(h)), Image.Resampling.LANCZOS)
def shrink_cover(img: Image, width: float, height: float):
if img.width <= width and img.height <= height:
return img
r = img.width / img.height
w = width
h = width / r
if h < height:
h = height
w = height * r
return img.resize((int(w), int(h)), Image.Resampling.LANCZOS)
def _resize2(img: Image, width: float):
height = width / img.width * img.height
return img.resize((int(width), int(height)), Image.Resampling.LANCZOS)