Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Use Your Digital Camera with Linux

by Robert Bernier
01/06/2005

For years, I've wanted a digital camera. Both my father and brother had one; why not me? Then one day it happened: I had been working a new contract for about a month when the boss said it was time we met. He was in New York City and here I was in Ottawa. Coincidentally, NYC-bug (pronounced "nice bug") had invited my girlfriend to go see Eric Allman and Kirk McKusick talk at a BSD user group meeting. Of course, being the natural born tourist that I am, I had to buy one immediately.

I purchased a Kodak EasyShare CX7330. It came with its own built-in 16MB of memory, but I bought an additional memory card of 256MB, which gave me enough space for several hundred photos. Not only does it take great still shots, but I can also record up to 18 minutes' worth of full-motion MOVs complete with sound (albeit not quite not as good as a dedicated video camera, but still a lot of fun). It comes bundled with a CD of Microsoft software. I made space for some cable, batteries, and a small tripod in the camera case as soon as I threw away the manual and CD.

The Software

This camera, like most, comes with a USB plug, so interfacing with a Linux box is easy. Camera applications are as about as common as snow (depending upon where you live, of course). However, for the most part they depend upon the libgphoto2 libraries.

After an evening of research, I determined I needed the following software to make the most of my camera:

The Installation

Be warned, it will take some time to install these tools properly on your machine unless your distribution handles dependencies in an intelligent manner.

I installed everything I needed on my Debian machine with the following commands:

% apt-get install imagemagick
% apt-get install fbi
% apt-get install gphoto2
% apt-get install digikam

In theory, it should have been an easy exercise persuading the camera and PC to communicate with each other. I wasted a lot of time before realizing that my Debian implementation of libgphoto2 prevents anybody from accessing the camera without explicit permission. I solved the problem by editing a file called usbcam in the directory /etc/hotplug/usb. The configuration file allows any user who belongs to the group usb access to the camera. Your own Linux distribution's version should include a similar script; otherwise, you'll need to read the documentation that comes with the library, especially if you decide to compile and install the source code yourself.

The first thing you want to do before beginning any graphics manipulation is to make sure the PC sees the camera: all you need to do is plug the camera into the USB port and turn it on. Ain't life grand? Try running as root if you have problems connecting to the camera and verify if it is indeed a question of permissions.

Manipulating Your Photos with gphoto2

It's an oxymoron to use command-line utilities to manipulate graphics. On the other hand, not only is it faster than the traditional GUI approach, but you can also create scripts that can handle and process your photographs in a very quick and efficient manner, thus creating a sophisticated post-processing environment with very little effort.

Typing gphoto2 --usage provides a brief listing of gphoto2's options. Typing gphoto2 --help returns the extended gphoto2 help output. Don't forget to read the gphoto2 man page, which provides an excellent reference.

Connecting the Camera

First things first: verify that you can see the camera. You should turn it on after plugging the USB cable into both camera and PC. To detect the camera, use the command:

$ gphoto2 --auto-detect:

Loading camera drivers from '/usr/l...  |                    \   2.0%
Loading camera drivers from '/usr/l...  |-                   |   4.0%
.
.
.
Loading camera drivers from '/usr/l...  |---------------------|  98.0%
Loading camera drivers from '/usr/l...  |---------------------|100.0%  
Model                         Port
----------------------------------------------------------
USB PTP Class Camera          usb:

In this case, the application has recognized my camera as a USB PTP-class camera connected on the USB port. If you don't see a driver, then the gphoto2 library doesn't see the camera. This is a question of permissions--so that running as root doesn't produce this problem--or else gphoto2 doesn't know which driver to use. You can list all the supported cameras with gphoto2 --list-cameras | less. Use the --camera switch to use a driver explicitly for subsequent commands.

Defining Your Camera's Abilities

Each camera comes with a set of capabilities. gphoto2 can list them:

$ gphoto2 -a
.
.
Abilities for camera            : USB PTP Class Camera
Serial port support             : no
USB support                     : yes
Capture choices                 :
                                : Capture not supported by the driver
Configuration support           : yes
Delete files on camera support   : yes
File preview (thumbnail) support : yes
File upload support             : yes

Thumbnails

The next thing you'll want to do is list the available photographs. Use the command gphoto2 -L, which returns the file and its path. Yes that's right; I said a path--just like the kind you find on your own hard drive filesystem.

The command gphoto2 -l (with a lowercase l) lists only the folders:

There are 2 folders in folder '/':
 - store_00010001
 - store_00020001
There are 2 folders in folder '/store_00010001':
 - DCIM
 - MISC
There is one folder in folder '/store_00010001/DCIM':
 - 100K7330
There are no folders in folder '/store_00010001/DCIM/100K7330'.
There are no folders in folder '/store_00010001/MISC'.
There are no folders in folder '/store_00020001'.

You can save a lot of time by first downloading the thumbnails:

$ gphoto2 --get-all-thumbnails

Related Reading

Linux Cookbook
By Carla Schroder

Viewing

To view images without booting into X, use the application fbi. For example, fbi * will allow you to page through the entire collection of thumbnails that you downloaded in your current directory from a text console.

Note: fbi functions only when the kernel has frame buffer support compiled in or as a module. Most distributions enable this. However, the frame buffer image viewer will not work in a pseudo terminal such as an xterm session.

You'll quickly discover that your system is significantly faster and more responsive when using fbi than it is when running a comparable viewer in a graphics environment under X.

Deleting

The nice thing about thumbnails is you can decide which images to download and which not to. You also have the ability to delete your photographs from the camera. For example, gphoto2 -d 3 deletes the third photograph in a camera's memory under its root directory.

To remove all the photographs recursively from a camera, run gphoto2 -DR.

If you find yourself having difficulties deleting images, the problem may be a path issue. For example, if I want to delete all my photos in the directory path /store_00010001/DCIM/100K7330, the delete command is:

$ gphoto2 -D --folder /store_00010001/DCIM/100K7330

Downloading

Download all the images with the command gphoto2 -P. On the other hand, gphoto2 -p 1,3-5,7 (with a lowercase p) will download a comma-separated list of files that includes image numbers 1, 3, 4, 5, and 7. Note that this capacity for ranges also exists for thumbnails and deletions.

Creating Directories

The manual says that my camera has the ability to store its collections of photographs as albums. Assuming I have an existing folder in my camera, I can therefore create a subfolder named TEST beneath the path /store_0001000:

$ gphoto2 -m TEST --folder /store_00010001

Here's the resulting listing:

$ gphoto2 -L
There are no files in folder '/'.
There are no files in folder '/store_00010001'.
There are no files in folder '/store_00010001/TEST'.

Uploading

The idea that I could upload photos of my own choosing was an unexpected bonus. Why? Can you think of a better way to transport copies of your favorite photos? As it so happens, I have another reason: my Kodak CX7330 plugs directly into a television, so I can have the whole family sit in front of the TV and watch a carefully crafted slide show while I go on and on and on and on ... describing in the smallest detail the significance of each and every one of those hundreds of photographs.

Uploading graphics into my camera requires that I explicitly describe the full file path. In this case I want to upload the JPEG file named test.jpg into my folder /store_00010001/:

$ gphoto2 -u test.jpg --folder /store_00010001/

Going Further

gphoto2 has an interactive shell that I have found very handy. It emulates navigating through the camera much like you would navigate a filesystem. Invoke the shell by typing gphoto2 --shell.

Another cool fact is that you can store files other than photographs, including nongraphics media such as text, zipped files, and encrypted binaries. It's basically a portable hard drive.

As an experiment, I succeeded in storing a copy of Damn Small Linux on my camera. Now all that remains is to figure out how to boot off the camera's memory on the PC.

There's a lot I haven't talked about, so you should make a point of reading all of gphoto's documented features in the man pages.

digiKam

Another great program is digiKam, the KDE-based photo management application. The initial configuration takes only a moment to run and thereafter will connect to the camera instantly. The application automatically downloads the thumbnails for each image, using an icon to represent nongraphics files. The graphics viewer in digiKam is quite nice in that it can view the entire graphic within the confines of your screen. You can also zoom in or out of each image.

Recently, I've taken the habit of creating web galleries on my website for the family. It's quick work with KDE's konqueror. I just type Ctrl-I to bring up the web gallery dialog box. I can then choose the number of graphics across the page as well as the type of information to present for each image, such as filename, size, and dimension. I can also set the kind of font to use on the page.

What if the images are too large to upload to the website? No problem; this next command takes advantage of the convert utility from the ImageMagick graphics utility suite. It makes a copy and resizes all the images down to a width of 640 pixels in the directory named reduced:

$ find . -exec convert -resize 640x {} ../reduced/{} \;

Caveats

Make sure you check the full path in the event you have problems deleting or uploading images when using gphoto2. When in doubt, use the shell mode or the digiKam client instead.

It's an unfortunate fact of life, but we still live in a proprietary world that limits our abilities when we choose the path of open source. My Kodak camera could probably work as a webcam; however, the moment it connects to the USB port, it disables its viewing function. Another problem I had was creating the folders--even though it has utilities for expressly this purpose on the Windows platform.

My Trip to NYC

For those with time to kill, take a look at my pictures from my New York trip. They're not the most exciting, but I still had fun.

Robert Bernier is the PostgreSQL business intelligence analyst for SRA America, a subsidiary of Software Research America (SRA).


Return to the Linux DevCenter.

Copyright © 2009 O'Reilly Media, Inc.