-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayImage.py
More file actions
41 lines (32 loc) · 1.42 KB
/
ArrayImage.py
File metadata and controls
41 lines (32 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
class TextImage():
def __init__(self, font='/Library/Fonts/Times New Roman.ttf',
occupancy=1.0, iscenter=True):
self.fontname = font
self.occupancy = occupancy
self.center = iscenter
def __call__(self, image, texts, x1, y1, x2, y2):
if not os.path.exists(self.fontname):
print('Fontname {} is not exist'.format(self.fontname))
length = len(texts)
textheight = (y2 - y1) / length
img = Image.new('RGBA', (x2 - x1, y2 - y1), (255, 255, 255, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(
self.fontname, int(textheight * self.occupancy), encoding='unic')
# make image with texts
for i, text in enumerate(texts):
sfont = font
textsize = font.getsize(text)
if textsize[0] > (x2 - x1):
fontscale = (x2 - x1) / textsize[0]
sfont = ImageFont.truetype(self.fontname, int(
textheight * self.occupancy * fontscale) - 1, encoding='unic')
draw.text((0, textheight * i), text, font=sfont, fill='#000')
centerize = 0.5 if self.center else 1
pastedimage = image.copy()
pastedimage.paste(
img, (x1, y1 + int(textheight * (1 - self.occupancy) * centerize)), img)
return pastedimage