1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
24 _log
= logging
.getLogger(__name__
)
26 class AsciiToImage(object):
28 Converter of ASCII art into image files, preserving whitespace
31 - font: Path to font file
32 default: fonts/Inconsolata.otf
33 - font_size: Font size, ``int``
51 def __init__(self
, **kw
):
53 self
._font
= kw
.get('font')
55 self
._font
= pkg_resources
.resource_filename(
56 'mediagoblin.media_types.ascii',
57 os
.path
.join('fonts', 'Inconsolata.otf'))
59 if kw
.get('font_size'):
60 self
._font
_size
= kw
.get('font_size')
62 _log
.info('Setting font to {0}, size {1}'.format(
66 self
._if
= ImageFont
.truetype(
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('.')
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
)
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(
86 def _create_image(self
, text
):
88 Write characters to a PIL image canvas.
91 - Character set detection and decoding,
92 http://pypi.python.org/pypi/chardet
94 # TODO: Account for alternative line endings
95 lines
= text
.split('\n')
97 line_lengths
= [len(i
) for i
in lines
]
99 # Calculate destination size based on text input and character size
101 max(line_lengths
) * self
._if
_dims
[0],
102 len(line_lengths
) * self
._if
_dims
[1])
104 _log
.info('Destination image dimensions will be {0}'.format(
112 draw
= ImageDraw
.Draw(im
)
117 line_length
= len(line
)
119 _log
.debug('Writing line at {0}'.format(char_pos
))
121 for _pos
in range(0, line_length
):
124 px_pos
= self
._px
_pos
(char_pos
)
126 _log
.debug('Writing character "{0}" at {1} (px pos {2}'.format(
139 # Reset X position, increment Y position
145 def _px_pos(self
, char_pos
):
147 Helper function to calculate the pixel position based on
148 character position and character dimensions
151 for index
, val
in zip(range(0, len(char_pos
)), char_pos
):
152 px_pos
[index
] = char_pos
[index
] * self
._if
_dims
[index
]
157 if __name__
== "__main__":
159 txt
= urllib
.urlopen('file:///home/joar/Dropbox/ascii/install-all-the-dependencies.txt')
161 _log
.setLevel(logging
.DEBUG
)
162 logging
.basicConfig()
164 converter
= AsciiToImage()
166 converter
.convert(txt
.read(), '/tmp/test.png')
169 im, x, y, duration = renderImage(h, 10)
170 print "Rendered image in %.5f seconds" % duration
171 im.save('tldr.png', "PNG")