RhetTbull / osxphotos

Python app to work with pictures and associated metadata from Apple Photos on macOS. Also includes a package to provide programmatic access to the Photos library, pictures, and metadata.
MIT License
2.17k stars 100 forks source link

how do I import photos to the album using the python api or commandline tool ? #727

Closed eddo888 closed 2 years ago

eddo888 commented 2 years ago

I want to import photos to the album I would like to be able to create, update, delete and retrieve images using the python api and commandline tool. please let me know how to do this.

RhetTbull commented 2 years ago

osxphotos does not import photos (it's primarily an export tool). However, with the related photoscript package, you can use python to import photos. (osxphotos also uses photoscript for certain things such as downloading missing photos from iCloud) Below is a simple script that will import photos into photos, optionally putting them into an album (and creating the album if needed). You can install photoscript and click which are both prerequisites, then run the script or you can run it via osxphotos run command if you already have osxphotos installed. For example, if you save this script as import_photos.py: osxphotos run import_photos.py --album "New Photos" ~/Downloads/*.jpg

You can read the rest of the photoscript documentation here.

Neither photoscript nor osxphotos can delete photos. The best you can do is to use photoscript to place all photos you want to delete into an album (for example Photos to Delete) then select those photos in Photos and type Command+Option+Delete. You also cannot update photos with either photoscript or osxphotos though you can update the metadata about photos using photoscript.

"""Import photos to Photos.app from the command line"""

import photoscript
import click

@click.command()
@click.option("--album", "album_name", metavar="ALBUM")
@click.argument(
    "files", metavar="FILES...", nargs=-1, required=True, type=click.Path(exists=True)
)
def import_photos(album_name, files):
    """Import photos into Photos.app"""
    photos_library = photoscript.PhotosLibrary()

    album = None
    if album_name:
        album = photos_library.album(album_name)
        if album is None:
            click.echo(f"Creating Photos album '{album_name}'")
            album = photos_library.create_album(album_name)

    for file in files:
        click.echo(f"Importing file {file}")
        photos_library.import_photos([file], album=album, skip_duplicate_check=True)

if __name__ == "__main__":
    import_photos()
eddo888 commented 2 years ago

cool, thanks. I found another way to use applescript, messy but works.

''' from applescript import AppleScript

applescript = AppleScript("""

on import_photo(file_name, album_name)

set image_list to { }
set this_file to posix path of file_name
set the end of image_list to this_file

tell application "Photos"
    set the_album to album album_name
    log this_file
    import image_list into the_album with skip check duplicates
end tell

return the end of image_list

end import_photo

""")

for file in files: print(applescript.call('import_photo', file, '_FlightSim')) '''