#!/usr/bin/env python import gphoto2 as gp import os import sys from time import time, sleep EXPOSURE_LENGTH = 6 ISO = '200' def take_image(): context = gp.Context() camera = gp.Camera() camera.init() config = camera.get_config() updated_config = False shutterspeed = config.get_child_by_name('shutterspeed') if shutterspeed.get_value() != 'bulb': shutterspeed.set_value('bulb') updated_config = True iso = config.get_child_by_name('iso') if iso.get_value() != ISO: iso.set_value(ISO) updated_config = True if updated_config: camera.set_config(config) print(f"Shutterspeed is {shutterspeed.get_value()}") print(f"ISO is {iso.get_value()}") print(f"Exposure Length is {EXPOSURE_LENGTH}s") print("Capture Image") t = time() bulb = config.get_child_by_name('bulb') bulb.set_value(1) camera.set_config(config) sleep(EXPOSURE_LENGTH) bulb.set_value(0) camera.set_config(config) file_added = False capture_complete = False timeout = False folder = None file = None data = None while (not file_added or not capture_complete) and not timeout: typ, data = camera.wait_for_event(3000) if typ == gp.GP_EVENT_FILE_ADDED: print(f'File Exists On Camera {data.folder}{data.name}') file_added = True folder = data.folder file = data.name if typ == gp.GP_EVENT_CAPTURE_COMPLETE: print('Capture Complete!') capture_complete = True if typ == gp.GP_EVENT_TIMEOUT: print("Timeout waiting for image") timeout = True continue f = camera.file_get(folder, file, gp.GP_FILE_TYPE_NORMAL, data, context) t2 = time() print(f"Elapsed time {t2-t}s") target_path = os.path.join(os.getcwd(), file) f.save(target_path) print("Cleaning up file on camera") camera.file_delete(folder, file) return 0 if __name__ == '__main__': sys.exit(take_image())