From a190e017998e45be5e03ec20ef0a3651df77daeb Mon Sep 17 00:00:00 2001 From: James Hodgson Date: Fri, 7 Oct 2022 15:17:51 +0100 Subject: [PATCH] #22 use bytesio --- src/picam/picam.py | 6 ++++-- src/picam/picam1.py | 19 +++++-------------- src/picamui/picamui.py | 2 +- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/picam/picam.py b/src/picam/picam.py index 5327493..48906e5 100644 --- a/src/picam/picam.py +++ b/src/picam/picam.py @@ -1,3 +1,5 @@ +from io import BytesIO + class PiCam: def __init__(self, config: dict): self.capture_resolution = config["capture_resolution"] @@ -17,14 +19,14 @@ class PiCam: """ self.preview_resolution = resolution - def capture(self) -> bytearray: + def capture(self) -> BytesIO: """Capture an image :returns: Image data as a byte buffer """ raise NotImplementedError - def capture_preview(self) -> bytearray: + def capture_preview(self) -> BytesIO: """Capture a preview image for the viewfinder :returns: Image data as a byte buffer diff --git a/src/picam/picam1.py b/src/picam/picam1.py index b8db65f..65e9321 100644 --- a/src/picam/picam1.py +++ b/src/picam/picam1.py @@ -34,14 +34,7 @@ class PiCam1(PiCam): self.camera.close() self.camera = None - def capture(self, preview: bool = False) -> bytearray: - # Bytes to hold output buffer - if preview: - # Multiply by 3 for individual R, G, B values - out = bytearray((self.preview_resolution[0] * self.preview_resolution[1]) * 3) - else: - out = bytearray((self.capture_resolution[0] * self.capture_resolution[1]) * 3) - + def capture(self, preview: bool = False) -> BytesIO: if not preview: # If preview we'll already have the right resolution self.set_camera_resolution(self.capture_resolution) @@ -49,15 +42,13 @@ class PiCam1(PiCam): bytes_stream = BytesIO() self.camera.capture(bytes_stream, format='rgb', use_video_port=preview) - bytes_stream.seek(0) - bytes_stream.readinto(out) - bytes_stream.close() - if not preview: # If preview we'll already have the right resolution self.set_camera_resolution(self.preview_resolution) - return out + bytes_stream.seek(0) - def capture_preview(self) -> bytearray: + return bytes_stream + + def capture_preview(self) -> BytesIO: return self.capture(preview=True) diff --git a/src/picamui/picamui.py b/src/picamui/picamui.py index b99718b..5c5aa54 100644 --- a/src/picamui/picamui.py +++ b/src/picamui/picamui.py @@ -41,7 +41,7 @@ class PiCamUi: self.uiElements.append(btnExit) def updatePreview(self, rgb, res): - img = pygame.image.frombuffer(rgb, res, 'RGB') + img = pygame.image.load(rgb, res, 'RGB') img = pygame.transform.scale(img, (self.screen.get_width(), self.screen.get_height())) if img: