Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Used to make a montage of faces of California's War Dead for a Facebook poster image. https://www.facebook.com/photo.php?fbid=10151492464488010&set=a.74076168009.73112.5863113009&type=1
import os
import urllib2
from urllib import urlretrieve
from bs4 import BeautifulSoup
# Open our page with all the photos
response = urllib2.urlopen('http://projects.latimes.com/wardead/iraq/')
# Hand it off to BeautifulSoup for HTML parsing
soup = BeautifulSoup(response.read())
# Narrow the page down to just the main body well
body = soup.find("div", class_="content-body")
# Strain out just the headshots, which all have the same style to look for
images = body.find_all("img", style='border: 2px solid #ccc;')
# Loop through them all
counter = 1
for i in images:
# Gin up the path they'll be saved at
path = os.path.join(
'./iraq',
str(counter) + "." +
i['src'].split(".")[-1]
)
# If that path doesn't already exist...
if not os.path.exists(path):
# Download the file
print "Downloading %s as %s" % (i.get('title', ''), path)
urlretrieve(i['src'], path)
# Flip the counter up one
counter += 1
# Clip a 60x60 thumbnail from each image
os.system("convert -define jpeg:size=150x187 ./iraq/*.jpg -thumbnail 60x60^ -gravity center -extent 60x60 square.jpg")
# Make a montage of all those crops
os.system("montage -geometry 60x60+0+0 -tile 31x15 square*.jpg splash.jpg")
# Then I went into GIMP and added the headline and description text before uploading to Facebook
# https://www.facebook.com/photo.php?fbid=10151492464488010&set=a.74076168009.73112.5863113009&type=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment