56 lines
1.7 KiB
Python
56 lines
1.7 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, THUMB_WIDTH, THUMB_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.frame_factor = float(data['frame_factor']) if 'frame_factor' in data else 1.0
|
|
self.frame_original = Image.open("./data/frames/" + data['frame'])
|
|
self.frame = pil_image_to_surface(shrink_cover(self.frame_original, 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)
|