Completed
Pull Request — master (#389)
by Jace
06:10
created

_add_blurred_background()   B

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 5.2516

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 25
ccs 1
cts 15
cp 0.0667
crap 5.2516
rs 8.8571
c 1
b 0
f 0
1 1
import os
2 1
import hashlib
3 1
import logging
4
5 1
from PIL import Image as ImageFile, ImageFont, ImageDraw, ImageFilter
0 ignored issues
show
Configuration introduced by
The import PIL could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class Image(object):
12
    """JPEG generated by applying text to a template."""
13
14 1
    def __init__(self, template, text, root=None,
15
                 style=None, font=None, size=None):
16 1
        self.root = root
17 1
        self.template = template
18 1
        self.style = style
19 1
        self.text = text
20 1
        self.font = font
21 1
        self.width = size.get('width') if size else None
22 1
        self.height = size.get('height') if size else None
23
24 1
    @property
25
    def path(self):
26 1
        if not self.root:
27
            return None
28
29 1
        base = os.path.join(self.root, self.template.key, self.text.path)
30 1
        custom = [self.style, self.font, self.width, self.height]
31
32 1
        if any(custom):
33 1
            slug = self.hash(custom)
34 1
            return "{}#{}.img".format(base, slug)
35
        else:
36 1
            return base + ".img"
37
38 1
    @staticmethod
39
    def hash(values):
40 1
        sha = hashlib.md5()
41 1
        for index, value in enumerate(values):
42 1
            sha.update("{}:{}".format(index, value or "").encode('utf-8'))
43 1
        return sha.hexdigest()
44
45 1
    def save(self):
46 1
        data = _generate(
47
            top=self.text.top, bottom=self.text.bottom,
48
            font=self.font.path,
49
            background=self.template.get_path(self.style),
50
            width=self.width, height=self.height,
51
        )
52
53 1
        directory = os.path.dirname(self.path)
54 1
        if not os.path.isdir(directory):
55 1
            os.makedirs(directory)
56
57 1
        log.info("Saving image: %s", self.path)
58 1
        path = data.save(self.path, format=data.format)
59
60 1
        return path
61
62
63
# The following Pillow image functions are based on:
64
# https://github.com/danieldiekmeier/memegenerator
65
66
67 1
def _generate(top, bottom, font, background, width, height):
68
    """Add text to an image and save it."""
69 1
    log.info("Loading background: %s", background)
70 1
    background_image = ImageFile.open(background)
71 1
    if background_image.mode not in ('RGB', 'RGBA'):
72 1
        if background_image.format == 'JPEG':
73 1
            background_image = background_image.convert('RGB')
74 1
            background_image.format = 'JPEG'
75
        else:
76 1
            background_image = background_image.convert('RGBA')
77 1
            background_image.format = 'PNG'
78
79
    # Resize to a maximum height and width
80 1
    ratio = background_image.size[0] / background_image.size[1]
81 1
    if width and height:
82
        if width < height * ratio:
83
            dimensions = width, int(width / ratio)
84
        else:
85
            dimensions = int(height * ratio), height
86 1
    elif width:
87
        dimensions = width, int(width / ratio)
88 1
    elif height:
89
        dimensions = int(height * ratio), height
90
    else:
91 1
        dimensions = 600, int(600 / ratio)
92 1
    image = background_image.resize(dimensions, ImageFile.LANCZOS)
93 1
    image.format = 'PNG'
94
95
    # Draw image
96 1
    draw = ImageDraw.Draw(image)
97
98 1
    max_font_size = int(image.size[1] / 5)
99 1
    min_font_size_single_line = int(image.size[1] / 12)
100 1
    max_text_len = image.size[0] - 20
101 1
    top_font_size, top = _optimize_font_size(font, top, max_font_size,
102
                                             min_font_size_single_line,
103
                                             max_text_len)
104 1
    bottom_font_size, bottom = _optimize_font_size(font, bottom, max_font_size,
105
                                                   min_font_size_single_line,
106
                                                   max_text_len)
107
108 1
    top_font = ImageFont.truetype(font, top_font_size)
109 1
    bottom_font = ImageFont.truetype(font, bottom_font_size)
110
111 1
    top_text_size = draw.multiline_textsize(top, top_font)
112 1
    bottom_text_size = draw.multiline_textsize(bottom, bottom_font)
113
114
    # Find top centered position for top text
115 1
    top_text_position_x = (image.size[0] / 2) - (top_text_size[0] / 2)
116 1
    top_text_position_y = 0
117 1
    top_text_position = (top_text_position_x, top_text_position_y)
118
119
    # Find bottom centered position for bottom text
120 1
    bottom_text_size_x = (image.size[0] / 2) - (bottom_text_size[0] / 2)
121 1
    bottom_text_size_y = image.size[1] - bottom_text_size[1] * (7 / 6)
122 1
    bottom_text_position = (bottom_text_size_x, bottom_text_size_y)
123
124 1
    _draw_outlined_text(draw, top_text_position,
125
                        top, top_font, top_font_size)
126 1
    _draw_outlined_text(draw, bottom_text_position,
127
                        bottom, bottom_font, bottom_font_size)
128
129
    # Pad image if a specific dimension is requested
130 1
    if width and height:
131
        image = _add_blurred_background(image, background_image, width, height)
132
133 1
    return image
134
135
136 1
def _optimize_font_size(font, text, max_font_size, min_font_size,
137
                        max_text_len):
138
    """Calculate the optimal font size to fit text in a given size."""
139
140
    # Check size when using smallest single line font size
141 1
    fontobj = ImageFont.truetype(font, min_font_size)
142 1
    text_size = fontobj.getsize(text)
143
144
    # Calculate font size for text, split if necessary
145 1
    if text_size[0] > max_text_len:
146 1
        phrases = _split(text)
147
    else:
148 1
        phrases = (text,)
149 1
    font_size = max_font_size // len(phrases)
150 1
    for phrase in phrases:
151 1
        font_size = min(_maximize_font_size(font, phrase, max_text_len),
152
                        font_size)
153
154
    # Rebuild text with new lines
155 1
    text = '\n'.join(phrases)
156
157 1
    return font_size, text
158
159
160 1
def _draw_outlined_text(draw_image, text_position, text, font, font_size):
161
    """Draw white text with black outline on an image."""
162
163
    # Draw black text outlines
164 1
    outline_range = max(1, font_size // 25)
165 1
    for x in range(-outline_range, outline_range + 1):
166 1
        for y in range(-outline_range, outline_range + 1):
167 1
            pos = (text_position[0] + x, text_position[1] + y)
168 1
            draw_image.multiline_text(pos, text, (0, 0, 0),
169
                                      font=font, align='center')
170
171
    # Draw inner white text
172 1
    draw_image.multiline_text(text_position, text, (255, 255, 255),
173
                              font=font, align='center')
174
175
176 1
def _add_blurred_background(foreground, background, width, height):
177
    """Add a blurred background to match the requested dimensions."""
178
    base_width, base_height = foreground.size
179
180
    border_width = min(width, base_width + 2)
181
    border_height = min(height, base_height + 2)
182
    border_dimensions = border_width, border_height
183
    border = ImageFile.new('RGB', border_dimensions)
184
    border.paste(foreground, ((border_width - base_width) // 2,
185
                              (border_height - base_height) // 2))
186
187
    padded_dimensions = (width, height)
188
    padded = background.resize(padded_dimensions, ImageFile.LANCZOS)
189
190
    darkened = padded.point(lambda p: p * 0.4)
191
192
    blurred = darkened.filter(ImageFilter.GaussianBlur(5))
193
    blurred.format = 'PNG'
194
195
    blurred_width, blurred_height = blurred.size
196
    offset = ((blurred_width - border_width) // 2,
197
              (blurred_height - border_height) // 2)
198
    blurred.paste(border, offset)
199
200
    return blurred
201 1
202
203 1
def _maximize_font_size(font, text, max_size):
204
    """Find the biggest font size that will fit."""
205 1
    font_size = max_size
206 1
207 1
    fontobj = ImageFont.truetype(font, font_size)
208 1
    text_size = fontobj.getsize(text)
209 1
    while text_size[0] > max_size and font_size > 1:
210 1
        font_size = font_size - 1
211
        fontobj = ImageFont.truetype(font, font_size)
212 1
        text_size = fontobj.getsize(text)
213
214
    return font_size
215 1
216
217
def _split(text):
218
    """Split a line of text into two similarly sized pieces.
219
220
    >>> _split("Hello, world!")
221
    ('Hello,', 'world!')
222
223
    >>> _split("This is a phrase that can be split.")
224
    ('This is a phrase', 'that can be split.')
225
226
    >>> _split("This_is_a_phrase_that_can_not_be_split.")
227
    ('This_is_a_phrase_that_can_not_be_split.',)
228 1
229
    """
230 1
    result = (text,)
231 1
232 1
    if len(text) >= 3 and ' ' in text[1:-1]:  # can split this string
233 1
        space_indices = [i for i in range(len(text)) if text[i] == ' ']
234 1
        space_proximities = [abs(i - len(text) // 2) for i in space_indices]
235 1
        for i, j in zip(space_proximities, space_indices):
236 1
            if i == min(space_proximities):
237
                result = (text[:j], text[j + 1:])
238 1
                break
239
240
    return result
241