omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Adding an image to my own album in Photos – how?

    Pythonista
    3
    7
    7301
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • metawops
      metawops last edited by metawops

      Hi,
      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:

      1. Check if the album "my album" exists
      2. If it does not exist, create it (photos.create_album())
      3. Create the PNG image somehow from the ui.View contents (found this, works, writes a file local to Pythonista's sandboxed file system)
      4. Save this file into the album "my album" using AssetCollection.add_assets() somehow

      Questions:

      • 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!
      Stefan.

      1 Reply Last reply Reply Quote 0
      • lukaskollmer
        lukaskollmer last edited by

        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

        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by omz

          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][0]
          	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')
          
          
          metawops 1 Reply Last reply Reply Quote 1
          • metawops
            metawops @omz last edited by

            @omz WOW!
            Never ever would I've been able to write such a line like you did in the try statment! 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 image_path. 😉

            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)?

            1 Reply Last reply Reply Quote 0
            • omz
              omz last edited by omz

              @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 asset.creation_date to datetime.datetime.now() manually, after creating the asset.

              metawops 1 Reply Last reply Reply Quote 0
              • metawops
                metawops @omz last edited by

                @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?

                1 Reply Last reply Reply Quote 0
                • omz
                  omz last edited by

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors