docs: Document video resolution config.
[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
d0e9f843
AL
17try:
18 from PIL import Image
19 from PIL import ImageFont
20 from PIL import ImageDraw
21except ImportError:
22 import Image
23 import ImageFont
24 import ImageDraw
a246ccca
JW
25import logging
26import pkg_resources
27import os
28
29_log = logging.getLogger(__name__)
30
c56d4b55 31
a246ccca
JW
32class AsciiToImage(object):
33 '''
34 Converter of ASCII art into image files, preserving whitespace
35
36 kwargs:
37 - font: Path to font file
38 default: fonts/Inconsolata.otf
39 - font_size: Font size, ``int``
40 default: 11
41 '''
a246ccca 42 def __init__(self, **kw):
196a5181 43 self._font = kw.get('font', pkg_resources.resource_filename(
a246ccca 44 'mediagoblin.media_types.ascii',
196a5181 45 os.path.join('fonts', 'Inconsolata.otf')))
a246ccca 46
196a5181 47 self._font_size = kw.get('font_size', 11)
a246ccca 48
a246ccca
JW
49 self._if = ImageFont.truetype(
50 self._font,
010d28b4
JW
51 self._font_size,
52 encoding='unic')
a246ccca 53
64da09e8
JW
54 _log.info('Font set to {0}, size {1}'.format(
55 self._font,
56 self._font_size))
57
a246ccca
JW
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('.')
63 # `---'
64
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)
68
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(
72 destination))
73
74 def _create_image(self, text):
75 '''
76 Write characters to a PIL image canvas.
77
78 TODO:
79 - Character set detection and decoding,
80 http://pypi.python.org/pypi/chardet
81 '''
64da09e8 82 _log.debug('Drawing image')
010d28b4
JW
83 # Convert the input from str to unicode
84 text = text.decode('utf-8')
85
a246ccca
JW
86 # TODO: Account for alternative line endings
87 lines = text.split('\n')
88
89 line_lengths = [len(i) for i in lines]
90
91 # Calculate destination size based on text input and character size
92 im_dims = (
93 max(line_lengths) * self._if_dims[0],
94 len(line_lengths) * self._if_dims[1])
95
96 _log.info('Destination image dimensions will be {0}'.format(
97 im_dims))
98
99 im = Image.new(
100 'RGBA',
101 im_dims,
102 (255, 255, 255, 0))
103
104 draw = ImageDraw.Draw(im)
105
106 char_pos = [0, 0]
107
108 for line in lines:
109 line_length = len(line)
110
111 _log.debug('Writing line at {0}'.format(char_pos))
112
113 for _pos in range(0, line_length):
114 char = line[_pos]
115
116 px_pos = self._px_pos(char_pos)
117
010d28b4 118 _log.debug('Writing character "{0}" at {1} (px pos {2})'.format(
64da09e8 119 char.encode('ascii', 'replace'),
a246ccca
JW
120 char_pos,
121 px_pos))
122
123 draw.text(
124 px_pos,
125 char,
126 font=self._if,
127 fill=(0, 0, 0, 255))
128
129 char_pos[0] += 1
130
131 # Reset X position, increment Y position
132 char_pos[0] = 0
133 char_pos[1] += 1
134
135 return im
136
137 def _px_pos(self, char_pos):
138 '''
139 Helper function to calculate the pixel position based on
140 character position and character dimensions
141 '''
142 px_pos = [0, 0]
143 for index, val in zip(range(0, len(char_pos)), char_pos):
144 px_pos[index] = char_pos[index] * self._if_dims[index]
145
146 return px_pos