Tag Archives: python imaging library

You’re welcome, Internet

Introducing the “Wolverine Crush” meme image generator

Because it had to be made at some point or other.

Generate your own wolverine crush image

Try it out

You will need your desired image to have an approximate aspect ratio of 1:1.14 to fit properly in the Wolverine photo frame. You can crop to a proper aspect ratio / proportions here (I have already set the parameters, but that site is screwy… double check that it is set at 1:1.14 and not 1:1).

The actual size of the image is not important (for best results, use an image over 300px one each side though) as it will be resized correctly as long as the aspect ratio of 1:1.14 is observed. Incorrect proportions will not fit properly and will result in a skewed image or whitespace in Wolverine picture frame.


The code

The Python code behind making it was surprisingly straight forward. I had been avoiding doing anything that involved the PIL (Python Imaging Library) for some time. I always assumed it would be a nightmare to manipulate images via scripts, but this was actually far easier than I thought it  would be (admittedly, this is a simple manipulation) and is an inspiration to look into making simple custom GIMP scripts or learning a little more PIL at some point down the road.

Most of what I needed to know for this meme generator project came from just two Stack Exchange questions [1][2]

# 'WOLVERINE CRUSH' meme generator
# JS Kouri
# 8/2014
# No restrictions on use

import Image
import hashlib
import time

def file_name():
  # Generate unique file name (variable 'name') based on hash of time
  global name
  t = time.time()
  h = hashlib.md5()
  name = str(h.hexdigest())
  name = name[0:5] + ".png"

''' Note: I hate sequential file names and I keep forgetting the syntax of the hashlib library, hence this odd naming scheme method. It's personally useful to me for memorizing syntax with repeat exposure.  '''

# user selected image to go into pic frame
fn = "imagetogointopictureframe.png" 
location = "/dir/to/img" + fn

# open base image and image to be superimposed:
wolverine = Image.open("../wp-content/uploads/2014/08/wolverine_generator.png")
user_img = Image.open(location)

# rotate user image to match angle of picture frame, preserve quality with bicubic interpolation
rot = user_img.rotate(6, resample=Image.BICUBIC, expand=1)

# create a new empty image with alpha, set to base (wolverine) image size
new_im = Image.new('RGBA', (480,700))

# paste rotated user image first, paste base image with alpha in picture frame second
new_im.paste(rot, (120,350) )
new_im.paste(wolverine, (0,0), mask=wolverine)

# save
location = "../desired/save/dir/" + name

# DEBUG 'print' image, useful when running on local machine for debugging