|
2 | 2 |
|
3 | 3 | from PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter |
4 | 4 |
|
5 | | -class ComputerTextGenerator(object): |
6 | | - @classmethod |
7 | | - def generate(cls, text, font, text_color, font_size, orientation, space_width, fit): |
8 | | - if orientation == 0: |
9 | | - return cls.__generate_horizontal_text(text, font, text_color, font_size, space_width, fit) |
10 | | - elif orientation == 1: |
11 | | - return cls.__generate_vertical_text(text, font, text_color, font_size, space_width, fit) |
12 | | - else: |
13 | | - raise ValueError("Unknown orientation " + str(orientation)) |
| 5 | +def generate(text, font, text_color, font_size, orientation, space_width, fit): |
| 6 | + if orientation == 0: |
| 7 | + return _generate_horizontal_text(text, font, text_color, font_size, space_width, fit) |
| 8 | + elif orientation == 1: |
| 9 | + return _generate_vertical_text(text, font, text_color, font_size, space_width, fit) |
| 10 | + else: |
| 11 | + raise ValueError("Unknown orientation " + str(orientation)) |
| 12 | + |
| 13 | +def _generate_horizontal_text(text, font, text_color, font_size, space_width, fit): |
| 14 | + image_font = ImageFont.truetype(font=font, size=font_size) |
| 15 | + words = text.split(' ') |
| 16 | + space_width = image_font.getsize(' ')[0] * space_width |
| 17 | + |
| 18 | + words_width = [image_font.getsize(w)[0] for w in words] |
| 19 | + text_width = sum(words_width) + int(space_width) * (len(words) - 1) |
| 20 | + text_height = max([image_font.getsize(w)[1] for w in words]) |
| 21 | + |
| 22 | + txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0)) |
| 23 | + |
| 24 | + txt_draw = ImageDraw.Draw(txt_img) |
| 25 | + |
| 26 | + colors = [ImageColor.getrgb(c) for c in text_color.split(',')] |
| 27 | + c1, c2 = colors[0], colors[-1] |
| 28 | + |
| 29 | + fill = ( |
| 30 | + random.randint(min(c1[0], c2[0]), max(c1[0], c2[0])), |
| 31 | + random.randint(min(c1[1], c2[1]), max(c1[1], c2[1])), |
| 32 | + random.randint(min(c1[2], c2[2]), max(c1[2], c2[2])) |
| 33 | + ) |
| 34 | + |
| 35 | + for i, w in enumerate(words): |
| 36 | + txt_draw.text((sum(words_width[0:i]) + i * int(space_width), 0), w, fill=fill, font=image_font) |
| 37 | + |
| 38 | + if fit: |
| 39 | + return txt_img.crop(txt_img.getbbox()) |
| 40 | + else: |
| 41 | + return txt_img |
| 42 | + |
| 43 | +def _generate_vertical_text(text, font, text_color, font_size, space_width, fit): |
| 44 | + image_font = ImageFont.truetype(font=font, size=font_size) |
14 | 45 |
|
15 | | - @classmethod |
16 | | - def __generate_horizontal_text(cls, text, font, text_color, font_size, space_width, fit): |
17 | | - image_font = ImageFont.truetype(font=font, size=font_size) |
18 | | - words = text.split(' ') |
19 | | - space_width = image_font.getsize(' ')[0] * space_width |
| 46 | + space_height = int(image_font.getsize(' ')[1] * space_width) |
20 | 47 |
|
21 | | - words_width = [image_font.getsize(w)[0] for w in words] |
22 | | - text_width = sum(words_width) + int(space_width) * (len(words) - 1) |
23 | | - text_height = max([image_font.getsize(w)[1] for w in words]) |
| 48 | + char_heights = [image_font.getsize(c)[1] if c != ' ' else space_height for c in text] |
| 49 | + text_width = max([image_font.getsize(c)[0] for c in text]) |
| 50 | + text_height = sum(char_heights) |
24 | 51 |
|
25 | | - txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0)) |
| 52 | + txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0)) |
26 | 53 |
|
27 | | - txt_draw = ImageDraw.Draw(txt_img) |
| 54 | + txt_draw = ImageDraw.Draw(txt_img) |
28 | 55 |
|
29 | | - colors = [ImageColor.getrgb(c) for c in text_color.split(',')] |
30 | | - c1, c2 = colors[0], colors[-1] |
| 56 | + colors = [ImageColor.getrgb(c) for c in text_color.split(',')] |
| 57 | + c1, c2 = colors[0], colors[-1] |
31 | 58 |
|
32 | | - fill = ( |
33 | | - random.randint(min(c1[0], c2[0]), max(c1[0], c2[0])), |
34 | | - random.randint(min(c1[1], c2[1]), max(c1[1], c2[1])), |
35 | | - random.randint(min(c1[2], c2[2]), max(c1[2], c2[2])) |
36 | | - ) |
| 59 | + fill = ( |
| 60 | + random.randint(c1[0], c2[0]), |
| 61 | + random.randint(c1[1], c2[1]), |
| 62 | + random.randint(c1[2], c2[2]) |
| 63 | + ) |
37 | 64 |
|
38 | | - for i, w in enumerate(words): |
39 | | - txt_draw.text((sum(words_width[0:i]) + i * int(space_width), 0), w, fill=fill, font=image_font) |
| 65 | + for i, c in enumerate(text): |
| 66 | + txt_draw.text((0, sum(char_heights[0:i])), c, fill=fill, font=image_font) |
40 | 67 |
|
41 | | - if fit: |
42 | | - return txt_img.crop(txt_img.getbbox()) |
43 | | - else: |
44 | | - return txt_img |
45 | | - |
46 | | - @classmethod |
47 | | - def __generate_vertical_text(cls, text, font, text_color, font_size, space_width, fit): |
48 | | - image_font = ImageFont.truetype(font=font, size=font_size) |
49 | | - |
50 | | - space_height = int(image_font.getsize(' ')[1] * space_width) |
51 | | - |
52 | | - char_heights = [image_font.getsize(c)[1] if c != ' ' else space_height for c in text] |
53 | | - text_width = max([image_font.getsize(c)[0] for c in text]) |
54 | | - text_height = sum(char_heights) |
55 | | - |
56 | | - txt_img = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0)) |
57 | | - |
58 | | - txt_draw = ImageDraw.Draw(txt_img) |
59 | | - |
60 | | - colors = [ImageColor.getrgb(c) for c in text_color.split(',')] |
61 | | - c1, c2 = colors[0], colors[-1] |
62 | | - |
63 | | - fill = ( |
64 | | - random.randint(c1[0], c2[0]), |
65 | | - random.randint(c1[1], c2[1]), |
66 | | - random.randint(c1[2], c2[2]) |
67 | | - ) |
68 | | - |
69 | | - for i, c in enumerate(text): |
70 | | - txt_draw.text((0, sum(char_heights[0:i])), c, fill=fill, font=image_font) |
71 | | - |
72 | | - if fit: |
73 | | - return txt_img.crop(txt_img.getbbox()) |
74 | | - else: |
75 | | - return txt_img |
| 68 | + if fit: |
| 69 | + return txt_img.crop(txt_img.getbbox()) |
| 70 | + else: |
| 71 | + return txt_img |
0 commit comments