Conditions | 11 |
Total Lines | 84 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Tests | 48 |
CRAP Score | 11 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like memegen.domain.image._generate() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | 1 | import os |
|
71 | def _generate(top, bottom, font_path, background, width, height, |
||
72 | 1 | watermark, watermark_font_path): |
|
73 | 1 | """Add text to an image and save it.""" |
|
74 | 1 | log.info("Loading background: %s", background) |
|
75 | 1 | background_image = ImageFile.open(background) |
|
76 | 1 | if background_image.mode not in ('RGB', 'RGBA'): |
|
77 | 1 | if background_image.format == 'JPEG': |
|
78 | background_image = background_image.convert('RGB') |
||
79 | 1 | background_image.format = 'JPEG' |
|
80 | 1 | else: |
|
81 | background_image = background_image.convert('RGBA') |
||
82 | background_image.format = 'PNG' |
||
83 | 1 | ||
84 | 1 | # Resize to a maximum height and width |
|
85 | 1 | ratio = background_image.size[0] / background_image.size[1] |
|
86 | 1 | pad_image = bool(width and height) |
|
87 | pad_watermark = True |
||
88 | 1 | if pad_image: |
|
89 | 1 | if width < height * ratio: |
|
90 | 1 | dimensions = width, int(width / ratio) |
|
91 | 1 | pad_watermark = False |
|
92 | 1 | else: |
|
93 | dimensions = int(height * ratio), height |
||
94 | 1 | elif width: |
|
95 | 1 | dimensions = width, int(width / ratio) |
|
96 | 1 | elif height: |
|
97 | dimensions = int(height * ratio), height |
||
98 | else: |
||
99 | 1 | dimensions = 600, int(600 / ratio) |
|
100 | dimensions = _fit_image(*dimensions) |
||
101 | 1 | image = background_image.resize(dimensions, ImageFile.LANCZOS) |
|
102 | 1 | image.format = 'PNG' |
|
103 | 1 | ||
104 | 1 | # Draw image |
|
105 | draw = ImageDraw.Draw(image) |
||
106 | |||
107 | max_font_size = int(image.size[1] / 9) |
||
108 | 1 | min_font_size_single_line = int(image.size[1] / 12) |
|
109 | max_text_len = image.size[0] - 20 |
||
110 | top_font_size, top = _optimize_font_size( |
||
111 | font_path, top, max_font_size, |
||
112 | min_font_size_single_line, max_text_len, |
||
113 | 1 | ) |
|
114 | 1 | bottom_font_size, bottom = _optimize_font_size( |
|
115 | font_path, bottom, max_font_size, |
||
116 | 1 | min_font_size_single_line, max_text_len, |
|
117 | 1 | ) |
|
118 | |||
119 | top_font = ImageFont.truetype(font_path, top_font_size) |
||
120 | 1 | bottom_font = ImageFont.truetype(font_path, bottom_font_size) |
|
121 | 1 | ||
122 | 1 | top_text_size = draw.multiline_textsize(top, top_font) |
|
123 | bottom_text_size = draw.multiline_textsize(bottom, bottom_font) |
||
124 | |||
125 | 1 | # Find top centered position for top text |
|
126 | 1 | top_text_pos_x = (image.size[0] / 2) - (top_text_size[0] / 2) |
|
127 | 1 | top_text_pos_y = 0 |
|
128 | 1 | top_text_pos = (top_text_pos_x, top_text_pos_y) |
|
129 | 1 | ||
130 | # Find bottom centered position for bottom text |
||
131 | 1 | bottom_text_size_x = (image.size[0] / 2) - (bottom_text_size[0] / 2) |
|
132 | bottom_text_size_y = image.size[1] - bottom_text_size[1] * (7 / 6) |
||
133 | 1 | if watermark and pad_watermark: |
|
134 | bottom_text_size_y = bottom_text_size_y - 5 |
||
135 | bottom_text_pos = (bottom_text_size_x, bottom_text_size_y) |
||
136 | |||
137 | 1 | outline = int(round(max(1, min(dimensions) / 300))) |
|
138 | 1 | t_outline = min(outline, top_font_size // 20) |
|
139 | b_outline = min(outline, bottom_font_size // 20) |
||
140 | _draw_outlined_text(image, top_text_pos, top, top_font, t_outline) |
||
141 | 1 | _draw_outlined_text(image, bottom_text_pos, bottom, bottom_font, b_outline) |
|
142 | 1 | ||
143 | 1 | # Pad image if a specific dimension is requested |
|
144 | 1 | if pad_image: |
|
145 | image = _add_blurred_background(image, background_image, width, height) |
||
146 | |||
147 | 1 | # Add watermark |
|
148 | if watermark: |
||
149 | watermark_font = ImageFont.truetype(watermark_font_path, 11) |
||
150 | 1 | watermark_pos = (3, image.size[1] - 15) |
|
151 | _draw_outlined_text(image, watermark_pos, watermark, watermark_font, |
||
152 | alpha=True) |
||
153 | |||
154 | return image |
||
155 | 1 | ||
279 |