jim-easterbrook / python-gphoto2

Python interface to libgphoto2
GNU Lesser General Public License v3.0
357 stars 59 forks source link

Extend documentation: how to set zoom #129

Closed gbrault closed 2 years ago

gbrault commented 2 years ago

Proposition to document zoom positioning

gphoto2 command:

gphoto2 --set-config zoom=150

With python-gphoto2

import gphoto2 as gp
camera = gp.Camera()
camera.init()
# get configuration tree
camera_config = camera.get_config()
zoom = camera_config.get_child_by_name('zoom')
zoom.set_value('150')
camera.set_config(camera_config)
camera.exit()

Just wanted to make sure this is correct?

jim-easterbrook commented 2 years ago

The overall structure looks correct, but I've no idea if 150 should be a string or a number or even a choice from a list. This sort of thing is very camera dependent.

jim-easterbrook commented 2 years ago

There's also the get_single_config alternative to consider.

gbrault commented 2 years ago
import gphoto2 as gp
# !gphoto2 --set-config zoom=150
# !gphoto2 --get-config zoom
camera = gp.Camera()
camera.init()
zoom = camera.get_single_config('zoom')
zoom.set_value('150')
camera.set_single_config('zoom',zoom)
zoom.get_value() # don't seem to read it from the camera
camera.exit()

@jim-easterbrook is that what you meant by "There's also the get_single_config alternative to consider."

Just one question when I do gphoto2 --get-config zoom it actually reads the value from the camera, which allows checking final position is reached. but zoom.get_value() seems not doing so?

What do you recommand?

Thanks for your help!

gbrault commented 2 years ago

For the remark concerning the value to provide, I have this function which provides a full overview of what the camera is telling about config:

def configTree(inode,onode):
    """Builds the Configuration Tree"""
    if not hasattr(onode,"children") and inode.count_children()!=0:
        onode.update({"children": []})
    if not hasattr(onode,"name"):
        onode.update({"name": inode.get_name()})
    if not hasattr(onode,"label"):
        onode.update({"label": inode.get_label()})
    try:
        if inode.count_choices() != 0:
            choices = inode.get_choices()
            onode.update({"choices": choices})
    except:
        pass
    try:
        value = inode.get_value()
        onode.update({"value": value})
    except:
        pass
    if inode.count_children() == 0:
        return
    for child in inode.get_children():
        onode['children'].append({})
        configTree(child,onode['children'][-1])

you call it

camera_config = camera.get_config()
onode = {}
configTree(camera_config,onode)

and you can have a readable output with

json.dumps(onode, indent=4)

I would assume it would provide choice for zoom setting if this is a set of choices...

jim-easterbrook commented 2 years ago

zoom.set_value('150') camera.set_single_config('zoom',zoom) zoom.get_value() # don't seem to read it from the camera

No, you're reading the value of your local variable zoom. You need to call get_single_config to get the value from the camera.

I don't have any particular recommendation. The get/set single value stuff is useful when you only want to set one or two values, getting the whole tree is useful if you want to do more. Some cameras may be a lot quicker to do single configs, other cameras not.

I don't have much experience with libgphoto2, I wrote the interface to allow downloading of photos and don't have much interest in doing other stuff.