Skip to content

Instantly share code, notes, and snippets.

@ymek
Last active December 27, 2015 01:09
Show Gist options
  • Select an option

  • Save ymek/7242759 to your computer and use it in GitHub Desktop.

Select an option

Save ymek/7242759 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
» brew info lzlib
lzlib: stable 1.5
http://www.nongnu.org/lzip/lzlib.html
/usr/local/Cellar/lzlib/1.5 (8 files, 120K) *
Built from source
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/lzlib.rb
» brew info freetype
freetype: stable 2.5.0.1 (bottled)
http://www.freetype.org
/usr/local/Cellar/freetype/2.5.0.1 (59 files, 2.6M) *
Poured from bottle
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/freetype.rb
"""
import os
from PIL import Image, ImageDraw, ImageFont
import logging
L = logging.getLogger()
text = 'There is asdfasfd'
ascent = 76
descent = 18
font = ImageFont.truetype('../src/data/fonts/SEGOEUI.TTF', 70, encoding='unic')
font = ImageFont.truetype('/Library/Fonts/Tahoma.ttf', 70, encoding='unic')
im = Image.new('RGBA', (573, 54), '#000')
mask = Image.new('L', im.size, color=255)
mask_draw = ImageDraw.Draw(mask)
mask_draw.rectangle((0, 0, im.size[0], im.size[1]), fill=0)
im.putalpha(mask)
draw = ImageDraw.Draw(im)
top, left = 0, 0
h_diff = 54 - 54
if h_diff > 0 and h_diff > descent:
ttop = (54 - 54) - descent
else:
ttop = top
L.debug('Generating text mask @ %d x %d' % (573, 54))
draw.text((left, ttop), text, font=font,
fill='#000')
left += 573
if os.environ.has_key('DEBUG'):
print '-' * 80
print 'S:%d H:%d A:%d D:%d %s' % (70, 54, ascent,
descent, text)
print '-' * 80
lh = 54 - descent
draw.line([(0, lh), (573, lh)], fill='red', width=1)
from PIL import ImageOps
im = ImageOps.expand(im, border=2, fill='red')
im.save('/tmp/output-text-%d.png' % len(text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment