Created
April 1, 2010 18:19
-
-
Save mccutchen/352177 to your computer and use it in GitHub Desktop.
Creates a poster of Esquire magazine covers for a given decade.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Creates a poster of Esquire magazine covers for a given decade. | |
The covers for a given decade can be downloaded with something like | |
the following curl command: | |
curl -O http://www.esquire.com/cm/esquire/cover-images/196[0-9]_[1-12].jpg | |
""" | |
import os, sys | |
import Image | |
# What size is each cover image supposed to be? | |
cover_width = 382 | |
cover_height = 514 | |
expected_size = (cover_width, cover_height) | |
# Determine the size of the final image based on the size of the | |
# individual covers. | |
final_width = cover_width * 12 | |
final_height = cover_height * 10 | |
# Where are the covers located? | |
cover_path = 'covers/%(year)d_%(month)d.jpg' | |
def combinecovers(decade): | |
"""Creates a giant image containing all of the covers for the | |
given decade, with one row of covers per year and one column per | |
month. Assumes the covers are in the current directory.""" | |
# Create the output image. | |
output_image = Image.new('RGB', (final_width, final_height), 'black') | |
# Iterate through the years of the decade and the months of the | |
# year. | |
for year in range(decade, decade + 10): | |
for month in range(1, 13): | |
# Figure out the path to this month's cover image. | |
path = cover_path % dict(year=year, month=month) | |
# Make sure the path exists. | |
if os.path.exists(path): | |
# Open this month's cover image. | |
cover = Image.open(path) | |
# Calculate the offset at which to place this month's | |
# cover in the larger final image. | |
offset = [cover_width * (month - 1), | |
cover_height * (year - decade)] | |
# Make sure the image is the right size | |
if cover.size != expected_size: | |
w, h = cover.size | |
# The image is proportionally the right size, but | |
# is simply too large. | |
if h > w: | |
new_size = expected_size | |
# The image is at a different proportion, so | |
# resize it to the expected size, maintaining its | |
# aspect ratio. | |
else: | |
ratio = min(float(cover_width)/float(w), | |
float(cover_height)/float(h)) | |
new_size = (int(w * ratio), int(h * ratio)) | |
# Update the offset to center the oddly-sized | |
# image in its assigned space | |
if new_size[0] > new_size[1]: | |
offset[1] += new_size[1] / 2 | |
else: | |
offset[0] += new_size[0] / 2 | |
# Resize the cover to fit | |
cover = cover.resize(new_size, Image.ANTIALIAS) | |
# Place this month's cover in the larger image. | |
output_image.paste(cover, tuple(offset)) | |
# Write the larger image to disk. | |
output_image.save('covers_%ds.jpg' % decade) | |
if __name__ == '__main__': | |
try: | |
decade = int(sys.argv[1]) | |
combinecovers(decade) | |
except IndexError: | |
print 'Usage: %s DECADE' % __file__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment