nenuscanner/camera.py

59 lines
1.6 KiB
Python

import gphoto2 as gp
import shutil
from . import leds
class Camera:
def __init__(self):
self.inner = gp.Camera()
def __enter__(self):
self.inner.init()
def __exit__(self):
self.inner.exit()
def capture(self, output_path: str) -> bool:
try:
with self:
file_path = self.inner.capture(gp.GP_CAPTURE_IMAGE)
preview = self.inner.file_get(file_path.folder, file_path.name[:-3] + '.JPG', gp.GP_FILE_TYPE_NORMAL)
raw = self.inner.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW)
preview.sve(output_path + '.jpg')
raw.save(output_path + '.cr2')
return True
except Exception as e:
print('An error occured when capturing photo', e)
return False
class DummyCamera:
def __init__(self, leds: leds.DummyLeds):
self.leds = leds
def __enter__(self):
pass
def __exit__(self):
pass
def capture(self, output_path: str) -> bool:
# Find which leds are turned on
found = None
all_on = False
for led in self.leds.leds:
if led.is_on:
if found is None:
found = led
else:
all_on = True
if found:
shutil.copyfile('data-keep/small/' + str(found) + '.jpg', output_path + '.jpg')
elif all_on:
shutil.copyfile('data-keep/small/LED17.jpg', output_path + '.jpg')
else:
shutil.copyfile('data-keep/small/LED17.jpg', output_path + '.jpg')