PyObjC Framework Photos
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
- breaking PyObjC 12.0 dropped support for Python 3.9. PyObjC 11.0 dropped support for Python 3.8. The latest versions require Python 3.10 or later.
- gotcha PyObjC (and thus `pyobjc-framework-photos`) is a macOS-specific library. It provides bindings to Apple's Objective-C frameworks, which are only available on macOS.
- breaking PyObjC 11.1 introduced significant changes to how initializer methods are modeled, aligning with `clang`'s automatic reference counting (ARC) documentation. Custom `init` methods in Python subclasses might behave differently regarding reference counting.
- gotcha Experimental free-threading support (PEP 703) in Python 3.13 is *not* fully supported by PyObjC. While PyObjC 11.0 introduced *experimental* support, there may still be stability or performance issues when running with free-threading enabled.
Install
-
pip install pyobjc-framework-photos
Imports
- Photos
import Photos
- objc
import objc
Quickstart
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()