canon Package

canon Package

Use old Canon cameras with Python.

exception canon.__init__.CanonError

Bases: exceptions.Exception

camera Module

class canon.camera.Camera(device)

Bases: object

Camera objects are the intended API endpoint. Cameras have two public properties which provide most of the interesting functionality:

  • storage for filesystem operations, and
  • capture for taking pictures.
abilities

http://www.graphics.cornell.edu/~westin/canon/ch03s25.html

camera_time

Get the current date and time stored and ticking on the camera.

capture

Do remote captures.

cleanup()
firmware_version
get_abilities()
identify() -> (model, owner, version)
initialize(force=False)
model

Camera model string as stored on it.

on_ac

True if the camera is not running on battery power.

owner

The owner of this camera, writable.

ready
storage

Access the camera filesystem.

canon.camera.find(idVendor=1193, idProduct=12398)

Find a canon camera on some usb bus, possibly.

Pass in idProduct for your particular model, default values are for a PowerShot G3.

capture Module

class canon.capture.CanonCapture(usb)

Bases: object

Manage taking pictures via USB. The whole point.

shooting modes: P, M, Av, Tv

Metering Mode 0 White Balance 1

whaat?

Lens Focal Length Drive Mode Owner’s Name

active
aperture
capture(inst, *args, **kw)
exposure_compensation
flash
flash_compensation
get_capture_settings()
image_mode
initialize(force=False)
iso_speed
settings
shooting_mode
shutter_speed
start(force=False)
stop()
transfer_mode

XXX: Remote Capture 2.5.7 seems to do this in a different manner.

white_balance
class canon.capture.CaptureSettings

Bases: canon.util.Bitfield

APERTURE_F10 = 61
APERTURE_F11 = 64
APERTURE_F13 = 67
APERTURE_F14 = 69
APERTURE_F16 = 72
APERTURE_F18 = 75
APERTURE_F1_2 = 13
APERTURE_F1_4 = 16
APERTURE_F1_6 = 19
APERTURE_F1_8 = 21
APERTURE_F20 = 77
APERTURE_F22 = 80
APERTURE_F25 = 83
APERTURE_F29 = 85
APERTURE_F2_0 = 24
APERTURE_F2_2 = 27
APERTURE_F2_5 = 29
APERTURE_F2_8 = 32
APERTURE_F32 = 88
APERTURE_F3_2 = 35
APERTURE_F3_5 = 37
APERTURE_F4_0 = 40
APERTURE_F4_5 = 43
APERTURE_F5_0 = 45
APERTURE_F5_6 = 48
APERTURE_F6_3 = 51
APERTURE_F7_1 = 53
APERTURE_F8 = 56
APERTURE_F9 = 59
AUTO_FOCUS_AI_FOCUS = 2
AUTO_FOCUS_AI_SERVO = 1
AUTO_FOCUS_ONE_SHOT = 0
BEEP_OFF = 0
BEEP_ON = 1
EXPOSURE_MINUS_0_1_2 = 252
EXPOSURE_MINUS_0_1_3 = 253
EXPOSURE_MINUS_0_2_3 = 251
EXPOSURE_MINUS_1 = 248
EXPOSURE_MINUS_1_1_2 = 244
EXPOSURE_MINUS_1_1_3 = 245
EXPOSURE_MINUS_1_2_3 = 243
EXPOSURE_MINUS_2 = 240
EXPOSURE_PLUS_0_1_2 = 4
EXPOSURE_PLUS_0_1_3 = 3
EXPOSURE_PLUS_0_2_3 = 5
EXPOSURE_PLUS_1 = 8
EXPOSURE_PLUS_1_ = 13
EXPOSURE_PLUS_1_1_2 = 12
EXPOSURE_PLUS_1_1_3 = 11
EXPOSURE_PLUS_2 = 16
EXPOSURE_ZERO = 0
FLASH_MODE_AUTO = 2
FLASH_MODE_OFF = 0
FLASH_MODE_ON = 1
IMAGE_FORMAT_LARGE_FINE_JPEG = (3, 1, 0)
IMAGE_FORMAT_LARGE_NORMAL_JPEG = (2, 1, 0)
IMAGE_FORMAT_MEDIUM_FINE_JPEG = (3, 1, 1)
IMAGE_FORMAT_MEDIUM_NORMAL_JPEG = (2, 1, 1)
IMAGE_FORMAT_RAW = (4, 2, 0)
IMAGE_FORMAT_RAW_AND_LARGE_FINE_JPEG = (52, 18, 0)
IMAGE_FORMAT_RAW_AND_LARGE_NORMAL_JPEG = (36, 18, 0)
IMAGE_FORMAT_RAW_AND_MEDIUM_FINE_JPEG = (52, 18, 16)
IMAGE_FORMAT_RAW_AND_MEDIUM_NORMAL_JPEG = (36, 18, 16)
IMAGE_FORMAT_RAW_AND_SMALL_FINE_JPEG = (52, 18, 32)
IMAGE_FORMAT_RAW_AND_SMALL_NORMAL_JPEG = (36, 18, 32)
IMAGE_FORMAT_SMALL_FINE_JPEG = (3, 1, 2)
IMAGE_FORMAT_SMALL_NORMAL_JPEG = (2, 1, 2)
ISO_100 = 72
ISO_1000 = 99
ISO_125 = 75
ISO_1250 = 101
ISO_160 = 77
ISO_1600 = 104
ISO_200 = 80
ISO_250 = 83
ISO_320 = 85
ISO_3200 = 112
ISO_400 = 88
ISO_50 = 64
ISO_500 = 91
ISO_640 = 93
ISO_800 = 96
MANUAL_FOCUS = 3
SHUTTER_SPEED_0_3_SEC = 69
SHUTTER_SPEED_0_4_SEC = 67
SHUTTER_SPEED_0_5_SEC = 64
SHUTTER_SPEED_0_6_SEC = 61
SHUTTER_SPEED_0_8_SEC = 59
SHUTTER_SPEED_10_SEC = 29
SHUTTER_SPEED_13_SEC = 27
SHUTTER_SPEED_15_SEC = 24
SHUTTER_SPEED_1_10 = 83
SHUTTER_SPEED_1_100 = 109
SHUTTER_SPEED_1_1000 = 136
SHUTTER_SPEED_1_125 = 112
SHUTTER_SPEED_1_1250 = 139
SHUTTER_SPEED_1_13 = 85
SHUTTER_SPEED_1_15 = 88
SHUTTER_SPEED_1_160 = 115
SHUTTER_SPEED_1_1600 = 141
SHUTTER_SPEED_1_20 = 91
SHUTTER_SPEED_1_200 = 117
SHUTTER_SPEED_1_2000 = 144
SHUTTER_SPEED_1_25 = 93
SHUTTER_SPEED_1_250 = 120
SHUTTER_SPEED_1_2500 = 147
SHUTTER_SPEED_1_30 = 96
SHUTTER_SPEED_1_320 = 123
SHUTTER_SPEED_1_3200 = 149
SHUTTER_SPEED_1_3_SEC = 53
SHUTTER_SPEED_1_4 = 72
SHUTTER_SPEED_1_40 = 99
SHUTTER_SPEED_1_400 = 125
SHUTTER_SPEED_1_4000 = 152
SHUTTER_SPEED_1_5 = 75
SHUTTER_SPEED_1_50 = 101
SHUTTER_SPEED_1_500 = 128
SHUTTER_SPEED_1_5000 = 154
SHUTTER_SPEED_1_6 = 77
SHUTTER_SPEED_1_60 = 104
SHUTTER_SPEED_1_640 = 131
SHUTTER_SPEED_1_6400 = 157
SHUTTER_SPEED_1_6_SEC = 50
SHUTTER_SPEED_1_8 = 80
SHUTTER_SPEED_1_80 = 107
SHUTTER_SPEED_1_800 = 133
SHUTTER_SPEED_1_8000 = 160
SHUTTER_SPEED_1_SEC = 56
SHUTTER_SPEED_20_SEC = 21
SHUTTER_SPEED_25_SEC = 19
SHUTTER_SPEED_2_5_SEC = 45
SHUTTER_SPEED_2_SEC = 48
SHUTTER_SPEED_30_SEC = 16
SHUTTER_SPEED_3_2_SEC = 43
SHUTTER_SPEED_4_SEC = 40
SHUTTER_SPEED_5_SEC = 37
SHUTTER_SPEED_6_SEC = 35
SHUTTER_SPEED_8_SEC = 32
SHUTTER_SPEED_BULB = 4
aperture

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

beep

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

exposure_bias

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

flash

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

focus_mode

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

image_format

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

iso

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

macro
shooting_mode

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

shutter_speed

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

class canon.capture.ExitRemoteControlCmd(payload=None, serial=None)

Bases: canon.capture.RemoteControlCommand

subcmd = 1
subcmd_resplen = 28
class canon.capture.GetCaptureSettingsCmd

Bases: canon.capture.GetParamsCmd

class canon.capture.GetParamsCmd(payload=None, serial=None)

Bases: canon.capture.RemoteControlCommand

subcmd = 10
subcmd_resplen = 76
class canon.capture.InitRemoteControlCmd(payload=None, serial=None)

Bases: canon.capture.RemoteControlCommand

subcmd = 0
subcmd_resplen = 28
class canon.capture.RemoteControlCommand(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

cmd1 = 19
cmd2 = 18
cmd3 = 513
first_chunk_size = 64
resplen
subcmd = None
subcmd_resplen = None
class canon.capture.SetCaptureSettingsCmd(settings)

Bases: canon.capture.SetParamsCmd

class canon.capture.SetParamsCmd(payload=None, serial=None)

Bases: canon.capture.RemoteControlCommand

subcmd = 7
subcmd_resplen = 28
class canon.capture.SetTransferModeCmd(transfermode)

Bases: canon.capture.RemoteControlCommand

subcmd = 9
subcmd_resplen = 28
class canon.capture.SetZoomPositionCmd(payload=None, serial=None)

Bases: canon.capture.RemoteControlCommand

subcmd = 12
subcmd_resplen = 28
class canon.capture.ShutterReleaseCmd(payload=None, serial=None)

Bases: canon.capture.RemoteControlCommand

subcmd = 4
subcmd_resplen = 28
class canon.capture.TransferMode

Bases: canon.util.Bitfield

FULL_TO_DRIVE = 8
FULL_TO_PC = 2
THUMB_TO_DRIVE = 4
THUMB_TO_PC = 1
drive

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

pc

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

canon.capture.require_active_capture(func)

commands Module

class canon.commands.CheckACPowerCmd(payload=None, serial=None)

Bases: canon.commands.GetPowerStatusCmd

class canon.commands.Command(payload=None, serial=None)

Bases: object

A USB camera command.

Subclasses of Command are concrete commands to be executed on the camera. Instances thereof can run themselves on a CanonUSB instance and should each implement some sort of response parsing.

cmd1, cmd2 and cmd3 define the command to be executed.

All of the following properties need to be set for a command class:

cmd1 is a command code.

cmd2 is 0x11 for storage and 0x12 for control commands.

cmd3 is 0x201 for fixed-response-length commands and 0x202 for variable length.

MAX_CHUNK_SIZE = 5120
classmethod chunk_sizes(bytes_to_read)

Yield chunk sizes to read.

cmd1 = None
cmd2 = None
cmd3 = None
command_header
execute(usb)
first_chunk_size

Return the length of the first chunk of data to be read.

This differs for different commands, but should always be at least 0x40.

static from_command_packet(data)

Return a command instance from a command packet.

This is used for parsing sniffed USB traffic.

classmethod is_complete_command()
name

Simply the class name for convenient access.

classmethod next_chunk_size(remaining)

Calculate the size of the next chunk to read.

See http://www.graphics.cornell.edu/~westin/canon/ch03s02.html#par.VarXfers

payload
response_header
response_length
response_status
serial

Return the serial id of this command.

Generate one if not given in the constructor.

class canon.commands.CommandMeta

Bases: type

class canon.commands.FixedResponseCommand(payload=None, serial=None)

Bases: canon.commands.Command

cmd3 = 513
first_chunk_size
resplen = None
response_length

Extract resplen from the response header

For cmd3=0x201 (fixed response length) commands word at 0x00 is response length excluding the first 0x40, as well as the word at 0x48, the header of the next layer?

class canon.commands.GenericLockKeysCmd(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

Lock keys and turn off LCD

cmd1 = 32
cmd2 = 18
resplen = 20
class canon.commands.GetOwnerCmd(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

Doesn’t appear to be supported by the G3.

cmd1 = 5
cmd2 = 18
resplen = 52
class canon.commands.GetPicAbilitiesCmd(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

cmd1 = 31
cmd2 = 18
resplen = 852
class canon.commands.GetPowerStatusCmd(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

cmd1 = 10
cmd2 = 18
resplen = 24
class canon.commands.GetTimeCmd(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

cmd1 = 3
cmd2 = 18
resplen = 32
class canon.commands.IdentifyCameraCmd(payload=None, serial=None)

Bases: canon.commands.FixedResponseCommand

Identify camera.

execute() -> (model, owner, version) strings

cmd1 = 1
cmd2 = 18
resplen = 92
class canon.commands.IdentifyFlashDeviceCmd(payload=None, serial=None)

Bases: canon.commands.VariableResponseCommand

Flash device identification.

cmd1 = 10
cmd2 = 17
class canon.commands.SetOwnerCmd(owner)

Bases: canon.commands.FixedResponseCommand

Change camera owner

cmd1 = 5
cmd2 = 18
resplen = 20
class canon.commands.SetTimeCmd(new_timestamp)

Bases: canon.commands.FixedResponseCommand

cmd1 = 4
cmd2 = 18
resplen = 20
class canon.commands.VariableResponseCommand(payload=None, serial=None)

Bases: canon.commands.Command

cmd3 = 514
first_chunk_size = 64
response_length

Return the response length, excluding the first 0x40 bytes.

canon.commands.lookup(cmd1, cmd2, cmd3)
canon.commands.lookup_rc(subcmd)

protocol Module

Communicate with a Canon camera, old style.

This protocol implementation is heavily based on http://www.graphics.cornell.edu/~westin/canon/index.html and gphoto2’s source. Sporadic comments here are mostly copied from gphoto’s source and docs.

– need to put that somewhere – wValue differs between operations. wIndex is always 0x00 wLength is simply the length of data.

class canon.protocol.CanonUSB(device)

Bases: object

USB Link to the camera.

bulk_read(size, timeout=None)
control_read(wValue, data_length=0, timeout=None)

Read from the control pipe.

bRequest is 0x4 if length of data is >1, 0x0c otherwise (length >1 ? 0x04 : 0x0C) bmRequestType is 0xC0 during read and 0x40 during write.

control_write(wValue, data='', timeout=None)
initialize()

Bring the camera into a state where it accepts commands.

There: http://www.graphics.cornell.edu/~westin/canon/ch03.html#sec.USBCameraInit

interrupt_read(size, timeout=100, ignore_timeouts=False)
is_polling
is_ready()

Check if the camera has been initialized.

gphoto2 source claims that this command doesn’t change the state of the camera and can safely be issued without any side effects.

poller
poller_ctx(*args, **kwds)
ready

Check if the camera has been initialized.

gphoto2 source claims that this command doesn’t change the state of the camera and can safely be issued without any side effects.

start_poller(size=None, timeout=None)
stop_poller()
timeout_ctx(*args, **kwds)
class canon.protocol.InterruptPoller(usb, size=None, chunk=16, timeout=None)

Bases: threading.Thread

Poll the interrupt pipe on a CanonUSB.

This should not be instantiated directly, but via CanonUSB.poller

run()
stop()

storage Module

class canon.storage.CanonStorage(usb)

Bases: object

drive
get_disk_info()
get_drive()

Returns the Windows-like camera FS root.

get_file(path, target, thumbnail=False)

Download a file from the camera.

target is either a file-like object or the file name to open and write to. thumbnail says wheter to get the thumbnail or the whole file.

initialize(force=False)
ls(path=None, recurse=12)

Return an FSEntry for the path or storage root.

By default this will return the tree starting at path with large enough recursion depth to cover every file on the FS.

mkdir()
put_file()
rmdir()
walk(path=None)
class canon.storage.FSAttributes

Bases: canon.util.Bitfield

DOWNLOADED = 32
NONRECURSE_DIR = 16
RECURSE_DIR = 128
UNKNOWN_2 = 2
UNKNOWN_4 = 4
UNKNOWN_40 = 64
UNKNOWN_8 = 8
WRITE_PROTECTED = 1
downloaded
is_dir
protected
recurse
class canon.storage.FSEntry(name, attributes, size=None, timestamp=None)

Bases: object

entry_size
full_path
is_dir
is_file
type_
class canon.storage.GetFileCmd(path, target, thumbnail=False)

Bases: canon.commands.VariableResponseCommand

cmd1 = 1
cmd2 = 17
execute(usb)
class canon.storage.ListDirectoryCmd(path=None, recurse=12)

Bases: canon.commands.VariableResponseCommand

cmd1 = 11
cmd2 = 17

util Module

class canon.util.Bitfield

Bases: array.array

Packs an array(‘B’, ...) as a set of flags

This can be any length and must be subclassed. Subclasses define cls._size to be the length of the bitfield in bytes and any number of instances of Flag as class attributes.

The Flag instances provide the descriptor protocol and convenient access to values of various flag within the array.

canon.util.BooleanFlag
canon.util.Flag

A set of bitmasks within a bitfield.

This may represent combinations of bit states between 8 and 64 bits long, instances are descriptors on Bitfield-s.

canon.util.chunks(l, n)

Yield successive n-sized chunks from l.

canon.util.extract_string(data, start=0)
canon.util.hexdump(data, with_ascii=True, with_offset=True)

Return the binary data as nicely printed hexadecimal text.

canon.util.itole32a(i)
canon.util.le16toi(raw, start=None)
canon.util.le32toi(raw, start=None)

Table Of Contents

This Page