#22 use bytesio
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
from io import BytesIO
|
||||||
|
|
||||||
class PiCam:
|
class PiCam:
|
||||||
def __init__(self, config: dict):
|
def __init__(self, config: dict):
|
||||||
self.capture_resolution = config["capture_resolution"]
|
self.capture_resolution = config["capture_resolution"]
|
||||||
@@ -17,14 +19,14 @@ class PiCam:
|
|||||||
"""
|
"""
|
||||||
self.preview_resolution = resolution
|
self.preview_resolution = resolution
|
||||||
|
|
||||||
def capture(self) -> bytearray:
|
def capture(self) -> BytesIO:
|
||||||
"""Capture an image
|
"""Capture an image
|
||||||
|
|
||||||
:returns: Image data as a byte buffer
|
:returns: Image data as a byte buffer
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def capture_preview(self) -> bytearray:
|
def capture_preview(self) -> BytesIO:
|
||||||
"""Capture a preview image for the viewfinder
|
"""Capture a preview image for the viewfinder
|
||||||
|
|
||||||
:returns: Image data as a byte buffer
|
:returns: Image data as a byte buffer
|
||||||
|
|||||||
@@ -34,14 +34,7 @@ class PiCam1(PiCam):
|
|||||||
self.camera.close()
|
self.camera.close()
|
||||||
self.camera = None
|
self.camera = None
|
||||||
|
|
||||||
def capture(self, preview: bool = False) -> bytearray:
|
def capture(self, preview: bool = False) -> BytesIO:
|
||||||
# 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)
|
|
||||||
|
|
||||||
if not preview:
|
if not preview:
|
||||||
# If preview we'll already have the right resolution
|
# If preview we'll already have the right resolution
|
||||||
self.set_camera_resolution(self.capture_resolution)
|
self.set_camera_resolution(self.capture_resolution)
|
||||||
@@ -49,15 +42,13 @@ class PiCam1(PiCam):
|
|||||||
bytes_stream = BytesIO()
|
bytes_stream = BytesIO()
|
||||||
self.camera.capture(bytes_stream, format='rgb', use_video_port=preview)
|
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 not preview:
|
||||||
# If preview we'll already have the right resolution
|
# If preview we'll already have the right resolution
|
||||||
self.set_camera_resolution(self.preview_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)
|
return self.capture(preview=True)
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class PiCamUi:
|
|||||||
self.uiElements.append(btnExit)
|
self.uiElements.append(btnExit)
|
||||||
|
|
||||||
def updatePreview(self, rgb, res):
|
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()))
|
img = pygame.transform.scale(img, (self.screen.get_width(), self.screen.get_height()))
|
||||||
|
|
||||||
if img:
|
if img:
|
||||||
|
|||||||
Reference in New Issue
Block a user