Mindtwist.de

...let your mind twist!

How to create small silkscreen blog buttons with Python

Little blog buttons with size of 80x15 pixels and small silkscreen font are trendy. This article shows you how to create those buttons with help of a little Python script.

 

The Script

The following link contains a tarball with the converted silkscreen font and the drawbtn.py Python script and should be ready for use:

http://www.mindtwist.de/main/downloads.html?task=summary&cid=4&catid=3

#!/usr/bin/python
# This script creates small png buttons from your text.
#
# This script needs the silkscreen font and ImageDraw python library:
# $ sudo yum install silkscreen-fonts python-imaging
#
# The silkscreen font needs to be converted so that it may be used from within python:
# # ./otf2bdf -p 8 -r 72 -o slkscr.bdf /usr/share/fonts/silkscreen/slkscr.ttf
# # ./pilfont.py slkscr.bdf
#
# The otf2bdf tool may be obtained from:
# http://www.math.nmsu.edu/~mleisher/Software/otf2bdf/
#

import Image, ImageDraw, ImageFont

# set image dimensions - feel free to customize!
width = 80
height = 15

# set colors to use - feel free to customize!
border = "#666666"
inner_border = "#ffffff"
background = "#466b7b"
fontcolor = "#ffffff"

# Print some information about this script
print '''This script has been released under BSD license.
Copyright (C) 2010 Reiner Rottmann

drawbtn.py creates small 80x15 blog button images with centered user defined text.

The output gets saved as 'buttoncaption.png'. For example if you enter "test" as
label caption, the resulting file will be 'test.png'.

Please be aware that this script overwrites existing content!
'''

# set button label
#label_caption = "Rottmann.it"

# let the user enter the label caption
label_caption = raw_input("Please enter the label for your button: ")

# create new image
btn = Image.new("RGB",(width,height), "White")

# initialize drawing
draw = ImageDraw.ImageDraw(btn)

# draw the border
draw.rectangle([(0,0),(width - 1,height - 1)],outline=border)
# draw the inner border
draw.rectangle([(1,1),(width - 2,height - 2)],outline=inner_border)
# draw label
draw.rectangle([(2,2),(width - 3,height - 3)],fill=background)
# load the silkscreen font
fnt = ImageFont.load('./slkscr.pil')
# draw the label caption centered
w, h = draw.textsize(label_caption)
draw.text((width/2 - w /2, height/2-h/2),label_caption)

# end drawing
del draw

# save the fine piece of art
btn.save(label_caption+".png")

The Script in Action

$ ./drawbtn.py
This script has been released under BSD license.
Copyright (C) 2010 Reiner Rottmann <reinerATrottmann.it>

drawbtn.py creates small 80x15 blog button images with centered user defined text.

The output gets saved as '<button label>.png'. For example if you enter "test" as
label caption, the resulting file will be 'test.png'.

Please be aware that this script overwrites existing content!

Please enter the label for your button: ROTTMANN.IT

The resulting output file looks as follows:

Example output of drawbtn.py

 

Linux Magazine

Linux Magazine News (path: lmi_news)