USB API with Python Getting Started

install gphoto2 python module

python -m venv venv
source venv/bin/activate
pip install gphoto2
Collecting gphoto2
  Downloading gphoto2-2.5.1-cp311-cp311-macosx_14_0_arm64.whl (5.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.7/5.7 MB 8.6 MB/s eta 0:00:00
Installing collected packages: gphoto2
Successfully installed gphoto2-2.5.1

get camera summary

import gphoto2 as gp

camera = gp.Camera()
camera.init()

# Get summary
text = camera.get_summary()
print('Summary')
print('=======')
print(text)

camera.exit()

Output

python usb-demo.py 
Summary
=======
Manufacturer: Ricoh Company, Ltd.
Model: RICOH THETA X
  Version: 2.63.0
  Serial Number: 14138352
Vendor Extension ID: 0x6 (1.10)
Vendor Extension Description: 

Capture Formats: 
Display Formats: Firmware, Association/Directory, JPEG, MP4, DNG
Supported MTP Object Properties:

list files on camera

import gphoto2 as gp


def list_files_recursive(camera, path='/'):
    # List files in current directory
    files = camera.folder_list_files(path)
    for name, _ in files:  # Unpack the tuple to get just the name
        print(f'{path}{name}')
    
    # List and recurse into subdirectories
    folders = camera.folder_list_folders(path)
    for folder, _ in folders:  # Unpack the tuple to get just the folder name
        list_files_recursive(camera, path + folder + '/')


camera = gp.Camera()
camera.init()

# List files
print('Files')
print('=====')
storage_info = camera.get_storageinfo()
for storage in storage_info:
    # Access capacity directly from the storage object
    capacity = storage.capacitykbytes
    print(f'Storage capacity: {capacity} KB')

print('\nFile listing:')
print('============')
list_files_recursive(camera)

camera.exit()

output of list files

python usb-demo-files.py
Files
=====
Storage capacity: 48247664 KB

File listing:
============
/store_00020001/DCIM/100RICOH/R0010003.JPG
/store_00020001/DCIM/100RICOH/R0010004.JPG
/store_00020001/DCIM/100RICOH/R0010005.JPG
/store_00020001/DCIM/100RICOH/R0010006.JPG
/store_00020001/DCIM/100RICOH/R0010007.JPG
/store_00020001/DCIM/100RICOH/R0010008.JPG

take picture

import gphoto2 as gp

camera = gp.Camera()
camera.init()

print('Capturing image')
file_path = camera.capture(gp.GP_CAPTURE_IMAGE)
print(f'Image saved to {file_path.folder}/{file_path.name}')

camera.exit()

output of take picture

python usb-demo-take-picture.py
Capturing image
Image saved to /store_00020001/DCIM/100RICOH/R0010118.JPG

switch to video mode

import gphoto2 as gp

camera = gp.Camera()
camera.init()

# Get the configuration
config = camera.get_config()

# Find the capture mode setting
capture_mode = None
try:
    capture_mode = config.get_child_by_name('capturemode')
except gp.GPhoto2Error as e:
    print(f"Error getting capture mode: {e}")
    # If that fails, let's try to find it by browsing all settings
    for i in range(config.count_children()):
        child = config.get_child(i)
        if 'capture' in child.get_name().lower() and 'mode' in child.get_name().lower():
            capture_mode = child
            print(f"Found capture mode setting: {child.get_name()}")
            break

if capture_mode:
    # Print current value
    print(f"Current value: {capture_mode.get_value()}")
    # Set to video mode - we now know it's "Unknown value 8002"
    try:
        capture_mode.set_value("Unknown value 8002")
        camera.set_config(config)
        print("Camera set to video mode")
    except Exception as e:
        print(f"Failed to set video mode: {str(e)}")
else:
    print("Could not find capture mode setting")

camera.exit()