2017-09-28 23:16:08 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-30 21:49:20 +02:00
|
|
|
import io, os, sys, time, pygame
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-10-02 10:59:49 +02:00
|
|
|
try:
|
|
|
|
import picamera
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.rgb = bytearray(self.rect.width * self.rect.height * 4)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.camera.close()
|
|
|
|
|
|
|
|
def take(self, name):
|
|
|
|
self.camera.capture(name + '.jpg')
|
|
|
|
print('Take picture! ' + name)
|
|
|
|
|
|
|
|
def stream(self):
|
|
|
|
stream = io.BytesIO()
|
|
|
|
self.camera.capture(stream, use_video_port=True, format='rgba', resize=(self.rect.width, self.rect.height))
|
|
|
|
stream.seek(0)
|
|
|
|
stream.readinto(self.rgb)
|
|
|
|
stream.close()
|
|
|
|
img = pygame.image.frombuffer(self.rgb, (self.rect.width, self.rect.height), 'RGBA')
|
|
|
|
|
|
|
|
return pygame.transform.flip(img, True, False)
|
|
|
|
|
|
|
|
def draw_on(self, screen):
|
|
|
|
screen.blit(self.stream(), self.rect)
|
|
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
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))
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def take(self, name):
|
|
|
|
print('Take picture! ' + name)
|
2017-10-04 00:33:23 +02:00
|
|
|
return self.img
|
2017-10-02 10:59:49 +02:00
|
|
|
|
|
|
|
def draw_on(self, screen):
|
|
|
|
screen.blit(self.img, self.rect)
|
|
|
|
|
|
|
|
|
2017-09-29 11:27:48 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
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))
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.down_image = pygame.image.load('button-down.png').convert_alpha()
|
|
|
|
self.down_image = pygame.transform.smoothscale(self.down_image, (size, size))
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.rect = self.up_image.get_rect()
|
|
|
|
self.rect.topleft = topleft
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.pressed = False
|
2017-10-02 09:48:20 +02:00
|
|
|
self.enabled = True
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def draw_on(self, screen):
|
|
|
|
if self.pressed:
|
|
|
|
screen.blit(self.down_image, self.rect)
|
|
|
|
else:
|
|
|
|
screen.blit(self.up_image, self.rect)
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def is_pressed(self, pos):
|
|
|
|
return self.rect.collidepoint(pos)
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def press(self):
|
|
|
|
self.pressed = True
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def unpress(self):
|
|
|
|
self.pressed = False
|
2017-09-28 13:56:32 +02:00
|
|
|
self.run()
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
2017-09-29 12:35:52 +02:00
|
|
|
pass
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
class CaptureButton(Button):
|
2017-09-28 16:39:22 +02:00
|
|
|
countdown = 3
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
def __init__(self, size, topleft, photoboite):
|
|
|
|
super(CaptureButton, self).__init__(size, topleft)
|
|
|
|
self.photoboite = photoboite
|
|
|
|
self.elapsed = 0
|
2017-09-28 16:39:22 +02:00
|
|
|
self.capture_mode = False
|
|
|
|
self.count = 0
|
2017-09-28 13:56:32 +02:00
|
|
|
|
2017-10-02 10:10:26 +02:00
|
|
|
self.background_rect = pygame.Rect(0, self.photoboite.screen.get_height() - 80, self.photoboite.screen.get_width(), 80)
|
|
|
|
self.background = pygame.Surface((self.background_rect.width, self.background_rect.height))
|
|
|
|
self.background.fill((0, 0, 0))
|
|
|
|
self.background.set_alpha(150)
|
|
|
|
|
|
|
|
self.font = pygame.font.Font(None, 80)
|
|
|
|
|
2017-10-04 00:33:23 +02:00
|
|
|
self.photo_film = pygame.image.load('film.png').convert_alpha()
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
def draw_on(self, screen):
|
2017-10-02 09:48:20 +02:00
|
|
|
if self.enabled:
|
|
|
|
super(CaptureButton, self).draw_on(screen)
|
2017-10-02 10:10:26 +02:00
|
|
|
if self.capture_mode and self.elapsed < CaptureButton.countdown:
|
|
|
|
screen.blit(self.background, self.background_rect.topleft)
|
|
|
|
|
|
|
|
text = str(CaptureButton.countdown - self.elapsed)
|
|
|
|
size = self.font.size(text)
|
|
|
|
ren = self.font.render(text, True, (255, 255, 255))
|
|
|
|
ren.set_alpha(150)
|
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
topleft = (self.rect.topleft[0] + self.rect.width / 2 - size[0] / 2, self.rect.topleft[1] + self.rect.height / 2 - size[1] / 2)
|
2017-10-02 10:10:26 +02:00
|
|
|
top = self.background_rect.top + self.background_rect.height / 2 - size[1] / 2
|
|
|
|
left = self.background_rect.left + self.background_rect.width / 2 - size[0] / 2
|
|
|
|
screen.blit(ren, (left, top))
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
2017-09-28 16:39:22 +02:00
|
|
|
self.capture_mode = True
|
2017-10-02 09:48:20 +02:00
|
|
|
self.enabled = False
|
2017-09-28 16:39:22 +02:00
|
|
|
|
|
|
|
capture = Capture(self.count)
|
|
|
|
self.count += 1
|
|
|
|
|
2017-10-03 16:36:23 +02:00
|
|
|
for i in range(3):
|
2017-09-28 13:56:32 +02:00
|
|
|
wait = True
|
|
|
|
start = time.time()
|
|
|
|
while wait:
|
|
|
|
self.elapsed = int(time.time() - start)
|
2017-09-28 16:39:22 +02:00
|
|
|
if self.elapsed >= CaptureButton.countdown:
|
2017-09-28 13:56:32 +02:00
|
|
|
wait = False
|
2017-09-28 16:39:22 +02:00
|
|
|
|
|
|
|
self.photoboite.event()
|
|
|
|
self.photoboite.draw()
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.display.flip()
|
|
|
|
|
2017-09-29 12:35:52 +02:00
|
|
|
capture.take(self.photoboite.camera)
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.time.wait(1000)
|
2017-10-04 00:33:23 +02:00
|
|
|
|
|
|
|
for photo in capture.photos:
|
|
|
|
self.photoboite.screen.blit(self.photo_film, (0, 0))
|
|
|
|
pygame.display.flip()
|
|
|
|
pygame.time.wait(1000)
|
|
|
|
self.photoboite.event()
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
2017-09-28 16:39:22 +02:00
|
|
|
self.capture_mode = False
|
2017-10-02 09:48:20 +02:00
|
|
|
self.enabled = True
|
2017-10-03 16:36:23 +02:00
|
|
|
self.photoboite.sleep.move()
|
2017-09-28 16:39:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Capture:
|
|
|
|
def __init__(self, uid):
|
|
|
|
self.uid = uid
|
|
|
|
self.photos = []
|
|
|
|
|
2017-09-29 12:35:52 +02:00
|
|
|
def take(self, camera):
|
|
|
|
self.photos.append(Photo(self.uid, len(self.photos)).take(camera))
|
2017-09-28 16:39:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Photo:
|
|
|
|
def __init__(self, cid, uid):
|
|
|
|
self.cid = cid
|
|
|
|
self.uid = uid
|
|
|
|
|
2017-09-29 12:35:52 +02:00
|
|
|
def take(self, camera):
|
2017-09-28 16:39:22 +02:00
|
|
|
self.name = "%s-%04d-%04d" % (time.strftime('%Y%m%d-%H%M%S'), self.cid, self.uid)
|
2017-09-29 12:35:52 +02:00
|
|
|
camera.take(self.name)
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
return self
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-29 11:27:48 +02:00
|
|
|
|
2017-10-03 16:36:23 +02:00
|
|
|
class SleepMode:
|
|
|
|
def __init__(self, seconds):
|
|
|
|
self.enabled = True
|
|
|
|
self.drawn = False
|
|
|
|
self.seconds = seconds
|
|
|
|
|
|
|
|
def move(self):
|
|
|
|
self.last_action = time.time()
|
|
|
|
|
|
|
|
def event(self):
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.MOUSEBUTTONUP or event.type == pygame.KEYUP:
|
|
|
|
self.move()
|
|
|
|
self.enabled = False
|
|
|
|
|
|
|
|
def draw_on(self, screen):
|
|
|
|
if not self.drawn:
|
|
|
|
screen.fill((255,255,255))
|
|
|
|
pygame.display.flip()
|
|
|
|
self.drawn = True
|
|
|
|
|
|
|
|
def sleep(self):
|
|
|
|
if time.time() - self.last_action > self.seconds:
|
|
|
|
self.enabled = True
|
|
|
|
self.drawn = False
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
class Photoboite:
|
2017-09-27 00:04:44 +02:00
|
|
|
def __init__(self):
|
2017-09-28 09:36:11 +02:00
|
|
|
self.screen = self.screen()
|
|
|
|
pygame.font.init()
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-28 23:16:38 +02:00
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.buttons = []
|
|
|
|
|
2017-09-30 21:49:20 +02:00
|
|
|
button_size = int(self.screen.get_height() / 4)
|
|
|
|
topleft = (self.screen.get_width() - button_size) / 2, self.screen.get_height() - button_size - 10
|
2017-09-27 00:04:44 +02:00
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
self.buttons.append(CaptureButton(button_size, topleft, self))
|
2017-09-27 00:04:44 +02:00
|
|
|
|
2017-09-30 21:49:20 +02:00
|
|
|
camera_rect = pygame.Rect(0, 0, 800, 480)
|
2017-09-29 11:27:48 +02:00
|
|
|
self.camera = Camera(camera_rect)
|
2017-09-27 00:04:44 +02:00
|
|
|
|
2017-09-29 11:47:32 +02:00
|
|
|
self.clock = pygame.time.Clock()
|
|
|
|
|
2017-10-03 16:36:23 +02:00
|
|
|
self.sleep = SleepMode(30)
|
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
|
2017-09-28 09:36:11 +02:00
|
|
|
def screen(self):
|
|
|
|
if os.getenv('DISPLAY'):
|
|
|
|
pygame.display.init()
|
2017-09-28 23:20:27 +02:00
|
|
|
display = pygame.display.Info()
|
2017-09-30 21:49:20 +02:00
|
|
|
return pygame.display.set_mode((800, 480))
|
2017-09-28 09:36:11 +02:00
|
|
|
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()
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
def draw(self):
|
2017-09-29 11:47:32 +02:00
|
|
|
self.clock.tick(60)
|
|
|
|
|
2017-09-29 11:27:48 +02:00
|
|
|
self.camera.draw_on(self.screen)
|
2017-09-28 13:56:32 +02:00
|
|
|
|
|
|
|
for button in self.buttons:
|
|
|
|
button.draw_on(self.screen)
|
|
|
|
|
|
|
|
def event(self):
|
|
|
|
for event in pygame.event.get():
|
2017-10-03 16:36:23 +02:00
|
|
|
self.sleep.move()
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
|
2017-09-29 12:35:52 +02:00
|
|
|
self.quit()
|
2017-09-28 13:56:32 +02:00
|
|
|
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()
|
2017-09-28 09:36:11 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
|
|
|
pressed = False
|
|
|
|
while True:
|
2017-10-03 16:36:23 +02:00
|
|
|
if self.sleep.enabled:
|
|
|
|
self.sleep.event()
|
|
|
|
self.sleep.draw_on(self.screen)
|
|
|
|
self.clock.tick(60)
|
|
|
|
else:
|
|
|
|
self.event()
|
|
|
|
self.draw()
|
|
|
|
self.sleep.sleep()
|
|
|
|
pygame.display.flip()
|
2017-09-27 00:04:44 +02:00
|
|
|
|
2017-09-29 12:35:52 +02:00
|
|
|
def quit(self):
|
2017-09-30 21:49:20 +02:00
|
|
|
self.camera.close()
|
2017-09-29 12:35:52 +02:00
|
|
|
sys.exit()
|
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
Photoboite().run()
|