stopi2/dslr_helper.py

149 lines
5.1 KiB
Python
Raw Normal View History

2025-02-02 15:45:05 +01:00
#!/usr/bin/env python
# python-gphoto2 - Python interface to libgphoto2
# http://github.com/jim-easterbrook/python-gphoto2
# Copyright (C) 2015-22 Jim Easterbrook jim@jim-easterbrook.me.uk
#
# This file is part of python-gphoto2.
#
# python-gphoto2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# python-gphoto2 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with python-gphoto2. If not, see
# <https://www.gnu.org/licenses/>.
import logging
import locale
import os
# ~ import subprocess
import sys
import gphoto2 as gp
camera_current_settings = {
'capturemode' : 3, # use IR remote
'imagesize' : 2, # use size S (1936x1296)
'whitebalance' : 1, # Natural light
'capturetarget' : 0, # Internal memory
'nocfcardrelease' : 0, # Allow capture without sd card
'recordingmedia' : 1, # Write to RAM
}
def initialize_camera():
camera = gp.check_result(gp.gp_camera_new())
try:
gp.check_result(gp.gp_camera_init(camera))
# get configuration tree
current_camera_config = gp.check_result(gp.gp_camera_get_config(camera))
except:
camera.exit()
camera = None
current_camera_config = None
return camera, current_camera_config
def apply_gphoto_setting(config, setting, new_value):
# find the $setting config item
try:
cur_setting = gp.check_result(gp.gp_widget_get_child_by_name(config, setting))
# find corresponding choice
cur_setting_choice = gp.check_result(gp.gp_widget_get_choice(cur_setting, new_value))
# set config value
gp.check_result(gp.gp_widget_set_value(cur_setting, cur_setting_choice))
except:
print("Configuration error while setting {} to {}".format(setting, new_value))
def apply_dslr_settings(camera_settings):
camera, config = initialize_camera()
# iterate over the settings dictionary
for setting in camera_settings:
print(setting)
print(camera_settings[setting])
apply_gphoto_setting(config, setting, camera_settings[setting])
# validate config
status = gp.check_result(gp.gp_camera_set_config(camera, config))
# close camera
camera.exit()
return status
def check_status_value(config, value, optimal_value=None):
cur_check = gp.check_result(gp.gp_widget_get_child_by_name(config, value))
cur_check_value = gp.check_result(gp.gp_widget_get_value(cur_check))
if optimal_value is not None:
cur_check_choice = gp.check_result(gp.gp_widget_get_choice(cur_check, optimal_value[value]))
return [cur_check_value, cur_check_choice]
else:
return cur_check_value
# ~ def check_status(camera, config):
# ~ for value in camera_status:
# ~ cur_check_value, cur_check_choice = check_status_value(config, value, camera_status)
# ~ if cur_check_value == cur_check_choice:
# ~ return True
# ~ else:
# ~ # Some values are not optimal
# ~ return False
def find_file_ext(gp_name:str, full_path:str):
# extract dir path
dirname = os.path.dirname(full_path)
# extract filename from path
new_name = os.path.basename(full_path)
# if the path does'nt contain file name, return camera's FS filename
if not full_path.endswith(('.jpg', '.JPG', '.raw')):
return gp_name
suffix = gp_name.split('.')[-1]
prefix = new_name.split('.')[:-1]
prefix.insert(len(prefix), suffix)
print(suffix)
print(prefix)
return os.path.join(dirname, '.'.join(prefix))
def capture_and_download(target:str='/tmp'):
camera, current_camera_config = initialize_camera()
# Check battery level
battery_level = int(check_status_value(current_camera_config, 'batterylevel')[:-1])
if battery_level < 10:
print("Battery level is too low, shutter disabled.")
return False
file_path = camera.capture(gp.GP_CAPTURE_IMAGE)
print('Camera file path: {0}/{1}'.format(file_path.folder, file_path.name))
print(target)
target = find_file_ext(file_path.name, target)
print('Copying image to', target)
try:
camera_file = camera.file_get(
file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL)
except:
print("Camera error. Check Battery and try restarting the camera.")
return False
try:
camera_file.save(target)
except:
print('File access error.')
return False
camera.exit()
return True
def main():
apply_dslr_settings(camera_current_settings)
if __name__ == "__main__":
logging.basicConfig(
format='%(levelname)s: %(name)s: %(message)s', level=logging.ERROR)
callback_obj = gp.check_result(gp.use_python_logging())
sys.exit(main())