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__
)
27 class AsciiToImage(object):
29 Converter of ASCII art into image files, preserving whitespace
32 - font: Path to font file
33 default: fonts/Inconsolata.otf
34 - font_size: Font size, ``int``
37 def __init__(self
, **kw
):
38 self
._font
= kw
.get('font', pkg_resources
.resource_filename(
39 'mediagoblin.media_types.ascii',
40 os
.path
.join('fonts', 'Inconsolata.otf')))
42 self
._font
_size
= kw
.get('font_size', 11)
44 self
._if
= ImageFont
.truetype(
49 _log
.info('Font set to {0}, size {1}'.format(
53 # ,-,-^-'-^'^-^'^-'^-.
54 # ( I am a wall socket )Oo, ___
55 # `-.,.-.,.-.-.,.-.--' ' `
56 # Get the size, in pixels of the '.' character
57 self
._if
_dims
= self
._if
.getsize('.')
60 def convert(self
, text
, destination
):
61 # TODO: Detect if text is a file-like, if so, act accordingly
62 im
= self
._create
_image
(text
)
64 # PIL's Image.save will handle both file-likes and paths
65 if im
.save(destination
):
66 _log
.info('Saved image in {0}'.format(
69 def _create_image(self
, text
):
71 Write characters to a PIL image canvas.
74 - Character set detection and decoding,
75 http://pypi.python.org/pypi/chardet
77 _log
.debug('Drawing image')
78 # Convert the input from str to unicode
79 text
= text
.decode('utf-8')
81 # TODO: Account for alternative line endings
82 lines
= text
.split('\n')
84 line_lengths
= [len(i
) for i
in lines
]
86 # Calculate destination size based on text input and character size
88 max(line_lengths
) * self
._if
_dims
[0],
89 len(line_lengths
) * self
._if
_dims
[1])
91 _log
.info('Destination image dimensions will be {0}'.format(
99 draw
= ImageDraw
.Draw(im
)
104 line_length
= len(line
)
106 _log
.debug('Writing line at {0}'.format(char_pos
))
108 for _pos
in range(0, line_length
):
111 px_pos
= self
._px
_pos
(char_pos
)
113 _log
.debug('Writing character "{0}" at {1} (px pos {2})'.format(
114 char
.encode('ascii', 'replace'),
126 # Reset X position, increment Y position
132 def _px_pos(self
, char_pos
):
134 Helper function to calculate the pixel position based on
135 character position and character dimensions
138 for index
, val
in zip(range(0, len(char_pos
)), char_pos
):
139 px_pos
[index
] = char_pos
[index
] * self
._if
_dims
[index
]