Skip to content

Instantly share code, notes, and snippets.

@joemarchese
Created March 3, 2017 23:38
Show Gist options
  • Select an option

  • Save joemarchese/cdb580ac290b1bd9ffd66a232df54720 to your computer and use it in GitHub Desktop.

Select an option

Save joemarchese/cdb580ac290b1bd9ffd66a232df54720 to your computer and use it in GitHub Desktop.
A script to take an image from the internet, make a meme of it, and post it to Slack.
# A script to take an image from the internet, make a meme of it, and post
# it to Slack.
import urllib.request
from PIL import Image
from random import choice
from slacker import Slacker
# Instantiate a Slack Session
token = input('Please Input Your Slack Token: ')
slack = Slacker(token)
# Get Input File From The Innanets. Check Validity.
infile = input('Please Enter Full URL for the Image: ')
try:
infile = urllib.request.urlretrieve(infile, infile.split('/')[-1])
except urllib.error.URLError as error:
print('Something went wrong. Make sure your URL is valid.')
print('{}'.format(error))
quit()
# Check that the image file is valid
try:
image = Image.open(infile[0])
except OSError as error:
print('Something went wrong. Make sure your URL points to an image.')
print('OS Error: {}'.format(error))
quit()
# Process the input image
width, height = image.size
crop_size = (round(.10 * width), round(.10 * height), round(.90 * width), round(.90 * height))
zoom = image.crop(crop_size)
zoom = zoom.resize(image.size, resample=Image.BICUBIC)
zoom_two = zoom.crop(crop_size)
zoom_two = zoom_two.resize(image.size, resample=Image.BICUBIC)
zoom_three = zoom_two.crop(crop_size)
zoom_three = zoom_three.resize(image.size, resample=Image.BICUBIC)
# Build the Meme
four_image = Image.new('RGBA', ((width * 2), (height * 2)), 'white')
four_image.paste(image, (0, 0))
four_image.paste(zoom, (width, 0))
four_image.paste(zoom_two, (0, height))
four_image.paste(zoom_three, (width, height))
# Save the Meme Locally
four_image.save('meme.png', 'PNG')
# Post on Slack in #General
slack.files.upload('meme.png', channels=['#general'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment