Posted by iggykoopa on Sun 2nd Aug 19:32 (modification of post by view diff)
View followups from iggykoopa | download | new post
- #!/usr/bin/python
- import os,commands, urllib, Image, ImageFont, ImageDraw
- def reflect(home, artist, album, title, found):
- final_image = Image.new('RGBA', (300, 160) , (0, 0, 0, 0))
- mid_image = Image.new('RGBA', (300, 80) , (0, 0, 0, 0))
- image_flipped = Image.new('RGBA', (300, 80) , (0, 0, 0, 0))
- if found:
- image = Image.open(home + "/.album")
- else:
- image = Image.open(home + "/.noalbum")
- gradient = Image.new('L', (1,255))
- for y in range(255, 0, -1):
- if y < 128:
- gradient.putpixel((0,y),255 - (y * 2))
- else:
- gradient.putpixel((0,255-y),0)
- font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf",12)
- draw = ImageDraw.Draw(mid_image)
- draw.text((85, 30), artist, font=font)
- draw.text((85, 45), album, font=font)
- draw.text((85, 60), title, font=font)
- image = image.resize((80,80))
- mid_image.paste(image, (0, 0))
- image_flipped = mid_image.transpose(Image.FLIP_TOP_BOTTOM)
- alpha = gradient.resize(image_flipped.size)
- r,g,b,a = image_flipped.split()
- a_loaded = a.load()
- alpha_loaded = alpha.load()
- for x in range(300):
- for y in range(80):
- if a_loaded[x,y] < alpha_loaded[x,y]:
- alpha_loaded[x,y] = a_loaded[x,y]
- image_flipped.putalpha(alpha)
- final_image.paste(mid_image, (0, 0))
- final_image.paste(image_flipped, (0, 80))
- final_image.save(home + "/.album", "png")
- home = os.getenv("HOME")
- artist=commands.getoutput("mocp -Q %artist")
- album=commands.getoutput("mocp -Q %album")
- title=commands.getoutput("mocp -Q %song")
- if artist == "" and album == "":
- if os.path.exists(home + "/.album"):
- os.remove(home + "/.album")
- else:
- url = "http://www.albumart.org/index.php?srchkey=" + urllib.quote_plus(artist) + "+" + urllib.quote_plus(album) + "&itempage=1&newsearch=1&searchindex=Music"
- albumart = urllib.urlopen(url).read()
- image = ""
- for line in albumart.split("\n"):
- if "http://www.albumart.org/images/zoom-icon.jpg" in line:
- image = line.partition('src="')[2].partition('"')[0]
- break
- if image:
- if os.path.exists("/tmp/imagepath") and os.path.exists(home + "/.album"):
- imagepath = open("/tmp/imagepath").read()
- if imagepath == image:
- pass
- else:
- urllib.urlretrieve(image, home + "/.album")
- reflect(home, artist, album, title, 1)
- else:
- urllib.urlretrieve(image, home + "/.album")
- reflect(home, artist, album, title, 1)
- open("/tmp/imagepath","w").write(image)
- else:
- open("/tmp/imagepath","w").write("album_not_found")
- reflect(home, artist, album, title, 0)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.