Fotobox2/Photo.py
2025-06-27 23:38:45 +02:00

37 lines
1.8 KiB
Python

from PIL import Image
from Event import Event, shrink_inside
from config import THUMB_WIDTH, pil_image_to_surface, SCREEN_RECT, BORDER, now, generate_code, HTTPS, DOMAIN, PHOTO_HEIGHT, WIDTH
class Photo:
def __init__(self, number: int, img: Image):
self.number = number
self.datetime = now()
self.code = generate_code(4)
self.urlWithoutProtocol = "%s/p/%s" % (DOMAIN, self.code)
self.urlWithProtocol = "http%s://%s" % ('s' if HTTPS else '', self.urlWithoutProtocol)
self.photo_image = img
self.framed_image = None
self.thumb_surface = None
self.thumb_rect = None
self.chosen_surface = None
self.chosen_rect = None
def prepare(self, event: Event):
self.framed_image = Image.new('RGB', (event.frame_photo_sized.width, event.frame_photo_sized.height))
pos = ((self.framed_image.width - self.photo_image.width) // 2, (self.framed_image.height - self.photo_image.height) // 2)
self.framed_image.paste(self.photo_image, pos, self.photo_image.convert('RGBA'))
self.framed_image.paste(event.frame_photo_sized, (0, 0), event.frame_photo_sized)
self.thumb_surface = pil_image_to_surface(shrink_inside(self.framed_image, THUMB_WIDTH, PHOTO_HEIGHT))
self.thumb_rect = self.thumb_surface.get_rect(centery=SCREEN_RECT.centery, left=(self.number - 1) * (THUMB_WIDTH + BORDER) + BORDER)
self.chosen_surface = pil_image_to_surface(shrink_inside(self.framed_image, THUMB_WIDTH, PHOTO_HEIGHT))
self.chosen_rect = self.chosen_surface.get_rect(right=WIDTH - 2 * BORDER, centery=SCREEN_RECT.centery)
def _resize(self, width: float):
height = width / self.photo_image.width * self.photo_image.height
return self.photo_image.resize((int(width), int(height)), Image.Resampling.LANCZOS)