ich habe vor mit meinem Raspberry und einer DSLR eine kleine Fotobox zu bauen. Dazu sollen auf gewisse Trigger eine Slideshow ablaufen (3...2...1...Lächeln) -> Bildaufnhame -> Bild anzeigen -> wieder von vorne.
Was soll ich sagen: Es funktioniert auch!
Den Code habe ich mir aus den diversen Docs und Foren zusammengesucht, vor Python habe ich vor langer Zeit recht viel mit PHP gemacht, sonst habe ich gar nichts mehr mit der Materie zu tun. Ich habe das Gefühl vor allem Pygame massiv zu "miss"-brauchen - daher meine Frage: Kann mal jemand meinen Code begutachten und eventuell ein paar Rückmeldungen geben? Gerne natürlich auch, wenn was ganz gut ist
Hier einmal leserlicher im Pastebin:
http://pastebin.com/ktPmGnMT
Und hier für Leute, die nicht nach Extern wollen :
Code: Alles auswählen
#!/usr/bin/env python
import pygame
import os
import sys
import re
import piggyphoto
from glob import glob
from pygame.locals import *
## vars
(B, H) = (1280, 800)
BLACK = (0, 0, 0)
XY = [0, 0]
## thumbail size
thx = 714
thy = 474
## image settings
ImgFolder = "images/"
ImgPre = "IMG_"
## initialize surface, set caption, mouse invisible
pygame.init()
surf = pygame.display.set_mode((B, H), 0, 32)
pygame.display.set_caption('Photo.Box v1.0')
pygame.mouse.set_visible(False)
## fill bg + start image
surf.fill(BLACK)
start = pygame.image.load("start.png").convert_alpha()
surf.blit(start, XY)
pygame.display.update()
## toogle fullscreen
def ToggleScreen():
pygame.display.toggle_fullscreen()
## function to number images taken
def ImgCount(Folder):
if not os.path.exists(Folder):
os.makedirs(Folder, 0777)
if os.path.isfile(Folder + ImgPre + '0001.jpg'):
files = []
for f in glob(os.path.join(Folder, "*.jpg")):
files.append(os.path.splitext(os.path.basename(f))[0])
files = sorted(files)
lastpic = files[-1]
lastpic = re.findall(r'\d+', lastpic)
lastpic = int(''.join(lastpic))
Count = lastpic + 1
return str(Count).zfill(4)
else:
Count = str(1).zfill(4)
return Count
## main func
def PhotoBooth():
## screensize
screen = pygame.display.Info()
w = screen.current_w
h = screen.current_h
## vars
global xy, start, thx, thy, ImgFolder, ImgPre
bg1 = pygame.image.load("1.png").convert_alpha()
bg2 = pygame.image.load("2.png").convert_alpha()
bg3 = pygame.image.load("3.png").convert_alpha()
smile = pygame.image.load("smile.png").convert_alpha()
proc = pygame.image.load("proc.png").convert_alpha()
Count = ImgCount(ImgFolder)
Filename = ImgFolder + ImgPre + Count + ".jpg"
cam = piggyphoto.camera()
## Sequence1
surf.fill(BLACK)
surf.blit(bg1, XY)
pygame.display.update()
pygame.time.delay(500)
surf.fill(BLACK)
pygame.display.update()
pygame.time.delay(500)
## Sequence2
surf.blit(bg2, XY)
pygame.display.update()
pygame.time.delay(500)
surf.fill(BLACK)
pygame.display.update()
pygame.time.delay(500)
## Sequence3
surf.blit(bg3, XY)
pygame.display.update()
pygame.time.delay(500)
surf.blit(smile, XY)
pygame.display.update()
pygame.time.delay(50)
## Take picture
cam.capture_image(Filename)
## create thumbnail
##th = Image.open(Filename)
##th = th.resize((thx, thy), Image.ANTIALIAS)
##th = th.save(thumb)
## wait while your image is processed
surf.blit(proc, XY)
pygame.display.update()
pygame.time.delay(500)
## display taken image
photo = pygame.image.load(Filename).convert_alpha()
photo = pygame.transform.scale(photo, (w, h))
surf.blit(photo, XY)
pygame.display.update()
pygame.time.delay(5000)
surf.fill(BLACK)
pygame.time.delay(500)
## back to start
surf.blit(start, XY)
pygame.display.update()
## loop
while 1:
event = pygame.event.wait()
if event.type == KEYDOWN:
if event.key == pygame.K_ESCAPE:
ToggleScreen()
if event.type == KEYDOWN:
if event.key == pygame.K_SPACE:
PhotoBooth()
if event.type == pygame.MOUSEBUTTONDOWN and event.button==3:
PhotoBooth()
if event.type == KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.time.delay(50)
Grüße,
Chris