72 lines
2.9 KiB
Python
72 lines
2.9 KiB
Python
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()
|