Adding an image to my own album in Photos – how?
I'd like to kindly ask for a few lines of sample code. I want to achieve this:
I have a custom ui.View where I draw things to.
I want to save the contents of this view as a PNG to my own, new album inside the device's Photos library.
The next time the user wants to save an image it should go into the – now existing – album.
So I assume it goes like this:
- Check if the album "my album" exists
- If it does not exist, create it (photos.create_album())
- Create the PNG image somehow from the ui.View contents (found this, works, writes a file local to Pythonista's sandboxed file system)
- Save this file into the album "my album" using AssetCollection.add_assets() somehow
- Do I need to create this temporary PNG file?
- How do I check if an album exists? get_albums() only gives all albums. 😒
- How, exactly, do I add an image into "my album"?
Being still a Python starter it would be great if someone could help!
Thanks a lot!
Pythonista already includes a "draw image and save to camera roll" script in the examples folder. You might be able to learn from that how to save the contents of your view to an image.
You can find the example script at
Documents -> Examples -> User Interface
import photos import ui def add_to_album(image_path, album_name): # Find the album or create it: try: album = [a for a in photos.get_albums() if a.title == album_name] except IndexError: album = photos.create_album(album_name) # Add the file as an asset to the library: asset = photos.create_image_asset(image_path) # Add the asset to the album: album.add_assets([asset]) # Demo: if __name__ == '__main__': img = ui.Image('test:Lenna') png_data = img.to_png() with open('.temp.png', 'wb') as f: f.write(png_data) add_to_album('.temp.png', 'My Pythonista Album')
Never ever would I've been able to write such a line like you did in the
trystatment! I have no idea why & how this syntax works, that remains as a brain teaser for the weekend! 😉
Thanks so much for this quick help, Ole!! SO much appreciated! (Just a mini error: the parameter in the
create_image_asset()call has to be
However, I discovered a time stamp problem: I just saved an image to an album. When I did this the time was 03.03.2017, 20:07. When I launched the Photos app and opened my album I saw the one image in it but Photos said the image was dated from yesterday (02.03.2017) and from 22:12 ... 🤔
Anyone any idea why this might be the case (and how to fix it)?
@metawops I'm not quite sure yet where the timestamp issue is coming from, but it seems to be a bug. As a workaround, you can set
datetime.datetime.now()manually, after creating the asset.
@omz Thanks again, Ole! Works perfectly, time stamp is now correct. 😀
A bit off-topic extra question: is it possible to launch Photos, go to the album I created and show the image I saved?
@metawops It's possible to open the Photos app using
webbrowser.open('photos-redirect://')but I'm not sure if there's a way to show a specific photo or album that way.