#22 use bytesio

This commit is contained in:
James Hodgson
2022-10-07 15:17:51 +01:00
parent cc58fdff58
commit a190e01799
3 changed files with 10 additions and 17 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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: