def main(): # use Python logging logging.basicConfig( format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) # open camera connection camera = gp.check_result(gp.gp_camera_new()) context = gp.gp_context_new() gp.check_result(gp.gp_camera_init(camera, context)) # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera, context)) # find the date/time setting config item and get it # name varies with camera driver # Canon EOS350d - 'datetime' # PTP - 'd034' for name, fmt in (('datetime', '%Y-%m-%d %H:%M:%S'), ('d034', None)): now = datetime.now() OK, datetime_config = gp.gp_widget_get_child_by_name(config, name) if OK >= gp.GP_OK: widget_type = gp.check_result(gp.gp_widget_get_type(datetime_config)) if widget_type == gp.GP_WIDGET_DATE: raw_value = gp.check_result( gp.gp_widget_get_value(datetime_config)) camera_time = datetime.fromtimestamp(raw_value) else: raw_value = gp.check_result( gp.gp_widget_get_value(datetime_config)) if fmt: camera_time = datetime.strptime(raw_value, fmt) else: camera_time = datetime.utcfromtimestamp(float(raw_value)) print('Camera clock: ', camera_time.isoformat(' ')) print('Computer clock:', now.isoformat(' ')) err = now - camera_time if err.days < 0: err = -err lead_lag = 'ahead' print('Camera clock is ahead by',) else: lead_lag = 'behind' print('Camera clock is %s by %d days and %d seconds' % ( lead_lag, err.days, err.seconds)) break else: print('Unknown date/time config item') # clean up gp.check_result(gp.gp_camera_exit(camera, context)) return 0
def connect_camera(): global CAMERA logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) CAMERA = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(CAMERA)) # required configuration will depend on camera type! print "Checking camera config" # get configuration tree config = gp.check_result(gp.gp_camera_get_config(CAMERA)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): raise PeripheralStatusError('Camera is setup to record raw, but we need previs, and preview does not work with raw images') # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(CAMERA, config))
def init_camera(self): self.log.debug("Init GPhoto2 camera") callback_obj = gp.check_result(gp.use_python_logging()) self.camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(self.camera)) # required configuration will depend on camera type! self.log.info('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(self.camera)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name( config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): self.log.error('Cannot preview raw images') return None # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result( gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(self.camera, config)) return True
def initCamera(): global camera global context print("Init camera") # SLR Setup # GPhoto init / testing context = gp.gp_context_new() error, camera = gp.gp_camera_new() error = gp.gp_camera_init(camera, context) error, text = gp.gp_camera_get_summary(camera, context) #print('Summary') #print('=======') #print(text.text) # required configuration will depend on camera type! print('Checking camera config') config = gp.check_result(gp.gp_camera_get_config(camera)) OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: value = gp.check_result(gp.gp_widget_get_value(image_format)) if 'raw' in value.lower(): print('Cannot preview raw images') # find the capture size class config item OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) gp.check_result(gp.gp_camera_set_config(camera, config))
def get_exposure_bias(self): error, bias = gp.gp_widget_get_child_by_name(self.config,'exposurecompensation') if error !=0: print(error) print('get_exposure_bias: cannot read widget!') return else: self.bias = bias error, value = gp.gp_widget_get_value(bias) if error !=0: print(error) print('get_exposure_bias: cannot read value!') return count = gp.check_result(gp.gp_widget_count_choices(self.bias)) for choice in range(count): error, test = gp.gp_widget_get_choice(self.bias, choice) if test == value: break if error !=0: print(error) print('get_exposure_bias: cannot find choice!') return return choice, value
def main(): # use Python logging logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) # open camera connection camera = gp.check_result(gp.gp_camera_new()) context = gp.gp_context_new() gp.check_result(gp.gp_camera_init(camera, context)) # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera, context)) # find the capture target config item capture_target = gp.check_result( gp.gp_widget_get_child_by_name(config, 'capturetarget')) # print current setting value = gp.check_result(gp.gp_widget_get_value(capture_target)) print('Current setting:', value) # print possible settings for n in range(gp.check_result( gp.gp_widget_count_choices(capture_target))): choice = gp.check_result(gp.gp_widget_get_choice(capture_target, n)) print('Choice:', n, choice) # clean up gp.check_result(gp.gp_camera_exit(camera, context)) return 0
def get_exposure_time(self): error, exptime = gp.gp_widget_get_child_by_name(self.config,'shutterspeed2') if error !=0: print(error) print('get_exposure_time: cannot read widget!') return else: self.exptime = exptime error, value = gp.gp_widget_get_value(exptime) if error !=0: print(error) print('get_exposure_time: cannot read value!') return count = gp.check_result(gp.gp_widget_count_choices(self.exptime)) for choice in range(count): error, test = gp.gp_widget_get_choice(self.exptime, choice) if test == value: break if error !=0: print(error) print('get_exposure_time: cannot find choice!') return return choice, value
def get_iso(self): error, isovalue = gp.gp_widget_get_child_by_name(self.config,'iso') if error !=0: print(error) print('get_iso: cannot read widget!') return else: self.iso = isovalue error, value = gp.gp_widget_get_value(isovalue) if error !=0: print(error) print('get_iso: cannot read value!') return count = gp.check_result(gp.gp_widget_count_choices(self.iso)) for choice in range(count): error, test = gp.gp_widget_get_choice(self.iso, choice) if test == value: break if error !=0: print(error) print('get_iso: cannot find choice!') return return choice, value
def __init__(self, resolution=(320, 240), framerate=32, **kwargs): print("Init camera") # SLR Setup self.shotRequested = False # GPhoto init / testing self.context = gp.gp_context_new() self.error, self.camera = gp.gp_camera_new() self.error = gp.gp_camera_init(self.camera, self.context) self.error, self.text = gp.gp_camera_get_summary( self.camera, self.context) # required configuration will depend on camera type! print('Checking camera config') self.config = gp.check_result(gp.gp_camera_get_config(self.camera)) OK, image_format = gp.gp_widget_get_child_by_name( self.config, 'imageformat') if OK >= gp.GP_OK: value = gp.check_result(gp.gp_widget_get_value(image_format)) if 'raw' in value.lower(): print('Cannot preview raw images') # find the capture size class config item OK, capture_size_class = gp.gp_widget_get_child_by_name( self.config, 'capturesizeclass') if OK >= gp.GP_OK: value = gp.check_result( gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) gp.check_result(gp.gp_camera_set_config(self.camera, config)) self.frame = None self.shot = None self.stopped = False
def setConfig(config_item, config_value): child_type = gp.check_result(gp.gp_widget_get_type( config_all[config_item])) if child_type == gp.GP_WIDGET_RADIO: value = gp.check_result( gp.gp_widget_get_choice(config_all[config_item], int(config_value))) gp.check_result(gp.gp_widget_set_value(config_all[config_item], value)) gp.check_result(gp.gp_camera_set_config(camera, config, context)) elif child_type == gp.GP_WIDGET_TEXT: gp.check_result( gp.gp_widget_set_value(config_all[config_item], config_value)) gp.check_result(gp.gp_camera_set_config(camera, config, context)) elif child_type == gp.GP_WIDGET_RANGE: gp.check_result( gp.gp_widget_set_value(config_all[config_item], int(config_value))) gp.check_result(gp.gp_camera_set_config(camera, config, context)) elif child_type == gp.GP_WIDGET_TOGGLE: value = gp.check_result(gp.gp_widget_get_value( config_all[config_item])) if value: gp.check_result(gp.gp_widget_set_value(config_all[config_item], 0)) else: gp.check_result(gp.gp_widget_set_value(config_all[config_item], 1)) gp.check_result(gp.gp_camera_set_config(camera, config, context)) return "done"
def __walk_config(self, widget, cfg, pname=''): child_count = gp.check_result(gp.gp_widget_count_children(widget)) for n in range(child_count): child = gp.check_result(gp.gp_widget_get_child(widget, n)) label = gp.check_result(gp.gp_widget_get_label(child)) name = gp.check_result(gp.gp_widget_get_name(child)) child_type = gp.check_result(gp.gp_widget_get_type(child)) _name = pname + '/' +name _cfg = {'_type':child_type,'_widget':child,'_label':label} if child_type != gp.GP_WIDGET_SECTION: value = gp.check_result(gp.gp_widget_get_value(child)) print label, name, value _cfg['_value'] = value if child_type == gp.GP_WIDGET_RADIO: _cfg['_choice'] = [] choice_count = gp.check_result(gp.gp_widget_count_choices(child)) for n in range(choice_count): choice = gp.check_result(gp.gp_widget_get_choice(child, n)) if choice: _cfg['_choice'].append(choice) if child_type == gp.GP_WIDGET_RANGE: lo, hi, inc = gp.check_result(gp.gp_widget_get_range(child)) _cfg['_lo'] = lo _cfg['_hi'] = hi _cfg['_inc'] = inc cfg[_name]=_cfg self.__walk_config(child,cfg,pname=_name)
def open(self): context = gp.Context() camera = gp.Camera() camera.init(context) config = camera.get_config(context) # find and check the image format config item ok, image_format = gp.gp_widget_get_child_by_name( config, 'imageformat') if ok >= gp.GP_OK: value = gp.check_result(gp.gp_widget_get_value(image_format)) if value == 'raw': raise RuntimeError('Cannot preview raw images!') # find and set the capture size class config item # this is required for some canon cameras and does not hurt for others ok, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if ok >= gp.GP_OK: value = gp.check_result( gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) gp.check_result(gp.gp_camera_set_config(camera, config, context)) self.context = context self.camera = camera self.config = config
def get_config_value(self, name): config = gp.check_result(gp.gp_camera_get_config(self.camera, self.context)) OK, widget = gp.gp_widget_get_child_by_name(config, name) if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_value(widget)) print "get %s => %s" % (name, value) return value
def _get_config_value(self, config, name): error, conf_result = gp.gp_widget_get_child_by_name(config, name) gp.check_result(error) error, result = gp.gp_widget_get_value(conf_result) gp.check_result(error) return result
def __init__(self, config_changed, config, parent=None): QtGui.QCheckBox.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 value = gp.check_result(gp.gp_widget_get_value(config)) self.setChecked(value != 0) self.clicked.connect(self.new_value)
def __init__(self, config_changed, config, parent=None): QtGui.QLineEdit.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 value = gp.check_result(gp.gp_widget_get_value(config)) if value: self.setText(value) self.editingFinished.connect(self.new_value)
def __init__(self, config_changed, config, parent=None): QtGui.QDateTimeEdit.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 value = gp.check_result(gp.gp_widget_get_value(config)) if value: self.setDateTime(datetime.fromtimestamp(value)) self.dateTimeChanged.connect(self.new_value) self.setDisplayFormat('yyyy-MM-dd hh:mm:ss')
def __init__(self, config_changed, config, parent=None): QtGui.QSlider.__init__(self, Qt.Horizontal, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 lo, hi, self.inc = gp.check_result(gp.gp_widget_get_range(config)) value = gp.check_result(gp.gp_widget_get_value(config)) self.setRange(int(lo * self.inc), int(hi * self.inc)) self.setValue(int(value * self.inc)) self.sliderReleased.connect(self.new_value)
def __init__(self, config_changed, config, parent=None): QtWidgets.QLineEdit.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 value = gp.check_result(gp.gp_widget_get_value(config)) if value: if sys.version_info[0] < 3: value = value.decode('utf-8') self.setText(value) self.editingFinished.connect(self.new_value)
def get_config_value(self, field_name): """ Get configValue for given configField :param field_name: :return: """ # get configuration tree config = gp.check_result(gp.gp_camera_get_config(self.camera, self.context)) # find the capture target config item config_target = gp.check_result(gp.gp_widget_get_child_by_name(config, str(field_name))) # check value in range value = gp.check_result(gp.gp_widget_get_value(config_target)) return value
def read_exposure_settings(camera): ''' Get current exposure settings from your camera. ''' # get config keys config = gp.check_result(gp.gp_camera_get_config(camera)) iso_cfg = gp.check_result( \ gp.gp_widget_get_child_by_name(config, 'iso')) fnum_cfg = gp.check_result( \ gp.gp_widget_get_child_by_name(config, 'f-number')) speed_cfg = gp.check_result( \ gp.gp_widget_get_child_by_name(config, 'shutterspeed')) # get config values iso_val = gp.check_result(gp.gp_widget_get_value(iso_cfg)) fnum_val = gp.check_result(gp.gp_widget_get_value(fnum_cfg)) speed_val = gp.check_result(gp.gp_widget_get_value(speed_cfg)) # return return dict(iso=iso_val, fnum=fnum_val, speed=speed_val)
def main(): logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=logging.ERROR) gp.check_result(gp.use_python_logging()) camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera)) # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): print('Cannot preview raw images') return 1 # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config)) # capture preview image (not saved to camera memory card) print('Capturing preview image') for x in xrange(1,100): millis = int(round(time.time() * 1000)) camera_file = gp.check_result(gp.gp_camera_capture_preview(camera)) print("capture %d %s\n" % (int(round(time.time() * 1000)) - millis, camera_file)) file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file)) print("download %d\n" % (int(round(time.time() * 1000)) - millis)) data = memoryview(file_data) # display image #image = Image.open(io.BytesIO(file_data)) #image.show() gp.check_result(gp.gp_camera_exit(camera)) return 0
def get_config_value(self, field_name): """ Get configValue for given configField :param field_name: :return: """ # get configuration tree config = gp.check_result( gp.gp_camera_get_config(self.camera, self.context)) # find the capture target config item config_target = gp.check_result( gp.gp_widget_get_child_by_name(config, str(field_name))) # check value in range value = gp.check_result(gp.gp_widget_get_value(config_target)) return value
def __init__(self, config_changed, config, parent=None): QtGui.QComboBox.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 value = gp.check_result(gp.gp_widget_get_value(config)) choice_count = gp.check_result(gp.gp_widget_count_choices(config)) for n in range(choice_count): choice = gp.check_result(gp.gp_widget_get_choice(config, n)) if choice: self.addItem(choice) if choice == value: self.setCurrentIndex(n) self.currentIndexChanged.connect(self.new_value)
def take_a_pic(): logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera)) # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): print('Cannot preview raw images') return 1 # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result( gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result( gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config)) # capture preview image (not saved to camera memory card) print('Capturing preview image') camera_file = gp.check_result( gp.gp_camera_capture_preview(camera)) file_data = gp.check_result( gp.gp_file_get_data_and_size(camera_file)) # display image data = memoryview(file_data) print(type(data), len(data)) print(data[:10].tolist()) image = Image.open(io.BytesIO(file_data)) image.show() gp.check_result(gp.gp_camera_exit(camera)) return image
def checkBattery(camera, batt_lvl): #global camera, batt_lvl # Get configuration tree cameraConfig = gp.check_result(gp.gp_camera_get_config(camera)) # Get Camera model batteryLevelWidget = gp.check_result( gp.gp_widget_get_child_by_name(cameraConfig, 'batterylevel')) batteryLevel = "" batteryLevel = gp.check_result( gp.gp_widget_get_value(batteryLevelWidget)) #, cameraName)) if batteryLevel < batt_lvl: logging.critical("Battery level too low : %d%" % int(batteryLevel)) return False else: logging.info("Battery level : %d%" % int(batteryLevel)) return True
def __init__(self, config_changed, config, parent=None): QtWidgets.QWidget.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 self.setLayout(QtWidgets.QHBoxLayout()) value = gp.check_result(gp.gp_widget_get_value(config)) self.buttons = [] for choice in gp.check_result(gp.gp_widget_get_choices(config)): if choice: button = QtWidgets.QRadioButton(choice) self.layout().addWidget(button) if choice == value: button.setChecked(True) self.buttons.append((button, choice)) button.clicked.connect(self.new_value)
def __init__(self, config_changed, config, parent=None): QtGui.QWidget.__init__(self, parent) self.config_changed = config_changed self.config = config if gp.check_result(gp.gp_widget_get_readonly(config)): self.setDisabled(True) assert gp.check_result(gp.gp_widget_count_children(config)) == 0 self.setLayout(QtGui.QHBoxLayout()) value = gp.check_result(gp.gp_widget_get_value(config)) self.buttons = [] for choice in gp.check_result(gp.gp_widget_get_choices(config)): if choice: button = QtGui.QRadioButton(choice) self.layout().addWidget(button) if choice == value: button.setChecked(True) self.buttons.append((button, choice)) button.clicked.connect(self.new_value)
def getCamera(cameraNumber=0): logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) # make a list of all available cameras camera_list = [] for name, addr in gp.check_result(gp.gp_camera_autodetect()): camera_list.append((name, addr)) camera_list.sort(key=lambda x: x[0]) if len(camera_list) == 0: print("No cameras found") return None if cameraNumber < 0 or cameraNumber >= len(camera_list): print('Camera out of range') return None # initialise chosen camera name, addr = camera_list[cameraNumber] camera = gp.Camera() # search ports for camera port name port_info_list = gp.PortInfoList() port_info_list.load() idx = port_info_list.lookup_path(addr) camera.set_port_info(port_info_list[idx]) camera.init() config = gp.check_result(gp.gp_camera_get_config(camera)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config)) return camera
def main(): logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera)) # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): print('Cannot preview raw images') return 1
def main(): logging.basicConfig( format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) context = gp.gp_context_new() camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera, context)) # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera, context)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): print('Cannot preview raw images') return 1 # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config, context)) # capture preview image (not saved to camera memory card) print('Capturing preview image') camera_file = gp.check_result(gp.gp_camera_capture_preview(camera, context)) file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file)) # display image data = memoryview(file_data) print(type(data), len(data)) print(data[:10].tolist()) image = Image.open(io.BytesIO(file_data)) image.show() gp.check_result(gp.gp_camera_exit(camera, context)) return 0
def preview_image(): callback_obj = gp.check_result(gp.use_python_logging()) camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera)) # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera)) # find the image format config item # camera dependent - 'imageformat' is 'imagequality' on some OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): print('Cannot preview raw images') # return 1 # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config)) # capture preview image (not saved to camera memory card) print('Capturing preview image') camera_file = gp.check_result(gp.gp_camera_capture_preview(camera)) file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file)) # display image data = memoryview(file_data) image = cv2.imdecode(np.asarray(bytearray(data), dtype=np.uint8), 3) cv2.imshow("preview",image) cv2.waitKey() # image.show() gp.check_result(gp.gp_camera_exit(camera))
def waitForCamera(context): showText(AREA_PREVIEW,"Warte auf Kamera ") pygame.display.flip() camera = gp.check_result(gp.gp_camera_new()) err=gp.gp_camera_init(camera, context) if (err < gp.GP_OK): if err != gp.GP_ERROR_MODEL_NOT_FOUND: # some other error we can't handle here raise gp.GPhoto2Error(err) return # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera, context)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): raise gp.GPhoto2Error('Cannot preview raw images') # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config, context)) OK,txt=gp.gp_camera_get_summary(camera,context) infod=txToDict(txt.text) showText(AREA_PREVIEW,infod.get('Model')) info.camera=infod.get('Model') pygame.display.flip() return camera
def waitForCamera(context): showText(AREA_PREVIEW, "Warte auf Kamera ") pygame.display.flip() camera = gp.check_result(gp.gp_camera_new()) err = gp.gp_camera_init(camera, context) if (err < gp.GP_OK): if err != gp.GP_ERROR_MODEL_NOT_FOUND: # some other error we can't handle here raise gp.GPhoto2Error(err) return # required configuration will depend on camera type! print('Checking camera config') # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera, context)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): raise gp.GPhoto2Error('Cannot preview raw images') # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config, context)) OK, txt = gp.gp_camera_get_summary(camera, context) infod = txToDict(txt.text) showText(AREA_PREVIEW, infod.get('Model')) info.camera = infod.get('Model') pygame.display.flip() return camera
def main(): # use Python logging logging.basicConfig( format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) callback_obj = gp.check_result(gp.use_python_logging()) # open camera connection camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera)) # get configuration tree config = gp.check_result(gp.gp_camera_get_config(camera)) # find the capture target config item capture_target = gp.check_result( gp.gp_widget_get_child_by_name(config, 'capturetarget')) # print current setting value = gp.check_result(gp.gp_widget_get_value(capture_target)) print('Current setting:', value) # print possible settings for n in range(gp.check_result(gp.gp_widget_count_choices(capture_target))): choice = gp.check_result(gp.gp_widget_get_choice(capture_target, n)) print('Choice:', n, choice) # clean up gp.check_result(gp.gp_camera_exit(camera)) return 0
def main(): logging.basicConfig( format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING) gp.check_result(gp.use_python_logging()) print('Establishing communication with the camera (wait few seconds)') camera = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(camera)) # required configuration will depend on camera type! config = gp.check_result(gp.gp_camera_get_config(camera)) print('Camera ready') # TODO get the list of 'gp_abilities_list_get_abilities' or something like that ? # --> find out how to set image format # --> also use it to set shutter and ISO def my_set(name, value): OK, widget = gp.gp_widget_get_child_by_name(config, name) if OK >= gp.GP_OK: widget_type = gp.check_result(gp.gp_widget_get_type(widget)) gp.check_result(gp.gp_widget_set_value(widget, value)) gp.check_result(gp.gp_camera_set_config(camera, config)) else: print("Error setting value %s for %s using widget %s" % (value, name, widget)) #my_set(name='imageformat', value='Large Fine JPEG') my_set(name='imageformat', value='RAW') my_set(name='shutterspeed', value='{}'.format(shutterspeed)) my_set(name='iso', value='{}'.format(iso)) # find the image format config item OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat') if OK >= gp.GP_OK: imgformat = gp.check_result(gp.gp_widget_get_value(image_format)) # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name(config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result(gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(camera, config)) # capture preview image (not saved to camera memory card) print('Capturing preview image') camera_file = gp.check_result(gp.gp_camera_capture_preview(camera)) file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file)) print('01----------', type(file_data),file_data) # display image data = memoryview(file_data) print('02----------', type(data), data) print(' ', len(data)) print(data[:10].tolist()) if 'raw' in imgformat.lower(): #rawimage = open(io.BytesIO(file_data)) ## FIXME raw format would be more accurate than JPEG, but AttributeError: '_io.BytesIO' object has no attribute 'encode' #xx from rawkit import raw #xx raw_image_process = raw.Raw(io.BytesIO(file_data)) #import rawpy #raw = rawpy.imread(bytesio) #rgb = raw.postprocess() #bytesio = io.BytesIO(file_data) #print('bytesio', bytesio) raw_file_name = 'image_logs/output_debayered_{}s_ISO{}_{}.cr2'.format(shutterspeed.replace('/','div'), iso, comment) gp.gp_file_save(camera_file, raw_file_name) # Note that - if Canon cameras are used - the dependency on rawkit can be replaced with a dedicated parser #https://codereview.stackexchange.com/questions/75374/cr2-raw-image-file-parser-in-python-3 from rawkit.raw import Raw from rawkit.options import interpolation raw_image = Raw(filename=raw_file_name) # 'bayer_data', 'close', 'color', 'color_description', 'color_filter_array', 'data', # 'image_unpacked', 'libraw', 'metadata', 'options', 'process', 'raw_image', 'save', # 'save_thumb', 'thumb_unpacked', 'thumbnail_to_buffer', 'to_buffer', 'unpack', 'unpack_thumb' #raw_image.options.interpolation = interpolation.linear # or "amaze", see https://rawkit.readthedocs.io/en/latest/api/rawkit.html raw_image.options.interpolation = interpolation.amaze # or "amaze", see https://rawkit.readthedocs.io/en/latest/api/rawkit.html raw_image.save("output-test.ppm") ## FIXME - saved ppm image has auto-normalized brightness, why? raw_image_process = raw_image.process() if raw_image_process is raw_image: print("they are identical") ## FIXME - npimage = np.array(raw_image.raw_image(include_margin=False)) # returns: 2D np. array #print('bayer_data', raw_image.bayer_data()) # ? #print('as_array', raw_image.as_array()) # does not exist, although documented?? #print(type(raw_image.to_buffer())) # Convert the image to an RGB buffer. Return type: bytearray #npimage = np.array(flat_list).reshape(4) print(npimage) ## gives 1-d array of values print(npimage.shape) ## gives 1-d array of values plt.imshow(npimage) plt.hist(npimage.flatten(), 4096) #plt.plot([200,500], [300,-100], lw=5, c='r') #plt.show() ## Save the raw pixels try: import cPickle as pickle except: import pickle print('retrieved image as numpy array with dimensions:', npimage.shape) #scipy.ndimage. print('', ) print('', ) print(npimage.shape) else: image = Image.open(io.BytesIO(file_data)) npimage = np.array(image) return npimage print('retrieved image as numpy array with dimensions:', npimage.shape) #image.show() plt.imshow(npimage) plt.plot([200,500], [300,-100], lw=5, c='k') plt.show() # TODO http://www.scipy-lectures.org/advanced/image_processing/#blurring-smoothing # display with polynomially curved paths, # linear convolve, # linearize along the paths, (possibly subtract background?) # generate polynomial x-axis # stitch smoothly gp.check_result(gp.gp_camera_exit(camera)) return 0
def get_value(): config = camera.get_config() param = 'manualfocusdrive' widget = gp.check_result(gp.gp_widget_get_child_by_name(config, param)) return gp.gp_widget_get_value(widget)
def __init__(self, memory_type=None): self.log = logger.add_module("Camera") if debug: self.log.info('debug mode without actual camera') self.last_image = False self.memory_type = memory_type self.last_image = None self.last_preview = None self.last_success = False self.error_count = 0 self.global_errors = 0 if debug: self.index = 0 else: # copy+paste: gphoto2/examples/preview-image.py gp.check_result(gp.use_python_logging()) self.cam = gp.check_result(gp.gp_camera_new()) gp.check_result(gp.gp_camera_init(self.cam)) # required configuration will depend on camera type! # get configuration tree config = gp.check_result(gp.gp_camera_get_config(self.cam)) self.available_memory = [] self.load_available_memory(True) #now comes the configuration which depends on the camera used if not debug: # find the image format config item # camera dependent - 'imageformat' is 'imagequality' on some OK, image_format = gp.gp_widget_get_child_by_name( config, 'imageformat') if OK >= gp.GP_OK: # get current setting value = gp.check_result(gp.gp_widget_get_value(image_format)) # make sure it's not raw if 'raw' in value.lower(): self.log.error('Cannot preview raw images') return 1 # find the capture size class config item # need to set this on my Canon 350d to get preview to work at all OK, capture_size_class = gp.gp_widget_get_child_by_name( config, 'capturesizeclass') if OK >= gp.GP_OK: # set value value = gp.check_result( gp.gp_widget_get_choice(capture_size_class, 2)) gp.check_result( gp.gp_widget_set_value(capture_size_class, value)) # set config gp.check_result(gp.gp_camera_set_config(self.cam, config)) # set storage folder+file - TODO: depending on memory_type capture_target = 1 for mem in self.available_memory: if mem[MEM_NAME] == memory_type: capture_target = mem[MEM_INDEX] break OK, capture_target_class = gp.gp_widget_get_child_by_name( config, 'capturetarget') if OK >= gp.GP_OK: # set value value = gp.check_result( gp.gp_widget_get_choice(capture_target_class, capture_target)) gp.check_result( gp.gp_widget_set_value(capture_target_class, value)) # set config gp.check_result(gp.gp_camera_set_config(self.cam, config))
def get_config_value(key, camera, context): config_widget = gp.check_result(gp.gp_camera_get_config(camera, context)) camera_widget_child = gp.check_result(gp.gp_widget_get_child_by_name(config_widget, key)) config_value = gp.check_result(gp.gp_widget_get_value(camera_widget_child)) return config_value
def get_parameter(self, parameter_name): error, parameter = gphoto2.gp_widget_get_child_by_name(self.config, parameter_name) if error < 0: print "Parameter does not exist" return return gphoto2.gp_widget_get_value(parameter)
def get_config_value (config, config_name): OK, config_child = gp.gp_widget_get_child_by_name(config,config_name) if OK >= gp.GP_OK: value = gp.check_result(gp.gp_widget_get_value(config_child)) return value
def get_config(self, config_name): config, widget = self._get_widget(config_name) return gp.check_result(gp.gp_widget_get_value(widget))