215 lines
6.4 KiB
Python
Executable File
215 lines
6.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os, sys, time, pygame
|
|
|
|
# import picamera
|
|
|
|
class Button:
|
|
def __init__(self, size, topleft):
|
|
self.up_image = pygame.image.load('button-up.png').convert_alpha()
|
|
self.up_image = pygame.transform.smoothscale(self.up_image, (size, size))
|
|
|
|
self.down_image = pygame.image.load('button-down.png').convert_alpha()
|
|
self.down_image = pygame.transform.smoothscale(self.down_image, (size, size))
|
|
|
|
self.rect = self.up_image.get_rect()
|
|
self.rect.topleft = topleft
|
|
|
|
self.pressed = False
|
|
|
|
def draw_on(self, screen):
|
|
if self.pressed:
|
|
screen.blit(self.down_image, self.rect)
|
|
else:
|
|
screen.blit(self.up_image, self.rect)
|
|
|
|
def is_pressed(self, pos):
|
|
return self.rect.collidepoint(pos)
|
|
|
|
def press(self):
|
|
self.pressed = True
|
|
|
|
def unpress(self):
|
|
self.pressed = False
|
|
self.run()
|
|
|
|
def run(self):
|
|
pass
|
|
|
|
class CaptureButton(Button):
|
|
countdown = 3
|
|
|
|
def __init__(self, size, topleft, photoboite):
|
|
super(CaptureButton, self).__init__(size, topleft)
|
|
self.photoboite = photoboite
|
|
self.elapsed = 0
|
|
self.capture_mode = False
|
|
self.count = 0
|
|
|
|
def draw_on(self, screen):
|
|
super(CaptureButton, self).draw_on(screen)
|
|
if self.capture_mode:
|
|
if self.elapsed >= CaptureButton.countdown:
|
|
text = "CLIC!"
|
|
else:
|
|
text = str(CaptureButton.countdown - self.elapsed)
|
|
size = self.photoboite.font.size(text)
|
|
ren = self.photoboite.font.render(text, True, (0, 0, 0))
|
|
topleft = (self.rect.topleft[0] + self.rect.width / 2 - size[0] / 2, self.rect.topleft[1] + self.rect.height / 2 - size[1] / 2)
|
|
screen.blit(ren, topleft)
|
|
|
|
def run(self):
|
|
pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
|
self.capture_mode = True
|
|
|
|
capture = Capture(self.count)
|
|
self.count += 1
|
|
|
|
for i in range(4):
|
|
wait = True
|
|
start = time.time()
|
|
while wait:
|
|
self.elapsed = int(time.time() - start)
|
|
if self.elapsed >= CaptureButton.countdown:
|
|
wait = False
|
|
|
|
self.photoboite.event()
|
|
self.photoboite.draw()
|
|
pygame.display.flip()
|
|
|
|
capture.take(self.photoboite.camera)
|
|
pygame.time.wait(1000)
|
|
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
|
self.capture_mode = False
|
|
|
|
|
|
class Capture:
|
|
def __init__(self, uid):
|
|
self.uid = uid
|
|
self.photos = []
|
|
|
|
def take(self, camera):
|
|
self.photos.append(Photo(self.uid, len(self.photos)).take(camera))
|
|
|
|
|
|
class Photo:
|
|
def __init__(self, cid, uid):
|
|
self.cid = cid
|
|
self.uid = uid
|
|
|
|
def take(self, camera):
|
|
self.name = "%s-%04d-%04d" % (time.strftime('%Y%m%d-%H%M%S'), self.cid, self.uid)
|
|
camera.take(self.name)
|
|
|
|
return self
|
|
|
|
|
|
class Camera:
|
|
def __init__(self, rect):
|
|
self.rect = rect
|
|
self.img = pygame.transform.scale(pygame.image.load('tournesol.jpg'), (self.rect.width, self.rect.height))
|
|
# self.camera = picamera.PiCamera()
|
|
# self.camera.led = False
|
|
# self.camera.resolution = (self.rect.width, self.rect.height)
|
|
|
|
def take(self, name):
|
|
# self.camera.capture(name + '.jpg')
|
|
print('Take picture! ' + name)
|
|
|
|
def stream(self):
|
|
return self.img
|
|
# stream = io.BytesIO()
|
|
# camera.capture(stream, use_video_port=True, format='rgb', rezise=(self.rect.width, self.rect.height))
|
|
# img = pygame.image.frombuffer(stream, (self.rect.width, self.rect.height), 'RGB')
|
|
# stream.close()
|
|
|
|
# return img
|
|
|
|
def draw_on(self, screen):
|
|
screen.blit(self.stream(), self.rect)
|
|
|
|
|
|
class Photoboite:
|
|
def __init__(self):
|
|
self.screen = self.screen()
|
|
pygame.font.init()
|
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
self.background = 255, 255, 255
|
|
self.buttons = []
|
|
|
|
button_size = int(self.screen.get_width() / 4)
|
|
topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2
|
|
|
|
self.buttons.append(CaptureButton(button_size, topleft, self))
|
|
|
|
camera_rect = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40)
|
|
self.camera = Camera(camera_rect)
|
|
|
|
self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180)
|
|
|
|
self.clock = pygame.time.Clock()
|
|
|
|
|
|
def screen(self):
|
|
if os.getenv('DISPLAY'):
|
|
pygame.display.init()
|
|
display = pygame.display.Info()
|
|
return pygame.display.set_mode((min(1280,display.current_w), min(720,display.current_h)))
|
|
else:
|
|
drivers = ['directfb', 'fbcon', 'svgalib']
|
|
found = False
|
|
for driver in drivers:
|
|
if not os.getenv('SDL_VIDEODRIVER'):
|
|
os.putenv('SDL_VIDEODRIVER', driver)
|
|
try:
|
|
pygame.display.init()
|
|
except pygame.error as e:
|
|
print('Driver: {0} failed.'.format(driver))
|
|
continue
|
|
found = True
|
|
break
|
|
|
|
if not found:
|
|
raise Exception('No suitable video driver found!')
|
|
|
|
return pygame.display.set_mode()
|
|
|
|
def draw(self):
|
|
self.clock.tick(60)
|
|
|
|
self.screen.fill(self.background)
|
|
|
|
self.camera.draw_on(self.screen)
|
|
|
|
for button in self.buttons:
|
|
button.draw_on(self.screen)
|
|
|
|
def event(self):
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
|
|
self.quit()
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
|
for button in self.buttons:
|
|
if button.is_pressed(event.pos):
|
|
button.press()
|
|
elif event.type == pygame.MOUSEBUTTONUP:
|
|
for button in self.buttons:
|
|
if button.is_pressed(event.pos):
|
|
button.unpress()
|
|
|
|
def run(self):
|
|
pressed = False
|
|
while True:
|
|
self.event()
|
|
self.draw()
|
|
|
|
pygame.display.flip()
|
|
|
|
def quit(self):
|
|
# self.camera.close()
|
|
sys.exit()
|
|
|
|
Photoboite().run()
|