#22 Begin python rework

This commit is contained in:
James Hodgson
2022-10-07 14:40:39 +01:00
parent 97adf57e2e
commit 783eff7636
9 changed files with 204 additions and 65 deletions

View File

@@ -1,36 +1,71 @@
import picamui
import picam
from picam_manager.picam_manager import PiCamManager
from picamui.picamui import PiCamUi
def main():
captureResolution = (1280, 1024)
captureDirectory = "./images"
captureExtension = "jpg"
import json
from PIL import Image
from datetime import datetime
CONFIG_PATH = "./config.json"
def load_config() -> dict:
"""Load JSON config from disk
:returns: Loaded JSON configuration as a dict
"""
with open(CONFIG_PATH, 'rt') as config_file:
return json.load(config_file)
def main() -> None:
"""Main function for JamesCam"""
config = load_config()
# Setup UI
ui = picamui.PiCamUi()
ui = PiCamUi()
ui.createUi()
# Setup camera
cam = picam.PiCam()
cam.setPreviewResolution(ui.getScreenResolution())
captureResolution = cam.getMaxResolution()
cam_manager = PiCamManager(config)
camera = cam_manager.camera
loop = True
while loop:
rgb = cam.getPreviewFrame()
ui.updatePreview(rgb, cam.getPreviewResolution())
preview_bytes = camera.capture_preview()
ui.updatePreview(preview_bytes, camera.get_preview_resolution())
ui.update()
uiEvents = ui.getEvents()
for event in uiEvents:
if event == "keyDownEscape" or event == "pygameQuit" or event == "btnExitPressed":
ui_events = ui.getEvents()
for event in ui_events:
if event == 'keyDownEscape' or event == 'pygameQuit' or event == 'btnExitPressed':
loop = False
elif event == "btnTakePressed":
cam.capture(captureResolution, captureDirectory, captureExtension)
elif event == 'btnTakePressed':
now = datetime.now()
capture_bytes = camera.capture()
pil_image = Image.frombuffer(mode='RGB', size=camera.get_capture_resolution(), data=capture_bytes)
formatted_filename = now.strftime(config['output_filename_format'])
filename = f'{formatted_filename}{config["output_extension"]}'
with open(f'{config["output_directory"]}/{filename}', "wb") as output_file:
pil_image.save(output_file, format=config["output_format"])
else:
print("Unknown event {}".format(event))
print('Unknown event {}'.format(event))
ui.cleanup()
def print_ascii_art() -> None:
"""Extremely important function"""
print("""
██  █████  ███  ███ ███████ ███████  ██████  █████  ███  ███ 
██ ██   ██ ████  ████ ██      ██      ██      ██   ██ ████  ████ 
██ ███████ ██ ████ ██ █████  ███████ ██  ███████ ██ ████ ██ 
██ ██ ██   ██ ██  ██  ██ ██          ██ ██  ██   ██ ██  ██  ██ 
 █████  ██  ██ ██  ██ ███████ ███████  ██████ ██  ██ ██  ██ 
""")
if __name__ == "__main__":
print_ascii_art()
main()