Skip to main content

Python interface to libgphoto2

Project description

python-gphoto2 is a comprehensive Python interface (or binding) to libgphoto2. It is built using SWIG to automatically generate the interface code. This gives direct access to nearly all the libgphoto2 functions, but sometimes in a rather un-Pythonic manner.

Other Python bindings to libgphoto2 are available: piggyphoto uses ctypes (included in standard Python installations) to interface to the library. The gphoto2 source tree includes some Python bindings which also use ctypes. gphoto2-cffi uses cffi.

Installation (binary wheel)

Since python-gphoto2 version 2.3.1 “binary wheels” are provided for many Linux and MacOS computers. These include a recent version of the libgphoto2 libraries, and pre-built Python interface modules, which makes installation quick and easy. Use pip’s --only-binary option to install one of these wheels:

sudo pip3 install gphoto2 --only-binary :all:

If this fails it is most likely because none of the available wheels is compatible with your computer. In this case you must install the dependencies listed below before installing python-gphoto2. The installation process compiles and links the bindings with the libgphoto2 installed on your computer.

Raspberry Pi

Binary wheels for the Raspberry Pi are available from piwheels. You still need to install some system packages to use these:

sudo apt install libexif12 libgphoto2-6 libgphoto2-port12 libltdl7
sudo pip3 install gphoto2

Dependencies

In most cases you should use your operating system’s package manager to install these. Note that you need the “development headers” for libgphoto2 and Python. On some systems these are included in the base package, but on others they need to be installed separately. Look for libgphoto2-dev or libgphoto2-devel or something similar. Test your installation with the command pkg-config --cflags --libs libgphoto2 python3.

Installation (source)

There are several ways to install python-gphoto2, with varying levels of control over the installation process. By far the easiest is to use your operating system’s package manager, if you’re lucky enough to be using a system that has python-gphoto2 available. Note that this may not install the latest version.

Otherwise you can install it with pip, or by downloading an archive, or by getting the source from GitHub. Make sure that you install the dependencies first.

Install with pip

The easiest installation method is to use the pip command. Note the use of --no-binary to prevent installation from a binary wheel:

sudo pip3 install -v gphoto2 --no-binary :all:

This installation may take longer than you expect as the package’s modules are compiled during installation. The -v option increases pip’s verbosity so you can see that it’s doing something.

Install a downloaded archive

Visit PyPI and download one of the zip or tar.gz files, then extract it and change to the new directory. For example:

tar xf gphoto2-2.3.0.tar.gz
cd gphoto2-2.3.0

Python’s setuptools are used to build and install python-gphoto2:

pip3 wheel . -v
sudo pip3 install gphoto2-*.whl

Install from GitHub (SWIG required)

SWIG (http://swig.org/) should be installable via your operating system’s package manager. Note that this may be an older version of SWIG than the one used to generate the files on PyPI.

To install the current development version, use git to “clone” the GitHub repository, then change to the new directory:

git clone https://github.com/jim-easterbrook/python-gphoto2.git
cd python-gphoto2

As before, Python’s setuptools are used to build and install python-gphoto2, but now you have to run SWIG first to generate the files to be compiled. The developer directory includes a script to run SWIG:

python3 developer/build_swig.py system
pip3 wheel . -v
sudo pip3 install gphoto2-*.whl

See “running SWIG” below for more detail.

Testing

Since version 2.3.1 you can test your installation of python-gphoto2 by running it with the python3 -m gphoto2 command:

python3 -m gphoto2
python-gphoto2 version: 2.3.1
libgphoto2 version: ['2.5.27', 'standard camlibs (SKIPPING docupen)', 'gcc', 'ltdl', 'EXIF']
libgphoto2_port version: ['0.12.0', 'iolibs: disk ptpip serial usb1 usbdiskdirect usbscsi vusb', 'gcc', 'ltdl', 'EXIF', 'USB', 'serial without locking']
python-gphoto2 examples: /usr/lib64/python3.6/site-packages/gphoto2/examples

This shows the python-gphoto2 and libgphoto2 version numbers, and where the example programs have been installed. Connect a digital camera to your computer, switch it on, and try one of the example programs:

python3 /usr/lib64/python3.6/site-packages/gphoto2/examples/camera-summary.py

If this works then you’re ready to start using python-gphoto2.

Reinstalling

If you update or move your installation of libgphoto2 the Python gphoto2 package may fail to import one of the libgphoto2 shared object files. If this happens you need to rebuild and reinstall the Python gphoto2 package:

sudo pip3 install -v -U --force-reinstall gphoto2

if you installed with pip, or

rm -rf build
pip3 wheel . -v
sudo pip3 install --force-reinstall gphoto2-*.whl

if you installed from source.

Using python-gphoto2

The Python interface to libgphoto2 should allow you to do anything you could do in a C program. However, there are still bits missing and functions that cannot be called from Python. Let me know if you run into any problems.

The following paragraphs show how the Python interfaces differ from C. See the example programs for typical usage of the Python gphoto2 API.

“C” interface

These functions are as similar as possible to their libgphoto2 equivalents. Most of them return an error code which you must check.

Using SWIG to generate the Python interfaces automatically means that every function in libgphoto2 should be available to Python. You can show the documentation of a function with the pydoc command (or python -m pydoc if you installed gphoto2 with pip inside a virtual environment):

jim@firefly ~$ pydoc3 gphoto2.gp_camera_folder_list_files
Help on built-in function gp_camera_folder_list_files in gphoto2:

gphoto2.gp_camera_folder_list_files = gp_camera_folder_list_files(...)
    gp_camera_folder_list_files(camera, folder, context) -> int

    Parameters
    ----------
    camera: gphoto2.Camera
    folder: str
    context: gphoto2.Context (default=None)


    Lists the files in supplied `folder`.

    Parameters
    ----------
    * `camera` :
        a Camera
    * `folder` :
        a folder
    * `list` :
        a CameraList
    * `context` :
        a GPContext

    Returns
    -------
    a gphoto2 error code

    See also gphoto2.Camera.folder_list_files
jim@firefly ~$

The first part of this text is the function signature and parameter list generated by SWIG. (Note that context is optional - it’s only needed if you need the callback functions that can be associated with a context.) The rest of the text is copied from the “doxygen” format documentation in the C source code. (The online API documentation shows how it is intended to look.) Note that this includes a list parameter that is not in the function signature. In C this is an “output” parameter, a concept that doesn’t really exist in Python. The Python version of gp_camera_folder_list_files returns a sequence containing the integer error code and the list value.

Most of the libgphoto2 functions that use pointer parameters to return values in the C API have been adapted like this in the Python API. (Unfortunately I’ve not found a way to persuade SWIG to include this extra return value in the documentation. You should use pydoc to check the actual parameters expected by the Python function.)

For example, the C code:

#include "gphoto2.h"
int error;
Camera *camera;
error = gp_camera_new(&camera);
...
error = gp_camera_unref(camera);

has this Python equivalent:

import gphoto2 as gp
error, camera = gp.gp_camera_new()
...

Note that the gp_camera_unref() call is not needed. It is called automatically when the Python camera object is deleted.

Here is a complete example program (without any error checking):

import gphoto2 as gp
error, camera = gp.gp_camera_new()
error = gp.gp_camera_init(camera)
error, text = gp.gp_camera_get_summary(camera)
print('Summary')
print('=======')
print(text.text)
error = gp.gp_camera_exit(camera)

“Object oriented” interface

This is the preferred way to use libgphoto2 from Python. Most of the libgphoto2 functions have been added as methods of the appropriate GPhoto2 object. This allows GPhoto2 to be used in a more “Pythonic” style. For example, gp.gp_camera_init(camera) can be replaced by camera.init(). These methods also include error checking. If an error occurs they raise a Python GPhoto2Error exception.

The example program can be re-written as follows:

import gphoto2 as gp
camera = gp.Camera()
camera.init()
text = camera.get_summary()
print('Summary')
print('=======')
print(str(text))
camera.exit()

No additional error checking is required.

Error checking

Most of the libgphoto2 functions return an integer to indicate success or failure. The Python interface includes a check_result() function to check these values and raise a GPhoto2Error exception if an error occurs.

This function also removes the error code from lists such as that returned by gp_camera_new() in the example. Using this function the earlier example becomes:

import gphoto2 as gp
camera = gp.check_result(gp.gp_camera_new())
gp.check_result(gp.gp_camera_init(camera))
text = gp.check_result(gp.gp_camera_get_summary(camera))
print('Summary')
print('=======')
print(text.text)
gp.check_result(gp.gp_camera_exit(camera))

There may be some circumstances where you don’t want an exception to be raised when some errors occur. You can “fine tune” the behaviour of the check_result() function by adjusting the error_severity variable:

import gphoto2 as gp
gp.error_severity[gp.GP_ERROR] = logging.WARNING
...

In this case a warning message will be logged (using Python’s standard logging module) but no exception will be raised when a GP_ERROR error occurs. However, this is a “blanket” approach that treats all GP_ERROR errors the same. It is better to test for particular error conditions after particular operations, as described below.

The GPhoto2Error exception object has two attributes that may be useful in an exception handler. GPhoto2Error.code stores the integer error generated by the library function and GPhoto2Error.string stores the corresponding error message.

For example, to wait for a user to connect a camera you could do something like this:

import gphoto2 as gp
...
print('Please connect and switch on your camera')
while True:
    try:
        camera.init()
    except gp.GPhoto2Error as ex:
        if ex.code == gp.GP_ERROR_MODEL_NOT_FOUND:
            # no camera, try again in 2 seconds
            time.sleep(2)
            continue
        # some other error we can't handle here
        raise
    # operation completed successfully so exit loop
    break
# continue with rest of program
...

When just calling a single function like this, it’s probably easier to test the error value directly instead of using Python exceptions:

import gphoto2 as gp
...
print('Please connect and switch on your camera')
while True:
    error = gp.gp_camera_init(camera)
    if error >= gp.GP_OK:
        # operation completed successfully so exit loop
        break
    if error != gp.GP_ERROR_MODEL_NOT_FOUND:
        # some other error we can't handle here
        raise gp.GPhoto2Error(error)
    # no camera, try again in 2 seconds
    time.sleep(2)
# continue with rest of program
...

Logging

The libgphoto2 library includes functions (such as gp_log()) to output messages from its various functions. These messages are mostly used for debugging purposes, and it can be helpful to see them when using libgphoto2 from Python. The Python interface includes a use_python_logging() function to connect libgphoto2 logging to the standard Python logging system. If you want to see the messages you should call use_python_logging() near the start of your program, as shown in the examples. In normal use you probably don’t want to see these messages (libgphoto2 is rather verbose) so this could be controlled by a “verbose” or “debug” option in your application.

The libgphoto2 logging messages have four possible severity levels, each of which is mapped to a suitable Python logging severity. You can override this mapping by passing your own to use_python_logging():

import logging
import gphoto2 as gp
...
callback_obj = gp.check_result(gp.use_python_logging(mapping={
    gp.GP_LOG_ERROR   : logging.INFO,
    gp.GP_LOG_DEBUG   : logging.DEBUG,
    gp.GP_LOG_VERBOSE : logging.DEBUG - 3,
    gp.GP_LOG_DATA    : logging.DEBUG - 6}))
...

If you prefer to use your own logging system you can define a logging callback function in Python. The function must take 3 or 4 parameters: level, domain, string and an optional data. The data parameter allows you to pass some user data to your callback function (e.g. to log which thread an error occurred in): The callback function is installed with gp_log_add_func:

import gphoto2 as gp
...
def callback(level, domain, string, data=None):
    print('Callback: level =', level, ', domain =', domain, ', string =', string, 'data =', data)
...
callback_obj1 = gp.check_result(gp.gp_log_add_func(gp.GP_LOG_VERBOSE, callback))
callback_obj2 = gp.check_result(gp.gp_log_add_func(gp.GP_LOG_VERBOSE, callback, 123))
...

What to do if you have a problem

If you find a problem in the Python gphoto2 interface (e.g. a segfault, a missing function, or a function without a usable return value) then please report it on the GitHub “issues” page (https://github.com/jim-easterbrook/python-gphoto2/issues) or email jim@jim-easterbrook.me.uk.

If your problem is more general, e.g. difficulty with capturing multiple images, then try doing what you want to do with the gphoto2 command line program. If the problem persists then it might be worth asking on the gphoto-user mailing list. Another reader of the mailing list may have the same camera model and already know what to do.

Notes on some gphoto2 functions

gp_log_add_func / use_python_logging

Since python-gphoto2 version 2.0.0 these functions return a sequence containing an error code and an object storing details of the callback. The callback is automatically uninstalled when this object is deleted.

In earlier versions of python-gphoto2 these functions return an integer id that must be passed to gp_log_remove_func to uninstall the callback.

gp_context_set_idle_func / gp_context_set_progress_funcs / etc.

These functions are only usable since python-gphoto2 version 1.9.0. They return a Python object which your program must store until the callback(s) are no longer required. Deleting the returned object cancels the callback(s), so there is no need to do this yourself. See the context_with_callbacks.py example for a convenient way to do this.

gp_file_get_data_and_size / CameraFile.get_data_and_size

Since python-gphoto2 version 1.2.0 these functions return a FileData object that supports the buffer protocol. The data can be made accessible to Python by using a memoryview object. This allows the data to be used without copying. See the copy-data.py example for typical usage.

In earlier versions of python-gphoto2 these functions returned a bytes object containing a copy of the data in the CameraFile object.

gp_file_set_data_and_size / CameraFile.set_data_and_size

Since python-gphoto2 version 2.1.0 these functions accept any bytes-like object. In earlier versions of python-gphoto2 these functions required a string and its length, and didn’t work correctly anyway.

gp_file_append / CameraFile.append

Since python-gphoto2 version 2.1.0 these functions accept any bytes-like object. In earlier versions of python-gphoto2 these functions required a string and its length.

gp_camera_file_read / Camera.file_read

The buf parameter can be any Python object that exposes a writeable buffer interface. This allows you to read a file directly into a Python object without additional copying. See the copy-chunks.py example which uses memoryview to expose a bytearray.

gp_camera_wait_for_event / Camera.wait_for_event

These functions return both the event type and the event data. The data you get depends on the type. GP_EVENT_FILE_ADDED and GP_EVENT_FOLDER_ADDED events return a CameraFilePath, others return None or a text string.

Running SWIG

SWIG is used to convert the .i interface definition files in src/gphoto2 to .py and .c files. These are then compiled to build the Python interface to libgphoto2. The files downloaded from PyPI include the SWIG generated files, but you may wish to regenerate them by running SWIG again (e.g. to test a new version of SWIG or of libgphoto2). You will also need to run SWIG if you have downloaded the python-gphoto2 sources from GitHub instead of using PyPI.

The developer directory includes a script to run SWIG. It requires one parameter: the version to be swigged. This can be system or a number triplet, e.g. 2.5.27:

python3 developer/build_swig.py system

This builds the interface for the version of libgphoto2 installed on your computer. The interface files are created in directories with names like src/swig-gp2.5.18. This naming scheme allows for different versions of libgphoto2. The most appropriate version is chosen when the interface is built.

To build interfaces for multiple versions of libgphoto2 (e.g. v2.5.10 as well as v2.5.0) you need to put those versions’ source files in your working directory and then run python developer/build_swig.py again, specifying the version:

python3 developer/build_swig.py 2.5.10

More information about this is in the file developer/README.txt.

Licence

python-gphoto2 - Python interface to libgphoto2
Copyright (C) 2014-22 Jim Easterbrook jim@jim-easterbrook.me.uk

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gphoto2-2.3.3.tar.gz (555.4 kB view hashes)

Uploaded Source

Built Distributions

gphoto2-2.3.3-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

gphoto2-2.3.3-cp310-cp310-macosx_10_9_x86_64.whl (6.1 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

gphoto2-2.3.3-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

gphoto2-2.3.3-cp39-cp39-macosx_10_9_x86_64.whl (6.1 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

gphoto2-2.3.3-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.9 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

gphoto2-2.3.3-cp38-cp38-macosx_10_9_x86_64.whl (6.1 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

gphoto2-2.3.3-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

gphoto2-2.3.3-cp37-cp37m-macosx_10_9_x86_64.whl (6.1 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

gphoto2-2.3.3-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

gphoto2-2.3.3-cp36-cp36m-macosx_10_9_x86_64.whl (6.1 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page