Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------
# test stuff
from optparse import OptionParser
import Image, ImageFont, ImageDraw, sys, codecs
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-l", "--label")
parser.add_option("-o", "--out")
parser.add_option("--font")
parser.add_option("-c", "--color") # font color
parser.add_option("-s", "--size") # font size
parser.add_option("-t", "--theme") # light or dark
parser.add_option("-b", "--box") # big, medium, small
(options, args) = parser.parse_args()
theme = (options.theme, "light")[options.theme is None]
box = (options.box, "big")[options.box is None]
if box == 'big':
size = 22
offset = 40
elif box == 'medium':
size = 18
offset = 20
else:
size = 12
offset = 10
if theme == 'light':
color = 'black'
else:
color = 'white'
#optional values
base_image = "base_%s_%s.png" % (theme, box)
logo_image = "logo_%s_%s.png" % (theme, box)
end_image = "base_right_%s_%s.png" % (theme, box)
size = (options.size, size)[options.size is None]
font = (options.font, "fonts/DejaVuSans.ttf")[options.font is None]
color = (options.color, color)[options.color is None]
#required values
path = options.out
label = options.label.decode(sys.getfilesystemencoding())#.encode('utf-8')
font = ImageFont.truetype(font, int(size))
#draw font
text_image = Image.new("RGBA", (10000, 100), (0,0,0,0))
draw = ImageDraw.Draw(text_image)
draw.text((0, 0), label, font = font, fill = color)
l,t,r,b = text_image.getbbox()
text_image = text_image.crop((l,t,r,b))
#get end image
end_image = Image.open(end_image)
el, et, er, eb = end_image.getbbox()
end_width = er-el
#get logo image
logo_image = Image.open(logo_image)
ll, lt, lr, lb = logo_image.getbbox()
logo_width = lr - ll
#get base image
base_image = Image.open(base_image)
bl, bt, br, bb = base_image.getbbox()
base_image = base_image.resize((r + offset + logo_width + end_width, bb))
bl, bt, br, bb = base_image.getbbox()
draw = ImageDraw.Draw(base_image)
draw.text((logo_width + offset/2, (bb - (b-t))/2.2), label, font = font, fill = color)
base_image.paste(logo_image, (0,0))
base_image.paste(end_image, (br-end_width, 0))
base_image.save(path, "PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment