#22 Begin python rework
This commit is contained in:
46
src/picam/picam.py
Normal file
46
src/picam/picam.py
Normal file
@@ -0,0 +1,46 @@
|
||||
class PiCam:
|
||||
def __init__(self, config: dict):
|
||||
self.capture_resolution = config["capture_resolution"]
|
||||
self.preview_resolution = config["preview_resolution"]
|
||||
|
||||
def set_capture_resolution(self, resolution: "tuple[int, int]") -> None:
|
||||
"""Set capture resolution for camera
|
||||
|
||||
:param resolution: New resolution for camera captures
|
||||
"""
|
||||
self.capture_resolution = resolution
|
||||
|
||||
def set_preview_resolution(self, resolution: "tuple[int, int]") -> None:
|
||||
"""Set resolution for camera preview (viewfinder)
|
||||
|
||||
:param resolution: New resolution for camera preview
|
||||
"""
|
||||
self.preview_resolution = resolution
|
||||
|
||||
def get_preview_resolution(self) -> "tuple[int, int]":
|
||||
"""Get the current preview resolution
|
||||
|
||||
:returns: Tuple containing current preview resolution (x, y)
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def get_capture_resolution(self) -> "tuple[int, int]":
|
||||
"""Get the current capture resolution
|
||||
|
||||
:returns: Tuple containing current capture resolution (x, y)
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def capture(self) -> bytes:
|
||||
"""Capture an image
|
||||
|
||||
:returns: Image data as a byte buffer
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def capture_preview(self) -> bytes:
|
||||
"""Capture a preview image for the viewfinder
|
||||
|
||||
:returns: Image data as a byte buffer
|
||||
"""
|
||||
raise NotImplementedError
|
||||
53
src/picam/picam1.py
Normal file
53
src/picam/picam1.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from picam import PiCam
|
||||
|
||||
from picamera import PiCamera
|
||||
|
||||
class PiCam1(PiCam):
|
||||
"""Implementation of PiCam class using legacy picamera module"""
|
||||
def __init__(self, config: dict):
|
||||
super().__init__(config)
|
||||
|
||||
self.init_camera()
|
||||
|
||||
def __del__(self):
|
||||
"""Destructor - cleans up picamera"""
|
||||
self.close_camera()
|
||||
|
||||
def set_camera_resolution(self, resolution: "tuple[int, int]") -> None:
|
||||
"""Set resolution of active camera
|
||||
|
||||
:param resolution: Resolution to set camera to
|
||||
"""
|
||||
self.camera.resolution = resolution
|
||||
|
||||
def set_preview_resolution(self, resolution: "tuple[int, int]") -> None:
|
||||
self.set_camera_resolution(resolution)
|
||||
return super().set_preview_resolution(resolution)
|
||||
|
||||
def init_camera(self) -> None:
|
||||
"""Initialize picamera camera object"""
|
||||
self.camera = PiCamera(resolution=self.preview_resolution)
|
||||
|
||||
def close_camera(self) -> None:
|
||||
"""Close picamera camera object"""
|
||||
self.camera.close()
|
||||
self.camera = None
|
||||
|
||||
def capture(self, preview: bool = False) -> bytes:
|
||||
# Bytes to hold output buffer
|
||||
out = bytes()
|
||||
|
||||
if not preview:
|
||||
# If preview we'll already have the right resolution
|
||||
self.set_camera_resolution(self.capture_resolution)
|
||||
|
||||
self.camera.capture(out, format='rgb', use_video_port=preview)
|
||||
|
||||
if not preview:
|
||||
# If preview we'll already have the right resolution
|
||||
self.set_camera_resolution(self.preview_resolution)
|
||||
|
||||
return out
|
||||
|
||||
def capture_preview(self) -> bytes:
|
||||
return self.capture(preview=True)
|
||||
Reference in New Issue
Block a user