aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/media_types/ascii/asciitoimage.py
blob: 8b32986e5f8bd5f03c468cb6b0ff25027df992fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

try:
    from PIL import Image
    from PIL import ImageFont
    from PIL import ImageDraw
except ImportError:
    import Image
    import ImageFont
    import ImageDraw
import logging
import pkg_resources
import os

_log = logging.getLogger(__name__)


class AsciiToImage:
    '''
    Converter of ASCII art into image files, preserving whitespace

    kwargs:
    - font: Path to font file
      default: fonts/Inconsolata.otf
    - font_size: Font size, ``int``
      default: 11
    '''
    def __init__(self, **kw):
        self._font = kw.get('font', pkg_resources.resource_filename(
                'mediagoblin.media_types.ascii',
                os.path.join('fonts', 'Inconsolata.otf')))

        self._font_size = kw.get('font_size', 11)

        self._if = ImageFont.truetype(
            self._font,
            self._font_size,
            encoding='unic')

        _log.info('Font set to {}, size {}'.format(
                self._font,
                self._font_size))

        #      ,-,-^-'-^'^-^'^-'^-.
        #     ( I am a wall socket )Oo,  ___
        #      `-.,.-.,.-.-.,.-.--'     '   `
        # Get the size, in pixels of the '.' character
        self._if_dims = self._if.getsize('.')
        #                               `---'

    def convert(self, text, destination):
        # TODO: Detect if text is a file-like, if so, act accordingly
        im = self._create_image(text)

        # PIL's Image.save will handle both file-likes and paths
        if im.save(destination):
            _log.info('Saved image in {}'.format(
                    destination))

    def _create_image(self, text):
        '''
        Write characters to a PIL image canvas.

        TODO:
        - Character set detection and decoding,
          http://pypi.python.org/pypi/chardet
        '''
        _log.debug('Drawing image')
        # Convert the input from str to unicode
        text = text.decode('utf-8')

        # TODO: Account for alternative line endings
        lines = text.split('\n')

        line_lengths = [len(i) for i in lines]

        # Calculate destination size based on text input and character size
        im_dims = (
            max(line_lengths) * self._if_dims[0],
            len(line_lengths) * self._if_dims[1])

        _log.info('Destination image dimensions will be {}'.format(
                im_dims))

        im = Image.new(
            'RGBA',
            im_dims,
            (255, 255, 255, 0))

        draw = ImageDraw.Draw(im)

        char_pos = [0, 0]

        for line in lines:
            line_length = len(line)

            _log.debug(f'Writing line at {char_pos}')

            for _pos in range(0, line_length):
                char = line[_pos]

                px_pos = self._px_pos(char_pos)

                _log.debug('Writing character "{}" at {} (px pos {})'.format(
                        char.encode('ascii', 'replace'),
                        char_pos,
                        px_pos))

                draw.text(
                    px_pos,
                    char,
                    font=self._if,
                    fill=(0, 0, 0, 255))

                char_pos[0] += 1

            # Reset X position, increment Y position
            char_pos[0] = 0
            char_pos[1] += 1

        return im

    def _px_pos(self, char_pos):
        '''
        Helper function to calculate the pixel position based on
        character position and character dimensions
        '''
        px_pos = [0, 0]
        for index, val in zip(range(0, len(char_pos)), char_pos):
                px_pos[index] = char_pos[index] * self._if_dims[index]

        return px_pos