Wednesday, 1 September 2010

Transparent Android icons






I have an Android phone and I wanted to create some new icons.
Inspired by various Andoid themes on xda-developers, I wanted text only icons.
I started whipping up icons in Photoshop, but it was a pain to try new fonts (yes I know PS has scripting...).

So I created a python script to generate a series of icons and zip them up.
The script uses Python Image Library, to render separate transparent PNG files.

from PIL import Image, ImageDraw, ImageFont
import zipfile
import os

class iconpack:
  def __init__(self, zipfsp, fontfsp, fontsize, height, width):
    self.zip=zipfile.ZipFile(zipfsp, mode='w')  # create the zip file
    self.fontfsp=fontfsp
    self.fontsize=fontsize
    self.h=height
    self.w=width

  def addicon(self,lines):
    iconfsp=self.render(lines)
    self.zip.write(iconfsp)  # add the icon tot he zip file
    os.remove(iconfsp) # delete the remaining icon file
    
  def render(self, lines):
    im =Image.new("RGBA",(self.w,self.h),(0,0,0,0))  # adjust the 4th zero to adjust transparency
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype(self.fontfsp,self.fontsize) # the font file needs to be accessible
    y=0
    for line in lines:
      rendered_hw=font.getsize(line)  # figure out how wide the rendered text will be
      x=im.size[0]/2-(rendered_hw[0]/2) # so we can centre it
      draw.text((x,y), line, font=font)
      y+=rendered_hw[1]-5   # adust the gap between the lines

    # write image file
    iconfsp=lines[0]+"_"+lines[1]+".png"
    im.save(iconfsp, "png")
    del im
    return iconfsp

    
# II saw an iconpack on xda which used 46x81, on the shoulders of giants, etc

# zipfile, font file, font size, width and height
ip=iconpack('DroidSans.zip','DroidSans.ttf',23,46,81) 
ip.addicon(["Inter", "Webs"])
ip.addicon(["My", "Email"])
ip.addicon(["Work", "Email"])
ip.addicon(["Snap", "Shot"])
ip.addicon(["Pod", "Casts"])
ip.addicon(["Audio", "Books"])
ip.addicon(["The", "Mrs"])
ip.addicon(["Bar", "Code"])
ip.addicon(["Four", "Square"])
ip.addicon(["SMS", ""])
ip.addicon(["Maps", ""])
ip.addicon(["Reddit", "is fun"])



Here's hows the icons turned out.  I tried a few fonts from FontSpace, but settled on DroidSans.


1 comment:

Value of a Dollar said...

Great thanks for these!

Alex