From 1f0d1692b56f3868ef2738bd662d86c34f3c16fa Mon Sep 17 00:00:00 2001 From: root Date: Sun, 11 Feb 2018 15:01:58 +0100 Subject: [PATCH] Pfad von persistenten Fotos ins lokale Verzeichnis verschoben + Laden von statischen Grafiken in ein Dictionary --- .gitignore | 1 + photobox.py | 103 +++++++++++++++++++++++++--------------------------- 2 files changed, 51 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index d1c0eb3..35efce1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +/images/ /tmp?.jpg diff --git a/photobox.py b/photobox.py index 06f9155..7d123a0 100755 --- a/photobox.py +++ b/photobox.py @@ -55,8 +55,8 @@ TEXT_POS = (250, -40) OVERLAY_ALPHA = 64 COLOR_BACK = pygame.Color(0, 0, 0) COLOR_FLASH = pygame.Color(255, 255, 255) -GRAPHICS_DIR = "./graphics" -IMAGE_DIR = "/var/www/html/current_images" +GRAPHICS_DIR = "graphics" +IMAGE_DIR = "images/0-CURRENT" COUNTDOWN = 3 SHOT_COUNT = 3 BORDER = 10 @@ -64,6 +64,9 @@ BORDER = 10 THUMB_WIDTH = int((800 - BORDER) / SHOT_COUNT - BORDER) THUMB_HEIGHT = int(THUMB_WIDTH / 1.333) +IMAGES_ORIGINAL = os.path.join(IMAGE_DIR, "original") +IMAGES_TODO = os.path.join(IMAGE_DIR, "todo") + # global variables @@ -73,22 +76,12 @@ session_start = monotonic_time() # CREATE DIRECTORIES try: - os.makedirs(GRAPHICS_DIR) + os.makedirs(IMAGES_ORIGINAL) except OSError: pass try: - os.makedirs(IMAGE_DIR) -except OSError: - pass - -try: - os.makedirs(IMAGE_DIR + "/original") -except OSError: - pass - -try: - os.makedirs(IMAGE_DIR + "/todo") + os.makedirs(IMAGES_TODO) except OSError: pass @@ -121,32 +114,26 @@ screen = pygame.display.set_mode((WIDTH,HEIGHT)) -# LOAD OVERLAYS -begin = pygame.image.load(GRAPHICS_DIR + "/begin.png") -begin_rect = begin.get_rect() +# LOAD GRAPHICS +gfx = {} -loading = pygame.image.load(GRAPHICS_DIR + "/loading.png") -loading_rect = loading.get_rect() +def _load(name): + filename = "%s.png" % name + path = os.path.join(GRAPHICS_DIR, filename) + + img = pygame.image.load(path) + rect = img.get_rect() + + gfx[name] = (img, rect) -yes = pygame.image.load(GRAPHICS_DIR + "/yes.png") -yes_rect = yes.get_rect() -yes_rect.y = 340 - -no = pygame.image.load(GRAPHICS_DIR + "/no.png") -no_rect = no.get_rect() -no_rect.y = 340 - -end_yes = pygame.image.load(GRAPHICS_DIR + "/end_yes.png") -end_yes_rect = end_yes.get_rect() - -end_no = pygame.image.load(GRAPHICS_DIR + "/end_no.png") -end_no_rect = end_no.get_rect() - -saved = pygame.image.load(GRAPHICS_DIR + "/saved.png") -saved_rect = saved.get_rect() - -canceled = pygame.image.load(GRAPHICS_DIR + "/canceled.png") -canceled_rect = canceled.get_rect() +_load("begin") +_load("loading") +_load("yes") +_load("no") +_load("end_yes") +_load("end_no") +_load("saved") +_load("canceled") @@ -154,7 +141,10 @@ canceled_rect = canceled.get_rect() img_cdn = [] pad_cdn = [] for i in range(0, COUNTDOWN): - img = Image.open(GRAPHICS_DIR + "/%s.png" % (COUNTDOWN - i)) + filename = "%s.png" % (COUNTDOWN - i) + path = os.path.join(GRAPHICS_DIR, filename) + + img = Image.open(path) pad = Image.new('RGB', ( ((img.size[0] + 31) // 32) * 32, ((img.size[1] + 15) // 16) * 16, @@ -199,20 +189,19 @@ def maintainance(): # wait pos = waitForTouch() - def waitForBegin(): screen.fill(COLOR_BACK) - screen.blit(begin, begin_rect) + screen.blit(*gfx["begin"]) pygame.display.flip() # wait pos = waitForTouch() if pos[0] > 720 and pos[1] > 400: maintainance() - + def countdown(camera, pad, img): @@ -267,7 +256,7 @@ def makePhotos(): def chooseImages(): # clear screen screen.fill(COLOR_BACK) - screen.blit(loading, loading_rect) + screen.blit(*gfx["loading"]) pygame.display.update() # load images from disk @@ -299,9 +288,9 @@ def chooseImages(): # draw background screen.fill(COLOR_BACK) if any_choice: - screen.blit(end_yes, end_yes_rect) + screen.blit(*gfx["end_yes"]) else: - screen.blit(end_no, end_no_rect) + screen.blit(*gfx["end_no"]) # draw images for i in range(0, SHOT_COUNT): @@ -311,11 +300,13 @@ def chooseImages(): x = 12 for choice in choices: if choice > 0: - yes_rect.x = x + (THUMB_WIDTH - yes_rect.width) / 2 - screen.blit(yes, yes_rect) + (img, rect) = gfx["yes"] else: - no_rect.x = x + (THUMB_WIDTH - no_rect.width) / 2 - screen.blit(no, no_rect) + (img, rect) = gfx["no"] + + rect.x = x + (THUMB_WIDTH - rect.width) / 2 + rect.y = 340 + screen.blit(img, rect) x = x + THUMB_WIDTH + BORDER @@ -355,9 +346,15 @@ def saveImages(choices): for i in range(0, SHOT_COUNT): if choices[i] > 0: print(" saving image #%s" % i) + filename = "%s-%s-session_age=%05d.jpg" % (local.strftime("%Y%m%dT%H:%M:%S.%f%z"), i, session_age) - os.rename("tmp%s.jpg" % i, IMAGE_DIR + "/original/" + filename) - copyfile(IMAGE_DIR + "/original/" + filename, IMAGE_DIR + "/todo/" + filename) + + path_tmp = "tmp%s.jpg" % i + path_original = os.path.join(IMAGES_ORIGINAL, filename) + path_todo = os.path.join(IMAGES_TODO, filename) + + os.rename(path_tmp, path_original) + copyfile(path_original, path_todo) @@ -369,11 +366,11 @@ while 1: if save: saveImages(choices); screen.fill(COLOR_BACK) - screen.blit(saved, saved_rect) + screen.blit(*gfx["saved"]) pygame.display.flip() else: screen.fill(COLOR_BACK) - screen.blit(canceled, canceled_rect) + screen.blit(*gfx["canceled"]) pygame.display.flip() sleep(2)