Skip to content

Instantly share code, notes, and snippets.

@foxlet
Created December 24, 2014 16:46
Show Gist options
  • Save foxlet/94afa3a3e68914c82957 to your computer and use it in GitHub Desktop.
Save foxlet/94afa3a3e68914c82957 to your computer and use it in GitHub Desktop.
import PIL
from PIL import ImageFont, Image, ImageDraw, ImageFilter, ImageChops
from cloudbot import hook
import random
import base64
import requests
import json
import datetime
import imghdr
import urllib
import os
from textwrap import wrap
# Impact.ttf is included with the meme.py package, but you can use any TrueType font.
meme_font = "data/fonts/Impact.ttf"
# Your imgur API key should go here.
api_key = "1ae994097d741ae"
# Directory to your meme folder store.
# There should NOT be a trailing slash.
meme_directory = "plugins/memes"
# The format of your memes in the store, preferably JPG or PNG.
meme_format = "jpg"
@hook.command
def meme(text):
# Set args to whatever will be the input.
args = text
if ";" in args:
arguments = args.split(";")
count = len(arguments)
if count == 2:
# Arguments with a specific meme, one line
topString = ''
bottomString = arguments[1]
meme = arguments[0]
link = meme_generator(meme, bottomString, topString)
elif count == 3:
# Arguments with a specific meme, two lines
topString = arguments[1]
bottomString = arguments[2]
meme = arguments[0]
link = meme_generator(meme, topString, bottomString)
else:
# So many args
# What do they mean?
# Too intense...
return "You gave one too many arguments."
else:
# Just text?, use standard meme. Eventually random.
topString = ''
bottomString = args
meme = 'standard'
link = meme_generator(meme, topString, bottomString)
return link
def meme_generator(meme, topString, bottomString):
print(meme)
img = Image.open("{}/{}.{}".format(meme_directory, str(meme), meme_format))
imageSize = img.size
# Find the largest font size that works
fontSize = imageSize[1]//5
font = ImageFont.truetype(meme_font, fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20:
fontSize = fontSize - 1
font = ImageFont.truetype(meme_font, fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
# Find the top centered position for top text
topTextPositionX = (imageSize[0]//2) - (topTextSize[0]//2)
topTextPositionY = 0
topTextPosition = (topTextPositionX, topTextPositionY)
# Find the bottom centered position for bottom text
bottomTextPositionX = (imageSize[0]//2) - (bottomTextSize[0]//2)
factor = bottomTextSize[1] * 0.5
add = bottomTextSize[1] + factor
bottomTextPositionY = imageSize[1] - add
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY)
draw = ImageDraw.Draw(img)
halo = Image.new('RGBA', img.size, (0, 0, 0, 0))
shadow_matte = Image.new('RGB', img.size, (0,0,0))
top_text = Image.new('RGBA', img.size, (255, 255, 255, 0))
# Draw the text outlines
# There may be a better way (antialised)
outlineRange = fontSize//15
for x in range(-outlineRange, outlineRange+1):
for y in range(-outlineRange, outlineRange+1):
ImageDraw.Draw(halo).text((topTextPosition[0]+x, topTextPosition[1]+y), topString, (0,0,0), font=font)
ImageDraw.Draw(halo).text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), bottomString, (0,0,0), font=font)
blurred_outline = halo.filter(ImageFilter.BLUR)
ImageDraw.Draw(blurred_outline).text(topTextPosition, topString, font = font, fill = (0, 0, 0))
img = Image.composite(img, shadow_matte, ImageChops.invert(blurred_outline))
ImageDraw.Draw(top_text).text(topTextPosition, topString, (255,255,255), font=font)
ImageDraw.Draw(top_text).text(bottomTextPosition, bottomString, (255,255,255), font=font)
img = Image.composite(img, top_text, ImageChops.invert(top_text))
img.save("temp.png")
link = sendtoimgur("temp.png")
return link
def sendtoimgur(file):
image_path = file
headers = {'Authorization': 'Client-ID ' + api_key}
format = imghdr.what(image_path)
print(format);
image_path_fixed = image_path
fh = open(image_path_fixed, 'rb');
base64img = base64.b64encode(fh.read())
url="https://api.imgur.com/3/upload.json"
r = requests.post(url, data={'key': api_key, 'image':base64img,'title':'meme'},headers=headers,verify=False)
print(r.text);
val=json.loads(r.text)
try:
return val['data']['link']
except KeyError:
return val['data']['error']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment