Squirrel Photo Bomber


The students in the high school programming course I teach seemed like they could use a break from parsing FASTA files, so I did a mini-lesson about image manipulation in Python. We wrote a script to paste the photo-bombing ground squirrel of meme fame into any image. It makes use of the Python Imaging Library (PIL) and a blue screened squirrel bitmap.

You can download the Python script, though you will also need the Python Imaging Library installed. Run the script on the command line with:

python crasher_squirrel_bergey.py

It will then prompt you for the location of the background image (which must be a bitmap, *.bmp), the input height as fraction of background height and the input position as fraction of background width. You can also pass these parameters on the command line:

python crasher_squirrel_bergey.py {bg_file}
	{ratio_of_bg} {x_position}

It expects the squirrel bitmap to be in the same directory as the script. The interesting bit of the code is pasted below. It masks out any pixels that are pure blue (0, 0, 255).

# Create mask of squirrel image to not paste blue regions later

# Split the image into individual bands
sq_colors = sq.split()

R, G, B = 0, 1, 2

mask = sq_colors[B].point(lambda i: i != 255 and 255)

# Past squirrel into image

im.paste(sq, paste_coord, mask)