Skip to content

Instantly share code, notes, and snippets.

@james-see
Last active October 2, 2016 18:21
Show Gist options
  • Save james-see/0d3ba1f1323f8fecc3ab to your computer and use it in GitHub Desktop.
Save james-see/0d3ba1f1323f8fecc3ab to your computer and use it in GitHub Desktop.
This script I use in pythonista to post to Flickr and then square the image and post to Instagram, all in a single step. The title of the image is a random five digit number as well.
from PIL import Image, ImageOps
import clipboard, photos,webbrowser
# Example for sending an email with an attached image using smtplib
#
# IMPORTANT: You need to enter your email login in the main() function.
# The example is prepared for GMail, but other providers
# should be possible by changing the mail server.
import smtplib
import random
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
import Image
from io import BytesIO
rr = str(random.randrange(5,100000))
im=photos.pick_image()#clipboard.get_image
def get_attachment(img):
bytes = BytesIO()
img.save(bytes, format='JPEG')
msg = MIMEBase('image', 'jpeg')
msg.set_payload(bytes.getvalue())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',
filename='image.jpeg')
return msg
def main():
### CHANGE THESE VALUES:
to = 'YOUR POST TO FLICKR EMAIL'
subject = rr
gmail_user = 'YOUR GMAIL ADDRESS'
gmail_pwd = 'YOUR GMAIL PASSWORD'
#Load a sample image, modify as needed:
image = im
print 'random number: ', rr
print 'image selected'
print 'Connecting...'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
print 'Preparing message...'
outer = MIMEMultipart()
outer['Subject'] = subject
outer['To'] = to
outer['From'] = gmail_user
outer.preamble = 'You will not see this in a MIME-aware email reader.\n'
attachment = get_attachment(image)
outer.attach(attachment)
composed = outer.as_string()
print 'Sending...'
smtpserver.sendmail(gmail_user, to, composed)
smtpserver.close()
print 'Done.'
if __name__ == '__main__':
main()
if im.size[0] >= im.size[1]:
whitespace=((im.size[0]-im.size[1])/2)+250
xbump=250
else:
xbump=((im.size[1]-im.size[0])/2)+250
whitespace=250
matted=ImageOps.expand(im,border=(xbump,whitespace),fill='white')
photos.save_image(matted)
inst='instagram://camera'
webbrowser.open(inst)
#if you have any questions or comments @jamescampbell on twitter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment