skip to navigation
skip to content

Not Logged In

gphoto2 0.6.0

Python interface to libgphoto2

Latest Version: 1.2.1

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

There are some Python helper classes to ease access to many of the low-level functions. This makes the package a bit more Pythonic, but you will still need to deal directly with the lower level at times.

Dependencies

Note that you need the “development headers” versions of libgphoto2 and Python. Most Linux distributions’ package managers have these, but the names vary. Look for libgphoto2-2-dev or libgphoto2-devel or something similar.

Installation and testing

There are several ways to install python-gphoto2, with varying levels of control over the installation process. Note that they all need SWIG and the other dependencies - there are no “binary” packages at present.

Install with pip

The easiest installation method is to use the pip command:

sudo pip install gphoto2

Install with git

To install the very latest 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

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

python setup.py build
sudo python setup.py install

Install a downloaded archive

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

tar xf python-gphoto2-gphoto2-0.3.2.tar.gz
cd python-gphoto2-gphoto2-0.3.2

As before, Python’s distutils are used to build and install python-gphoto2:

python setup.py build
sudo python setup.py install

Testing

Note

If you installed with pip the example files should be in /usr/share/python-gphoto2/examples or /usr/local/share/python-gphoto2/examples or somewhere similar (except for versions before 0.3.2, which didn’t install the examples at all).

Connect a digital camera to your computer, switch it on, and try one of the example programs:

python examples/camera-summary.py

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

Using python-gphoto2

The Python interface to libgphoto2 should allow you to do anything you could do in a C program. However, the project is quite young and 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.

Low-level interface

Using SWIG to generate the Python interfaces automatically means that every function in libgphoto2 should be available to Python. The pydoc command can be used to show basic information about a function:

jim@firefly ~/python-gphoto2 $ pydoc 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, list, context) -> int

    Parameters:
        camera: Camera *
        folder: char const *
        list: CameraList *
        context: GPContext *
jim@firefly ~/python-gphoto2 $

In general it is easier to use the C API documentation, but make sure you find the documentation for the version of libgphoto2 installed on your computer.

Note that there is one major difference between the Python and C APIs. C functions that use a pointer parameter to return a value (and often do some memory allocation) such as gp_camera_new() have Python equivalents that create the required pointer and return it in a list with the gphoto2 error code. 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 (since version 0.5.0). It is called automatically when the python camera object is deleted.

Some functions, such as gp_widget_get_value(), can return different types using a void * pointer in C. The Python interface includes type specific functions such as gp_widget_get_value_text().

Error checking

Most of the libgphoto2 functions return an integer to indicate success or failure. The Python interface includes a function to check these values and raise an exception if an error occurs. This function also unwraps lists such as that returned by gp_camera_new() in the example. Using this function the example becomes:

import gphoto2 as gp
camera = gp.check_result(gp.gp_camera_new())
...

Higher-level interface

There are some higher-level Python helper classes that handle object creation and deletion and make things even simpler. They provide simplified interfaces to many of the libgphoto2 functions, with shortened names and no need to pass shared data such as context. Here is a complete example program:

import gphoto2 as gp
with gp.Context() as context:
    with gp.Camera(context) as camera:
        camera.init()
        text = gp.CameraText()
        camera.get_summary(text)
        print('Summary')
        print('=======')
        print(text.text)
        camera.exit()

The higher level classes and the functions they wrap are as follows. Each class is sub-classed from a low-level object and can be passed to gphoto2 functions in place of the low-level object. (Prior to version 0.6.0 the low-level object was stored in a class attribute.)

Python class C function Python method Inherited low-level type
Camera gp_camera_xxx(camera, …, context) gp_camera_xxx(camera, …) xxx(…) Camera
CameraAbilitiesList gp_abilities_list_xxx(list, …) xxx(…) CameraAbilitiesList
CameraFile gp_file_xxx(file, …) xxx(…) CameraFile
CameraList gp_list_xxx(list, …) xxx(…) CameraList
CameraWidget gp_widget_xxx(widget, …) xxx(…) CameraWidget
Context gp_xxx(…, context) xxx(…) GPContext
PortInfoList gp_port_info_list_xxx(list, …) xxx(…) GPPortInfoList

Legalese

python-gphoto2 - Python interface to libgphoto2 http://github.com/jim-easterbrook/python-gphoto2 Copyright (C) 2014 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/.

 
File Type Py Version Uploaded on Size
gphoto2-0.6.0.tar.gz (md5) Source 2014-08-26 29KB
gphoto2-0.6.0.zip (md5) Source 2014-08-26 55KB
  • Downloads (All Versions):
  • 47 downloads in the last day
  • 547 downloads in the last week
  • 3793 downloads in the last month