Files
PiCamera/src/main.py
2022-10-07 14:40:39 +01:00

72 lines
2.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from picam_manager.picam_manager import PiCamManager
from picamui.picamui import PiCamUi
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()
ui.createUi()
# Setup camera
cam_manager = PiCamManager(config)
camera = cam_manager.camera
loop = True
while loop:
preview_bytes = camera.capture_preview()
ui.updatePreview(preview_bytes, camera.get_preview_resolution())
ui.update()
ui_events = ui.getEvents()
for event in ui_events:
if event == 'keyDownEscape' or event == 'pygameQuit' or event == 'btnExitPressed':
loop = False
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))
ui.cleanup()
def print_ascii_art() -> None:
"""Extremely important function"""
print("""
██  █████  ███  ███ ███████ ███████  ██████  █████  ███  ███ 
██ ██   ██ ████  ████ ██      ██      ██      ██   ██ ████  ████ 
██ ███████ ██ ████ ██ █████  ███████ ██  ███████ ██ ████ ██ 
██ ██ ██   ██ ██  ██  ██ ██          ██ ██  ██   ██ ██  ██  ██ 
 █████  ██  ██ ██  ██ ███████ ███████  ██████ ██  ██ ██  ██ 
""")
if __name__ == "__main__":
print_ascii_art()
main()