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.
Forum Archive
Photos module
https://forum.omz-software.com/search/create_image_asset?in=titlesposts
@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.
@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 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()
@cvp thanks I will try this and the other suggestion and let you know. Thanks.
@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()
@ccc As usual, you're right and as usual, I forgot that... Thanks for @resserone13 and for me
@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
@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)
@resserone13 Oups 😬 no idea