diff --git a/photoboite.py b/photoboite.py index 80758e8..70b485f 100755 --- a/photoboite.py +++ b/photoboite.py @@ -2,7 +2,55 @@ import io, os, sys, time, pygame -import picamera +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) + + def draw_on(self, screen): + screen.blit(self.img, self.rect) + + class Button: def __init__(self, size, topleft): @@ -118,36 +166,6 @@ class Photo: 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) - 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) - - class Photoboite: def __init__(self): self.screen = self.screen()