Skip to content

Instantly share code, notes, and snippets.

@giginet
Created December 7, 2011 13:11
Show Gist options
  • Select an option

  • Save giginet/1442745 to your computer and use it in GitHub Desktop.

Select an option

Save giginet/1442745 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
#
# banner.py
# created by giginet on 2011/12/07
#
import os
import random
import urllib2
from cStringIO import StringIO
from PIL import Image
class BannarCreator(object):
TWITTER_API = 'http://api.twitter.com/1/users/profile_image/%(username)s.json'
ICON_SIZE = (27, 27)
def __init__(self, path, randamize=False):
if os.path.exists(path):
f = open(path, 'r')
self.users = [user.strip() for user in f.readlines()]
else:
self.users = []
if randamize: random.shuffle(self.users)
def create(self):
base = Image.open('banner_template.png')
for i, user in enumerate(self.users):
row = int(i / 30)
col = i % 30
x = 3 + (self.ICON_SIZE[0] + 5) * col
y = 3 + (self.ICON_SIZE[1] + 5) * row
icon = self._fetch_icon(user).resize(self.ICON_SIZE)
base.paste(icon, (x, y))
return base
def _fetch_icon(self, username):
buf = urllib2.urlopen(self.TWITTER_API % {'username' : username}).read()
image = Image.open(StringIO(buf))
return image
bc = BannarCreator('users.txt', True)
banner = bc.create()
banner.show()
banner.save("banner.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment