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.


    Photos module

    Pythonista
    photos module photos email photos save photos
    3
    11
    5007
    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.
    • resserone13
      resserone13 last edited by

      Can some demonstrate all the methods and function of the photos module? I'm have trouble using create_image_asset(). I want to take a photo and save it to a perticular album. After that I want the photo sent via email and then deleted for the album.

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

        https://forum.omz-software.com/search/create_image_asset?in=titlesposts

        resserone13 1 Reply Last reply Reply Quote 0
        • resserone13
          resserone13 @ccc last edited by

          @ccc when I take a pic with the capture_image() how do I name it so I can pass it to create_image_asset() then send it via a mime email.

          cvp 2 Replies Last reply Reply Quote 0
          • cvp
            cvp @resserone13 last edited by

            @resserone13 create a photo in camera roll via

            import photos
            import os
            pil_image = photos.capture_image()	# returns a PIL Image
            path = 'new.jpg'
            pil_image.save(path, format='JPEG')
            photos.create_image_asset(path)
            os.remove(path) 
            
            resserone13 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @resserone13 last edited by

              @resserone13 to send a captured photo via email, you don't need to save it first in the camera roll. Not tested:

              import photos
              pil_image = photos.capture_image()	# returns a PIL Image
              
              import smtplib # for sending an email
              from email.mime.image import MIMEImage
              from email.mime.multipart import MIMEMultipart
              import io
              
              # Create the container email message.
              me = 'your email'
              msg = MIMEMultipart()
              msg['Subject'] = 'your subject'
              msg['From'] = me
              msg['To'] = me
              msg.preamble = 'your text'
              		
              # attach photo to mail		
              buf = io.BytesIO()
              pil_image.save(buf, format='JPEG')
              photo_data = buf.getvalue()
              img = MIMEImage(photo_data)
              msg.attach(img)
              
              # Send the email via our own SMTP server.
              s = smtplib.SMTP('your smtp server')
              s.sendmail(me, me, msg.as_string())
              s.quit() 
              
              1 Reply Last reply Reply Quote 0
              • resserone13
                resserone13 @cvp last edited by

                @cvp thanks I will try this and the other suggestion and let you know. Thanks.

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

                  @cvp, Caution about leaving io.BytesIO() open after you are done with them. In a memory constrained environment like Pythonista and with large objects like images, running out of RAM can be a problem.

                  # attach photo to mail      
                  buf = io.BytesIO()
                  pil_image.save(buf, format='JPEG')
                  photo_data = buf.getvalue()
                  
                  # might be safer rewritten:
                  with io.BytesIO() as buf:
                      pil_image.save(buf, format='JPEG')
                      photo_data = buf.getvalue()
                  
                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @ccc last edited by cvp

                    @ccc As usual, you're right and as usual, I forgot that... Thanks for @resserone13 and for me

                    resserone13 1 Reply Last reply Reply Quote 0
                    • resserone13
                      resserone13 @cvp last edited by

                      @cvp @ccc I'm still fitting pieces of the code you suggested into my existing code. Having a bit of trouble but going to try to work on it more.

                      I have hear the the with keyword will open and close things. Thanks

                      1 Reply Last reply Reply Quote 0
                      • resserone13
                        resserone13 last edited by ccc

                        @cvp @ccc. Thanks. I'm able to take a pick and send it via email. I used ...

                        #converts image to bytes and create mime image for mime email.
                        with io.BytesIO() as buf:
                            req_photo.save(buf,format='JPEG')
                            sent_img = buf.getvalue()
                            img_sent = MIMEImage(sent_img)
                        
                        # And..
                        
                        main_email.attach(img_sent)
                        

                        I was using and I'm wondering if it's better to send pic via email with.

                        attachment = open('/var/mobile/Media/DCIM/', 'rb',) as f:
                        #attachment = open('/var/mobile/Media/DCIM/', 'rb',)
                        #Adds attachment and encoding for attachment.
                        #part = MIMEBase(sent_img, 'png')
                        #part = MIMEBase('application', 'octet-stream')
                        #part.set.payload(attachment.read())
                        #encoders.encode_base64(part)
                        #part.add_header('Content-disposition', 'attachment: file' + sent_img)

                        cvp 1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @resserone13 last edited by

                          @resserone13 Oups 😬 no idea

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