Repository navigation
Expand file tree
/
Copy pathvicarioustext.py
More file actions
169 lines (151 loc) Β· 6.12 KB
/
Copy pathvicarioustext.py
File metadata and controls
169 lines (151 loc) Β· 6.12 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# import packages
from datetime import datetime
from PIL import ImageColor, ImageFont
import time
colorFFFFFF=ImageColor.getrgb("#ffffff")
def getdateandtime():
now = datetime.utcnow()
return now.strftime("%Y-%m-%d %H:%M:%S")
def getfont(size, isbold=False):
if isbold:
return ImageFont.truetype(font="/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",size=size,encoding="unic")
else:
return ImageFont.truetype(font="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",size=size,encoding="unic")
def getmaxfontsize(draw, s, maxwidth=480, maxheight=320, isbold=False, maxfontsize=128, minfontsize=8):
sw = maxwidth
sh = maxheight
fontsize = maxfontsize + 1
while (sw >= maxwidth or sh >= maxheight) and fontsize >= minfontsize:
fontsize = fontsize - 1
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
return fontsize,sw,sh
def getmaxtextforwidth(draw, words, width, fontsize, isbold=False):
wlen = len(words)
if wlen == 0:
return "", []
for x in range(wlen, 1, -1):
s = " ".join(words[0:x])
sw,sh,f = gettextdimensions(draw, s, fontsize, isbold)
if sw <= width:
return s, words[x:]
if wlen > 1:
# next long word
return " ".join(words[0:1]), words[1:]
else:
# final word
return " ".join(words[0:1]), []
def gettextdimensions(draw, s, fontsize, isbold=False):
thefont = getfont(fontsize, isbold)
# Deprecated, removed in Pillow 10.0.0
#sw,sh = draw.textsize(s, thefont)
# New in 8.0.0, required in Pillow 10.0.0
#left, top, right, bottom = thefont.getbbox(text=s)
left, top, right, bottom = draw.multiline_textbbox(xy=(0,0),text=f"{s}",font=thefont)
return right, bottom+top, thefont
def gettextoffset(thefont,s):
# Deprecated, removed in Pillow 10.0.0
#ox,oy = thefont.getoffset(s)
# New in 8.0.0, required in Pillow 10.0.0
left,top,_,_ = thefont.getbbox(text=s)
return left, top
def getwrappedtextatwidth(draw, text, width, fontsize, isbold=False):
outputtext = ""
rwords = text.split()
rpart, rwords = getmaxtextforwidth(draw, rwords, width, fontsize, isbold)
while len(rpart) > 0:
outputtext = outputtext + rpart
if len(rwords) > 0: outputtext = outputtext + "\n"
rpart, rwords = getmaxtextforwidth(draw, rwords, width, fontsize, isbold)
return outputtext
def drawcenteredtext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x-(sw/2),y-(sh/2)), text=s, font=thefont, fill=textcolor)
def drawbottomtext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x-(sw/2),y-sh), text=s, font=thefont, fill=textcolor)
def drawbottomlefttext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x,y-sh), text=s, font=thefont, fill=textcolor)
def drawbottomrighttext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x-sw,y-sh), text=s, font=thefont, fill=textcolor)
def drawLabel(draw, s="", fontsize=12, anchorposition="tl", anchorx=0, anchory=0, backgroundColor="#000000", textColor="#ffffff", borderColor="#888888"):
sw,sh,f = gettextdimensions(draw, s, fontsize, False)
padding = 1
border = 1
nx = anchorx
ny = anchory
if anchorposition == "tl":
# no change to nx, ny
pass
elif anchorposition == "tr":
nx = anchorx - sw
pass
elif anchorposition == "t":
nx = anchorx - (sw/2)
pass
elif anchorposition == "bl":
ny = anchory - sh
pass
elif anchorposition == "br":
nx = anchorx - sw
ny = anchory - sh
pass
elif anchorposition == "b":
nx = anchorx - (sw/2)
ny = anchory - sh
pass
elif anchorposition == "l":
ny = anchory - (sh/2)
pass
elif anchorposition == "r":
nx = anchorx - sw
ny = anchory - (sh/2)
pass
elif anchorposition == "c":
nx = anchorx - (sw/2)
ny = anchory - (sh/2)
draw.rectangle(xy=[(nx-padding-border,ny-padding-border),(nx+sw+padding+border,ny+sh+padding+border)],fill=ImageColor.getrgb(backgroundColor),outline=ImageColor.getrgb(borderColor),width=1)
draw.text(xy=(nx,ny), text=s, font=f, fill=ImageColor.getrgb(textColor))
def drawtoptext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x-(sw/2),y), text=s, font=thefont, fill=textcolor)
def drawtoplefttext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x,y), text=s, font=thefont, fill=textcolor)
def drawtoprighttext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x-sw,y), text=s, font=thefont, fill=textcolor)
def drawrighttext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x-sw,y-(sh/2)), text=s, font=thefont, fill=textcolor)
def drawlefttext(draw, s, fontsize, x, y, textcolor=colorFFFFFF, isbold=False):
sw,sh,thefont = gettextdimensions(draw, s, fontsize, isbold)
ox,oy = gettextoffset(thefont,s)
sw += ox
sh += oy
draw.text(xy=(x,y-(sh/2)), text=s, font=thefont, fill=textcolor)