| Conditions | 11 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 46 |
| 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 _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.
| 1 | 1 | import os |
|
| 66 | 1 | def _generate(top, bottom, font, background, width, height, watermark): |
|
| 67 | """Add text to an image and save it.""" |
||
| 68 | 1 | log.info("Loading background: %s", background) |
|
| 69 | 1 | background_image = ImageFile.open(background) |
|
| 70 | 1 | if background_image.mode not in ('RGB', 'RGBA'): |
|
| 71 | 1 | if background_image.format == 'JPEG': |
|
| 72 | 1 | background_image = background_image.convert('RGB') |
|
| 73 | 1 | background_image.format = 'JPEG' |
|
| 74 | else: |
||
| 75 | 1 | background_image = background_image.convert('RGBA') |
|
| 76 | 1 | background_image.format = 'PNG' |
|
| 77 | |||
| 78 | # Resize to a maximum height and width |
||
| 79 | 1 | ratio = background_image.size[0] / background_image.size[1] |
|
| 80 | 1 | if width and height: |
|
| 81 | 1 | if width < height * ratio: |
|
| 82 | 1 | dimensions = width, int(width / ratio) |
|
| 83 | else: |
||
| 84 | 1 | dimensions = int(height * ratio), height |
|
| 85 | 1 | elif width: |
|
| 86 | 1 | dimensions = width, int(width / ratio) |
|
| 87 | 1 | elif height: |
|
| 88 | 1 | dimensions = int(height * ratio), height |
|
| 89 | else: |
||
| 90 | 1 | dimensions = 600, int(600 / ratio) |
|
| 91 | 1 | image = background_image.resize(dimensions, ImageFile.LANCZOS) |
|
| 92 | 1 | image.format = 'PNG' |
|
| 93 | |||
| 94 | # Draw image |
||
| 95 | 1 | draw = ImageDraw.Draw(image) |
|
| 96 | |||
| 97 | 1 | max_font_size = int(image.size[1] / 5) |
|
| 98 | 1 | min_font_size_single_line = int(image.size[1] / 12) |
|
| 99 | 1 | max_text_len = image.size[0] - 20 |
|
| 100 | 1 | top_font_size, top = _optimize_font_size(font, top, max_font_size, |
|
| 101 | min_font_size_single_line, |
||
| 102 | max_text_len) |
||
| 103 | 1 | bottom_font_size, bottom = _optimize_font_size(font, bottom, max_font_size, |
|
| 104 | min_font_size_single_line, |
||
| 105 | max_text_len) |
||
| 106 | |||
| 107 | 1 | top_font = ImageFont.truetype(font, top_font_size) |
|
| 108 | 1 | bottom_font = ImageFont.truetype(font, bottom_font_size) |
|
| 109 | |||
| 110 | 1 | top_text_size = draw.multiline_textsize(top, top_font) |
|
| 111 | 1 | bottom_text_size = draw.multiline_textsize(bottom, bottom_font) |
|
| 112 | |||
| 113 | # Find top centered position for top text |
||
| 114 | 1 | top_text_position_x = (image.size[0] / 2) - (top_text_size[0] / 2) |
|
| 115 | 1 | top_text_position_y = 0 |
|
| 116 | 1 | top_text_position = (top_text_position_x, top_text_position_y) |
|
| 117 | |||
| 118 | # Find bottom centered position for bottom text |
||
| 119 | 1 | bottom_text_size_x = (image.size[0] / 2) - (bottom_text_size[0] / 2) |
|
| 120 | 1 | bottom_text_size_y = image.size[1] - bottom_text_size[1] * (7 / 6) |
|
| 121 | 1 | bottom_text_position = (bottom_text_size_x, bottom_text_size_y) |
|
| 122 | |||
| 123 | 1 | _draw_outlined_text(draw, top_text_position, |
|
| 124 | top, top_font, top_font_size) |
||
| 125 | 1 | _draw_outlined_text(draw, bottom_text_position, |
|
| 126 | bottom, bottom_font, bottom_font_size) |
||
| 127 | |||
| 128 | # Pad image if a specific dimension is requested |
||
| 129 | 1 | if width and height: |
|
| 130 | 1 | image = _add_blurred_background(image, background_image, width, height) |
|
| 131 | |||
| 132 | # Add watermark |
||
| 133 | 1 | if watermark: |
|
| 134 | 1 | draw = ImageDraw.Draw(image) |
|
| 135 | 1 | watermark_font = ImageFont.truetype(font, 15) |
|
| 136 | 1 | _draw_outlined_text(draw, (3, image.size[1] - 20), |
|
| 137 | watermark, watermark_font, 15) |
||
| 138 | |||
| 139 | 1 | return image |
|
| 140 | |||
| 247 |
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.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.