Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Create a random album cover
#!/usr/bin/env python2.6
import urllib
from xml.dom import minidom
from optparse import OptionParser
import subprocess
import re
from HTMLParser import HTMLParser
import random
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.get_data = False;
self.quotes = []
def handle_starttag(self, tag, attrs):
if tag == "dt":
if attrs[0][0] == 'class' and attrs[0][1] == 'quote':
self.get_data = True
pass
def handle_endtag(self, data):
pass
def handle_data(self, data):
if self.get_data:
self.quotes.append(data)
self.get_data = False
def getBandName():
random_wiki_url = "http://en.wikipedia.org/w/api.php?format=xml&action=query&list=random&rnnamespace=0&rnlimit=1"
dom = minidom.parse(urllib.urlopen(random_wiki_url))
for line in dom.getElementsByTagName('page'):
return line.getAttributeNode('title').nodeValue
def getAlbumTitle():
random_quote_url = "http://www.quotationspage.com/random.php3"
page = urllib.urlopen(random_quote_url).read()
parser = MyHTMLParser()
parser.feed(page)
num_quotes = len(parser.quotes)
quote = parser.quotes[random.randint(0, num_quotes)].rstrip('.')
last_set = random.randint(3,5)
words = quote.split()
if last_set > len(words):
last_set = len(words)
return (" ").join(words[-last_set:])
def getAlbumImage():
page = random.randint(1,80)
url = "http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=YOURAPIKEY&per_page=6&page=%d&format=rest" % (page)
print url
dom = minidom.parse(urllib.urlopen(url))
elem = dom.getElementsByTagName('photo')[4]
farm_id = elem.getAttributeNode('farm').nodeValue
server_id = elem.getAttributeNode('server').nodeValue
the_id = elem.getAttributeNode('id').nodeValue
secret = elem.getAttributeNode('secret').nodeValue
photo_url = "http://farm%s.staticflickr.com/%s/%s_%s_b.jpg" % (farm_id, server_id, the_id, secret)
target_photo = 'band.jpg'
subprocess.call(["/usr/bin/wget", "-O", target_photo, photo_url])
def main():
band_name = getBandName()
album_title = getAlbumTitle()
cover = getAlbumImage()
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
fnt = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf",25)
lineWidth = 20
img = Image.open("band.jpg")
imgbg = Image.new('RGBA', img.size, "#000000") # make an entirely black image
mask = Image.new('L',img.size,"#000000") # make a mask that masks out all
draw = ImageDraw.Draw(img) # setup to draw on the main image
drawmask = ImageDraw.Draw(mask) # setup to draw on the mask
drawmask.line((0, lineWidth, img.size[0],lineWidth),
fill="#999999", width=100) # draw a line on the mask to allow some bg through
img.paste(imgbg, mask=mask) # put the (somewhat) transparent bg on the main
draw.text((10,0), band_name, font=fnt, fill="#ffffff") # add some text to the main
draw.text((10,40), album_title, font=fnt, fill="#ffffff") # add some text to the main
del draw
img.save("out.jpg","JPEG",quality=100)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment