Created
October 31, 2013 00:37
-
-
Save ymek/7242739 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| 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') | |
| 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, | |
| 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