From 36da96c8e016057cab5cf48787e9f78e8b8b6887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Fri, 27 Jun 2025 17:47:51 +0200 Subject: [PATCH] frames --- .gitignore | 1 + Event.py | 11 ++++++++--- Fotobox.py | 56 +++++++++++++++++++++++++++--------------------------- Photo.py | 41 ++++++++++++++++----------------------- config.py | 3 +++ 5 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5fac628 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/data/ \ No newline at end of file diff --git a/Event.py b/Event.py index 6986b61..eb62265 100644 --- a/Event.py +++ b/Event.py @@ -4,7 +4,7 @@ from pathlib import Path from PIL import Image -from config import pil_image_to_surface, WIDTH, HEIGHT, SCREEN_RECT, DOMAIN, THUMB_WIDTH, THUMB_HEIGHT +from config import pil_image_to_surface, WIDTH, HEIGHT, SCREEN_RECT, DOMAIN, PHOTO_WIDTH, PHOTO_HEIGHT class Event: @@ -15,8 +15,8 @@ class Event: 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_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) @@ -53,3 +53,8 @@ def shrink_cover(img: Image, width: float, height: float): 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) diff --git a/Fotobox.py b/Fotobox.py index e25cd44..377d782 100644 --- a/Fotobox.py +++ b/Fotobox.py @@ -163,7 +163,7 @@ class Fotobox: if self._choice is not None: screen.blit(SAVE_SURFACE, SAVE_RECT) for photo in self._photos: - screen.blit(photo.thumb, photo.thumb_rect) + screen.blit(photo.thumb_surface, photo.thumb_rect) if self._choice == photo: rect = pygame.Rect( photo.thumb_rect.x - CHOICE_BORDER, @@ -174,7 +174,7 @@ class Fotobox: pygame.draw.rect(screen, (0, 255, 0), rect, width=CHOICE_BORDER) elif self._state == State.QR: screen.blit(self._qr, self._qr_rect) - screen.blit(self._choice.chosen, self._choice.chosen_rect) + screen.blit(self._choice.chosen_surface, self._choice.chosen_rect) if self._printer is not None and not self._printed: screen.blit(PRINT_SURFACE, PRINT_RECT) screen.blit(DONE_SURFACE, DONE_RECT) @@ -214,30 +214,30 @@ class Fotobox: self._qr = qr_create(self._choice.urlWithProtocol) self._qr_rect = self._qr.get_rect(left=2 * BORDER, centery=SCREEN_RECT.centery) - filename = "F-%s---R%04d-%s---S%04d-%s---P%04d-%s---%s.jpg" % ( - self._fotobox_uuid, - self._runtime_number, - iso(self._runtime_datetime), - self._shooting_number, - iso(self._shooting_datetime), - self._choice.number, - iso(self._choice.datetime), - self._choice.code, - ) + self.save(self._choice.photo_image, "") + self.save(self._choice.framed_image, "framed") - original = Path("./data/photos/original/F-%s/R%04d-%s/%s" % ( - self._fotobox_uuid, - - self._runtime_number, - iso(self._runtime_datetime), - - filename, - )) - original.parent.mkdir(parents=True, exist_ok=True) - self._choice.img.save(original, format='JPEG', quality=95) - - upload = Path("./data/photos/upload/%s" % (filename,)) - upload.parent.mkdir(parents=True, exist_ok=True) - os.link(original, upload) - - print("Photo saved: %s" % original.absolute()) + def save(self, img: Image, suffix: str): + filename = "F-%s---R%04d-%s---S%04d-%s---P%04d-%s---%s%s.jpg" % ( + self._fotobox_uuid, + self._runtime_number, + iso(self._runtime_datetime), + self._shooting_number, + iso(self._shooting_datetime), + self._choice.number, + iso(self._choice.datetime), + self._choice.code, + "---" + suffix if suffix != "" else "" + ) + path = Path("./data/photos/original/F-%s/R%04d-%s/%s" % ( + self._fotobox_uuid, + self._runtime_number, + iso(self._runtime_datetime), + filename, + )) + path.parent.mkdir(parents=True, exist_ok=True) + img.save(path, format='JPEG', quality=95) + upload = Path("./data/photos/upload/%s" % (filename,)) + upload.parent.mkdir(parents=True, exist_ok=True) + os.link(path, upload) + print("Photo saved: %s" % path.absolute()) diff --git a/Photo.py b/Photo.py index 25e7857..f27f018 100644 --- a/Photo.py +++ b/Photo.py @@ -1,10 +1,7 @@ -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 +from config import THUMB_WIDTH, pil_image_to_surface, SCREEN_RECT, BORDER, now, generate_code, HTTPS, DOMAIN, PHOTO_HEIGHT class Photo: @@ -15,31 +12,25 @@ class Photo: 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.photo_image = img + self.framed_image = None + self.thumb_surface = None self.thumb_rect = None - self.chosen = None + self.chosen_surface = 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) + 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) - 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) + 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.thumb_surface.get_rect(centery=SCREEN_RECT.centery, left=(self.number - 1) * (THUMB_WIDTH + BORDER) + BORDER) 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) + height = width / self.photo_image.width * self.photo_image.height + return self.photo_image.resize((int(width), int(height)), Image.Resampling.LANCZOS) diff --git a/config.py b/config.py index 89154fd..366fa92 100644 --- a/config.py +++ b/config.py @@ -15,6 +15,9 @@ WIDTH: int = 800 HEIGHT: int = 480 BORDER: int = 10 +PHOTO_WIDTH = 2592 +PHOTO_HEIGHT = 1944 + SHOOTING_COUNT: int = 2 SHOOTING_INTERVAL: float = 0 if DEBUG else 1.75 COUNTDOWN_COUNT: int = 1 if DEBUG else 3