Skip to content

Instantly share code, notes, and snippets.

@e000
Created January 23, 2011 06:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e000/791868 to your computer and use it in GitHub Desktop.
Save e000/791868 to your computer and use it in GitHub Desktop.
ascii -> png
"""
Python IRC ASCII to image script by e (e@tr0ll.in)
"""
import Image, ImageFont, ImageDraw
import time, re
_strip_colors_regex = re.compile('(\x03([0-9]{1,2})(,[0-9]{1,2})?)|[\x0f\x02\x1f\x03\x16]').sub
def strip_colors(string):
return _strip_colors_regex('', string)
_colorRegex = re.compile('(([0-9]{1,2})(,([0-9]{1,2}))?)')
colors = [16777215, 0, 8323072, 2788394, 255, 127, 10223772, 32764, 65535, 64512, 9671424, 16776960, 16515072, 16711935, 8355711, 13816530]
IGNORE_CHRS = ('\x16','\x1f','\x02', '\x03', '\x0f')
def renderImage(text, size=15, utf8 = False, defaultBg = 0, defaultFg = 1, defaultFont = 'FSEX300.ttf'):
try:
if utf8 and not isinstance(text, unicode):
text = text.decode('utf-8')
except:
pass
lineLens = [len(line) for line in strip_colors(text).split('\n')]
maxWidth, height = max(lineLens), len(lineLens)
font = ImageFont.truetype(defaultFont, size)
fontX, fontY = font.getsize('.')
imageX, imageY = maxWidth * fontX, height * fontY
image = Image.new('RGB', (imageX, imageY), colors[defaultBg])
draw = ImageDraw.Draw(image)
dtext, drect, match, x, y, fg, bg = draw.text, draw.rectangle, _colorRegex.match, 0, 0, defaultFg, defaultBg
start = time.time()
for text in text.split('\n'):
ll, i = len(text), 0
while i < ll:
chr = text[i]
if chr == "\x03":
m = match(text[i+1:i+6])
if m:
i+= len(m.group(1))
fg = int(m.group(2), 10) % 16
if m.group(4) is not None:
bg = int(m.group(4), 10) % 16
else:
bg, fg = defaultBg, defaultFg
elif chr == "\x0f":
fg, bg = defaultFg, defaultBg
elif chr not in IGNORE_CHRS:
if bg != defaultBg: # bg is not white, render it
drect((x, y, x+fontX, y+fontY), fill=colors[bg])
if bg != fg: # text will show, render it. this saves a lot of time!
dtext((x, y), chr, font=font, fill=colors[fg])
x += fontX
i += 1
y += fontY
fg, bg, x = defaultFg, defaultBg, 0
return image, imageX, imageY, time.time()-start
try:
import psyco
psyco.full() # use psycho to speed things up a bit...
except ImportError:
print "psyco failed to import, things will be slower ;["
if __name__ == "__main__":
import urllib
h = urllib.urlopen('ascii.txt').read()
im, x, y, duration = renderImage(h, 10)
print "Rendered image in %.5f seconds" % duration
im.save('tldr.png', "PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment