PyObjC Framework Photos

12.1 · active · verified Tue Apr 14

This library provides Python wrappers for the Photos framework on macOS, allowing Python applications to interact with the user's photo library. It's part of the larger PyObjC project, enabling access to Objective-C frameworks from Python. The current version is 12.1 and follows the PyObjC project's release cadence, often aligning with macOS SDK updates and Python version support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to check Photos library authorization and list the titles of a few user-created albums. This code must be run on macOS. You might need to grant your Python environment (e.g., Terminal.app or your IDE) access to Photos via System Settings > Privacy & Security > Photos.

import Photos
import objc

def list_some_photo_data():
    # All Cocoa calls must be made within an autorelease pool
    pool = objc.autorelease_pool()
    try:
        # Check authorization status for Photos access
        status = Photos.PHPhotoLibrary.authorizationStatus()

        if status == Photos.PHAuthorizationStatusAuthorized:
            print("Photos access is authorized.")
            
            # Fetch all user albums
            user_albums = Photos.PHAssetCollection.fetchAssetCollectionsWithType_subtype_options_(
                Photos.PHAssetCollectionTypeAlbum,
                Photos.PHAssetCollectionSubtypeAny,
                None
            )
            print(f"Found {user_albums.count()} user albums:")
            for i in range(min(3, user_albums.count())): # List up to 3 albums
                album = user_albums.objectAtIndex_(i)
                print(f"- {album.localizedTitle()}")
        elif status == Photos.PHAuthorizationStatusLimited:
            print("Photos access is limited (user selected specific photos).")
            # Can still fetch, but only for selected photos
        else:
            print(f"Photos access is not authorized. Current status: {status}")
            print("Please ensure your application (or terminal) has Photos access granted in System Settings > Privacy & Security > Photos.")
    finally:
        # The pool must be released
        del pool

if __name__ == '__main__':
    list_some_photo_data()

view raw JSON →