From cc58fdff58c8d7ce6f2fd6f1a8eac9b856e4971d Mon Sep 17 00:00:00 2001 From: James Hodgson Date: Fri, 7 Oct 2022 15:08:12 +0100 Subject: [PATCH] #22 Fix bytearray errors --- src/picam/picam.py | 4 ++-- src/picam/picam1.py | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/picam/picam.py b/src/picam/picam.py index f2eadb6..5327493 100644 --- a/src/picam/picam.py +++ b/src/picam/picam.py @@ -17,14 +17,14 @@ class PiCam: """ self.preview_resolution = resolution - def capture(self) -> bytes: + def capture(self) -> bytearray: """Capture an image :returns: Image data as a byte buffer """ raise NotImplementedError - def capture_preview(self) -> bytes: + def capture_preview(self) -> bytearray: """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 f2182ba..b8db65f 100644 --- a/src/picam/picam1.py +++ b/src/picam/picam1.py @@ -36,13 +36,17 @@ class PiCam1(PiCam): def capture(self, preview: bool = False) -> bytearray: # Bytes to hold output buffer - out = bytearray() - bytes_stream = BytesIO() + 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) if not preview: # If preview we'll already have the right resolution self.set_camera_resolution(self.capture_resolution) + bytes_stream = BytesIO() self.camera.capture(bytes_stream, format='rgb', use_video_port=preview) bytes_stream.seek(0) @@ -55,5 +59,5 @@ class PiCam1(PiCam): return out - def capture_preview(self) -> bytes: + def capture_preview(self) -> bytearray: return self.capture(preview=True)