Conditions | 13 |
Total Lines | 73 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
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 modules.memegenerator.make_meme() 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 | #!/usr/bin/python |
||
9 | def make_meme(topString, bottomString, filename, extension, path, meme_font): |
||
10 | img = Image.open(path + filename + extension) |
||
11 | imageSize = img.size |
||
12 | wrapwidth = int(imageSize[0]/20) |
||
13 | |||
14 | # wrap input text strings |
||
15 | if bottomString is None: |
||
16 | bottomString = [" "] |
||
17 | else: |
||
18 | bottomString = textwrap.wrap(bottomString ,width=wrapwidth) |
||
19 | if topString is None: |
||
20 | topString = [" "] |
||
21 | else: |
||
22 | topString = textwrap.wrap(topString ,width=wrapwidth) |
||
23 | |||
24 | # longest line to find font size |
||
25 | longestTopString = max(topString, key=len) |
||
26 | longestBottomString = max(bottomString, key=len) |
||
27 | |||
28 | # find biggest font size that works |
||
29 | fontSize = int(imageSize[1]/6) |
||
30 | font = ImageFont.truetype(meme_font, fontSize) |
||
31 | topTextSize = font.getsize(longestTopString) |
||
32 | bottomTextSize = font.getsize(longestBottomString) |
||
33 | while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20: |
||
34 | fontSize = fontSize - 1 |
||
35 | font = ImageFont.truetype(meme_font, fontSize) |
||
36 | topTextSize = font.getsize(longestTopString) |
||
37 | bottomTextSize = font.getsize(longestBottomString) |
||
38 | |||
39 | # find top centered position for top text |
||
40 | topPositions = [] |
||
41 | initialX = 0 - topTextSize[1] |
||
42 | for i in topString: |
||
43 | topTextLineSize = font.getsize(i) |
||
44 | topTextPositionX = (imageSize[0]/2) - (topTextLineSize[0]/2) |
||
45 | topTextPositionY = initialX + topTextSize[1] |
||
46 | initialX = topTextPositionY |
||
47 | topTextPosition = (topTextPositionX, topTextPositionY) |
||
48 | topPositions.append(topTextPosition) |
||
49 | |||
50 | # find bottom centered position for bottom text |
||
51 | bottomPositions = [] |
||
52 | initialY = imageSize[1] - bottomTextSize[1]*len(bottomString)-15 - bottomTextSize[1] |
||
53 | for i in bottomString: |
||
54 | bottomTextLineSize = font.getsize(i) |
||
55 | bottomTextPositionX = (imageSize[0]/2) - (bottomTextLineSize[0]/2) |
||
56 | bottomTextPositionY = initialY + bottomTextSize[1] |
||
57 | initialY = bottomTextPositionY |
||
58 | bottomTextPosition = (bottomTextPositionX, bottomTextPositionY) |
||
59 | bottomPositions.append(bottomTextPosition) |
||
60 | |||
61 | draw = ImageDraw.Draw(img) |
||
62 | |||
63 | # draw outlines |
||
64 | outlineRange = int(fontSize/25)+1 |
||
65 | for x in range(-outlineRange, outlineRange+1): |
||
66 | for y in range(-outlineRange, outlineRange+1): |
||
67 | ct = 0 |
||
68 | cb = 0 |
||
69 | for i in topPositions: |
||
70 | draw.text((i[0]+x, i[1]+y), topString[ct], (0, 0, 0), font=font) |
||
71 | ct += 1 |
||
72 | for i in bottomPositions: |
||
73 | draw.text((i[0]+x, i[1]+y), bottomString[cb], (0, 0, 0), font=font) |
||
74 | cb += 1 |
||
75 | |||
76 | for i in range(len(topString)): |
||
77 | draw.text(topPositions[i], topString[i], (255, 255, 255), font=font) |
||
78 | for i in range(len(bottomString)): |
||
79 | draw.text(bottomPositions[i], bottomString[i], (255, 255, 255), font=font) |
||
80 | |||
81 | img.save(path+filename+"-meme"+extension) |
||
82 |