pythonraspberry-pilibgphoto2

gphoto2 in Python vs command line; not able to capture preview in Python


I've worked with gphoto2 (2.5.20, on a RPi) before, and have been able to do the basics from the command line:

/usr/bin/gphoto2 --capture-image-and-download --filename=zzz.jpg --force-overwrite

/usr/bin/gphoto2 --capture-preview --filename=zzz.jpg --force-overwrite

The latter captures the preview image, which is a great deal smaller, and for some applications, is great; you can essentially make a webcam out of a DSLR.

However, after installing the gphoto2 library for python, this (which should be the equivalent) fails:

import gphoto2 as gp
camera = gp.Camera()
camera.init()
file_path = camera.capture(gp.GP_OPERATION_CAPTURE_PREVIEW)

with the message "gphoto2.GPhoto2Error: [-6] Unsupported operation"

If you choose

file_path = camera.capture(gp.GP_CAPTURE_IMAGE)

however, this works. Any reasons why we might get a working preview from the command line, and not in the Python version?

gp.__version__ returns 2.2.2 , and matches the latest release at https://github.com/jim-easterbrook/python-gphoto2


Solution

  • I drilled down into the libgphoto2 source. The cameras I have fall under the PTP world; I believe this is in camlibs/ptp2/library.c, where the camera_capture_preview function is located. The Python code fails with both a Nikon and Sony. There do seem to be two things in the code which point to possible issues; in the Nikon section, it will try 20 times to get the preview; in the Sony section, there are comments regarding timing. However, the command line works, so the problem may not have been there.

    I looked at the gphoto2 implementation in Python as well. The good news is that the code at https://github.com/jim-easterbrook/python-gphoto2/blob/master/examples/preview-image.py works, so if anyone wants to do this in Python, they can replace the

    file_path = camera.capture(gp.GP_OPERATION_CAPTURE_PREVIEW) line

    with the code there.

    Note that this worked on the rPi with gphoto2 (2.5.20); on an Ubuntu 16.04 LTS system, this fails (gphoto2 is 2.5.9).