Use fake Camera when picamera is not found

master
Guillaume Dott 2017-10-02 10:59:49 +02:00
parent f67ec8b037
commit 228ed201a6
1 changed files with 49 additions and 31 deletions

View File

@ -2,7 +2,55 @@
import io, os, sys, time, pygame 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: class Button:
def __init__(self, size, topleft): def __init__(self, size, topleft):
@ -118,36 +166,6 @@ class Photo:
return self 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: class Photoboite:
def __init__(self): def __init__(self):
self.screen = self.screen() self.screen = self.screen()