Created
January 28, 2022 05:28
-
-
Save programatt/1d1f4987770ed8ecf5327df0ae561d0a to your computer and use it in GitHub Desktop.
GPhoto2 Python Bulb Exposure Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import gphoto2 as gp | |
import os | |
import sys | |
from time import time, sleep | |
EXPOSURE_LENGTH = 10 | |
ISO = '100' | |
def take_image(): | |
context = gp.Context() | |
camera = gp.Camera() | |
camera.init() | |
config = camera.get_config() | |
cameramode = config.get_child_by_name('autoexposuremode').get_value() | |
if cameramode != 'Manual': | |
print("Cannot take bulb exposure if camera not in Manual ('M') mode.") | |
return -1 | |
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"Camera Mode is {cameramode}") | |
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('eosremoterelease') | |
bulb.set_value('Immediate') | |
camera.set_config(config) | |
sleep(EXPOSURE_LENGTH) | |
bulb.set_value('Release Full') | |
camera.set_config(config) | |
file_added = False | |
capture_complete = False | |
timeout = False | |
folder = None | |
file = 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, 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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment