It's 2012 all up in here
[mediagoblin.git] / mediagoblin / media_types / ascii / asciitoimage.py
CommitLineData
a246ccca 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
a246ccca
JW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import Image
18import ImageFont
19import ImageDraw
20import logging
21import pkg_resources
22import os
23
24_log = logging.getLogger(__name__)
25
26class AsciiToImage(object):
27 '''
28 Converter of ASCII art into image files, preserving whitespace
29
30 kwargs:
31 - font: Path to font file
32 default: fonts/Inconsolata.otf
33 - font_size: Font size, ``int``
34 default: 11
35 '''
36
37 # Font file path
38 _font = None
39
40 _font_size = 11
41
42 # ImageFont instance
43 _if = None
44
45 # ImageFont
46 _if_dims = None
47
48 # Image instance
49 _im = None
50
51 def __init__(self, **kw):
52 if kw.get('font'):
53 self._font = kw.get('font')
54 else:
55 self._font = pkg_resources.resource_filename(
56 'mediagoblin.media_types.ascii',
57 os.path.join('fonts', 'Inconsolata.otf'))
58
59 if kw.get('font_size'):
60 self._font_size = kw.get('font_size')
61
62 _log.info('Setting font to {0}, size {1}'.format(
63 self._font,
64 self._font_size))
65
66 self._if = ImageFont.truetype(
67 self._font,
68 self._font_size)
69
70 # ,-,-^-'-^'^-^'^-'^-.
71 # ( I am a wall socket )Oo, ___
72 # `-.,.-.,.-.-.,.-.--' ' `
73 # Get the size, in pixels of the '.' character
74 self._if_dims = self._if.getsize('.')
75 # `---'
76
77 def convert(self, text, destination):
78 # TODO: Detect if text is a file-like, if so, act accordingly
79 im = self._create_image(text)
80
81 # PIL's Image.save will handle both file-likes and paths
82 if im.save(destination):
83 _log.info('Saved image in {0}'.format(
84 destination))
85
86 def _create_image(self, text):
87 '''
88 Write characters to a PIL image canvas.
89
90 TODO:
91 - Character set detection and decoding,
92 http://pypi.python.org/pypi/chardet
93 '''
94 # TODO: Account for alternative line endings
95 lines = text.split('\n')
96
97 line_lengths = [len(i) for i in lines]
98
99 # Calculate destination size based on text input and character size
100 im_dims = (
101 max(line_lengths) * self._if_dims[0],
102 len(line_lengths) * self._if_dims[1])
103
104 _log.info('Destination image dimensions will be {0}'.format(
105 im_dims))
106
107 im = Image.new(
108 'RGBA',
109 im_dims,
110 (255, 255, 255, 0))
111
112 draw = ImageDraw.Draw(im)
113
114 char_pos = [0, 0]
115
116 for line in lines:
117 line_length = len(line)
118
119 _log.debug('Writing line at {0}'.format(char_pos))
120
121 for _pos in range(0, line_length):
122 char = line[_pos]
123
124 px_pos = self._px_pos(char_pos)
125
126 _log.debug('Writing character "{0}" at {1} (px pos {2}'.format(
127 char,
128 char_pos,
129 px_pos))
130
131 draw.text(
132 px_pos,
133 char,
134 font=self._if,
135 fill=(0, 0, 0, 255))
136
137 char_pos[0] += 1
138
139 # Reset X position, increment Y position
140 char_pos[0] = 0
141 char_pos[1] += 1
142
143 return im
144
145 def _px_pos(self, char_pos):
146 '''
147 Helper function to calculate the pixel position based on
148 character position and character dimensions
149 '''
150 px_pos = [0, 0]
151 for index, val in zip(range(0, len(char_pos)), char_pos):
152 px_pos[index] = char_pos[index] * self._if_dims[index]
153
154 return px_pos
155
156
157if __name__ == "__main__":
158 import urllib
159 txt = urllib.urlopen('file:///home/joar/Dropbox/ascii/install-all-the-dependencies.txt')
160
161 _log.setLevel(logging.DEBUG)
162 logging.basicConfig()
163
164 converter = AsciiToImage()
165
166 converter.convert(txt.read(), '/tmp/test.png')
167
168 '''
169 im, x, y, duration = renderImage(h, 10)
170 print "Rendered image in %.5f seconds" % duration
171 im.save('tldr.png', "PNG")
172 '''