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