46 lines
2.2 KiB
Python
46 lines
2.2 KiB
Python
from datetime import datetime
|
|
|
|
from PIL import Image
|
|
from pygame import Surface
|
|
|
|
from Event import Event, shrink_inside
|
|
from config import THUMB_WIDTH, pil_image_to_surface, SCREEN_RECT, BORDER, WIDTH, now, generate_code, HTTPS, DOMAIN, THUMB_HEIGHT
|
|
|
|
|
|
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.img = img
|
|
self.thumb = None
|
|
self.thumb_rect = None
|
|
self.chosen = None
|
|
self.chosen_rect = None
|
|
|
|
def prepare(self, event: Event):
|
|
frame = self._resize2(event.frame_original, THUMB_WIDTH)
|
|
photo = shrink_inside(self.img, frame.width / (event.frame_factor - 0.01), frame.height / (event.frame_factor - 0.01))
|
|
self.thumb = Surface((frame.width, frame.height))
|
|
self.thumb.blit(pil_image_to_surface(photo), ((frame.width - photo.width) / 2, (frame.height - photo.height) / 2))
|
|
self.thumb.blit(pil_image_to_surface(frame), (0, 0))
|
|
self.thumb_rect = self.thumb.get_rect(centery=SCREEN_RECT.centery, left=(self.number - 1) * (THUMB_WIDTH + BORDER) + BORDER)
|
|
|
|
frame_chosen = self._resize2(event.frame_original, THUMB_WIDTH)
|
|
photo_chosen = shrink_inside(self.img, frame_chosen.width / event.frame_factor, frame_chosen.height / event.frame_factor)
|
|
self.chosen = Surface((frame_chosen.width, frame_chosen.height))
|
|
self.chosen.blit(pil_image_to_surface(photo_chosen), ((frame_chosen.width - photo_chosen.width) / 2, (frame_chosen.height - photo_chosen.height) / 2))
|
|
self.chosen.blit(pil_image_to_surface(frame_chosen), (0, 0))
|
|
self.chosen_rect = self.chosen.get_rect(right=WIDTH - 2 * BORDER, centery=SCREEN_RECT.centery)
|
|
|
|
def _resize(self, width: float):
|
|
height = width / self.img.width * self.img.height
|
|
return self.img.resize((int(width), int(height)), Image.Resampling.LANCZOS)
|
|
|
|
def _resize2(self, img: Image, width: float):
|
|
height = width / img.width * img.height
|
|
return img.resize((int(width), int(height)), Image.Resampling.LANCZOS)
|