Skip to content

Instantly share code, notes, and snippets.



Created Nov 6, 2012
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):
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
def handle_endtag(self, data):
def handle_data(self, data):
if self.get_data:
self.get_data = False
def getBandName():
random_wiki_url = ""
dom = minidom.parse(urllib.urlopen(random_wiki_url))
for line in dom.getElementsByTagName('page'):
return line.getAttributeNode('title').nodeValue
def getAlbumTitle():
random_quote_url = ""
page = urllib.urlopen(random_quote_url).read()
parser = MyHTMLParser()
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 = "" % (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 = "" % (farm_id, server_id, the_id, secret)
target_photo = 'band.jpg'["/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 ="band.jpg")
imgbg ='RGBA', img.size, "#000000") # make an entirely black image
mask ='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"out.jpg","JPEG",quality=100)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment