forked from Hallows/nsRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_image.py
More file actions
228 lines (173 loc) · 7.44 KB
/
generate_image.py
File metadata and controls
228 lines (173 loc) · 7.44 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from PIL import Image, ImageDraw, ImageFont
import pymysql
from math import ceil
import time
from datetime import datetime
import init
boxLength = 200
boxHeight = 50
startX = 100
startY = 116
font = ImageFont.truetype(init.FONT_PATH + 'msyh.ttc', 20, index=1)
def GetDate(date: datetime):
print(date)
week = {0: "周一", 1: "周二", 2: "周三",
3: "周四", 4: "周五", 5: "周六", 6: "周日"}
dateStr = week[date.weekday()] + ' '
dateStr += time.strftime("%m月%d日 %H:%M", date.timetuple())
return dateStr
def DrawRectangal(img: Image.Image, x: int, y: int, info: list):
drawer = ImageDraw.Draw(img)
db = pymysql.connect(host=init.dbHost, port=init.dbPort, user=init.dbUser,
password=init.dbPassword, db=init.dbName, charset=init.dbCharset)
cursor = db.cursor()
cursor.execute(
"SELECT * FROM ns_mental WHERE mentalID = " + info[3].__str__())
mental = cursor.fetchone()
drawer.rectangle(
(startX + x*boxLength,
startY + (boxHeight / 2) + y * boxHeight,
startX + (x+1)*boxLength,
startY + (boxHeight / 2) + (y+1) * boxHeight),
fill=GetRGB(mental[4]),
outline=(0, 0, 0),
width=1
)
drawer.text((startX + (0.3+x)*boxLength,
startY + (boxHeight / 2) + (y + 0.25) * boxHeight), info[2], font=font, fill=0x000000)
r = boxLength / 10
drawer.ellipse((startX + x*boxLength + (boxLength * 0.05),
startY + (boxHeight / 2) + y *
boxHeight + (boxHeight / 2 - r),
startX + x*boxLength + (boxLength * 0.05) + 2*r,
startY + (boxHeight / 2) + y*boxHeight + (boxHeight / 2 + r)), fill=0x000000, outline=None)
logo = Image.open(init.MENTAL_ICON_PATH + mental[2])
logo = logo.resize((int(r * 1.8), int(r * 1.8)), Image.ANTIALIAS)
R, G, B, A = logo.split()
img.paste(logo, (int(startX + x*boxLength + (boxLength * (0.25) - 1.9 * r)),
int(startY + (boxHeight / 2) + y*boxHeight + (boxHeight / 2 - 0.9*r))), mask=A)
while True:
if info[4] == 1:
if mental[6] == 0:
break
cursor.execute(
"SELECT * FROM ns_mental WHERE mentalID = " + mental[6].__str__())
mentalConnection = cursor.fetchone()
drawer.ellipse((startX + x*boxLength + (boxLength * 0.25) - r,
startY + (boxHeight / 2) + y*boxHeight +
(boxHeight / 2),
startX + x*boxLength + (boxLength * 0.05) + 2*r,
startY + (boxHeight / 2) + y*boxHeight + (boxHeight / 2 + r)), fill=0x000000, outline=0xffffff, width=1)
logo = Image.open(init.MENTAL_ICON_PATH + mentalConnection[2])
logo = logo.resize((int(r * 0.9), int(r * 0.9)), Image.ANTIALIAS)
R, G, B, A = logo.split()
img.paste(logo, (int(startX + x*boxLength + (boxLength * (0.25) - 0.9 * r)),
int(startY + (boxHeight / 2) + y*boxHeight + (boxHeight / 2 + 0.1*r))), mask=A)
break
def GetRGB(id: str) -> tuple:
r = int(id[0:2], 16)
g = int(id[2:4], 16)
b = int(id[4:6], 16)
return (r, g, b)
def GetMember(id: int):
# 读取数据库得到数据,返回列表
db = pymysql.connect(host=init.dbHost, port=init.dbPort, user=init.dbUser,
password=init.dbPassword, db=init.dbName, charset=init.dbCharset)
cursor = db.cursor()
cursor.execute("SELECT * FROM ns_member WHERE teamID = " + id.__str__())
memberlist = cursor.fetchall()
db.close()
return (id, memberlist)
def GenerateImage(teamdata: tuple):
# 根据列表生成图片,返回文件名
canvasLength = 1200
canvasHeight = 480
memberCount = len(teamdata[1])
internal = []
external = []
healer = []
tank = []
db = pymysql.connect(host=init.dbHost, port=init.dbPort, user=init.dbUser,
password=init.dbPassword, db=init.dbName, charset=init.dbCharset)
cursor = db.cursor()
if not memberCount:
return "No Team"
for member in teamdata[1]:
cursor.execute(
"SELECT * FROM ns_mental WHERE mentalID = " + member[3].__str__())
mental = cursor.fetchone()
if mental[5] == 1:
tank.append(member)
elif mental[5] == 2:
healer.append(member)
elif mental[5] == 3:
internal.append(member)
elif mental[5] == 4:
external.append(member)
cursor.execute("SELECT * FROM ns_team WHERE teamID = " +
teamdata[0].__str__())
teaminfo = cursor.fetchone()
cursor.execute("SELECT * FROM ns_leader WHERE id = " +
teaminfo[1].__str__())
leaderinfo = cursor.fetchone()
if len(internal) > 10 or len(external) > 10 or len(tank) + len(healer) > 5:
print(ceil(len(internal) / 2))
num = max(ceil(len(internal) / 2),
ceil(len(external) / 2),
len(tank) + len(healer))
canvasHeight += (num - 5) * boxHeight
img = Image.new("RGB", (canvasLength, canvasHeight), (255, 255, 255))
drawer = ImageDraw.Draw(img)
tital = leaderinfo[2] + " " + teaminfo[2]
titalFont = ImageFont.truetype(init.FONT_PATH + 'msyh.ttc', 40, index=1)
w, h = titalFont.getsize(tital)
if w > canvasLength:
drawer.text((0, 10), tital, fill=0x000000, font=titalFont)
else:
drawer.text(((canvasLength - w)//2, 10), tital,
fill=0x000000, font=titalFont)
datainfo = "(团队ID:" + teamdata[0].__str__() + ") " + GetDate(teaminfo[3])
time_w, time_h = font.getsize(datainfo)
team_w, team_h = font.getsize(teaminfo[6])
if team_w + time_w < boxLength * 5:
drawer.text((startX, startY - time_h - 5),
datainfo, fill=0x000000, font=font)
drawer.text((startX + boxLength * 5 - team_w, startY -
team_h - 5), teaminfo[6], fill=0x000000, font=font)
else:
drawer.text((startX, startY - time_h - 5),
datainfo, fill=0x000000, font=font)
drawer.text((startX + time_w + 5, startY -
team_h - 5), teaminfo[6], fill=0x000000, font=font)
for i in range(5):
drawer.rectangle(
((startX + (boxLength * i),
startY,
startX + boxLength*(i + 1),
startY + boxHeight / 2)),
fill=(255, 255, 255),
outline=(0, 0, 0),
width=1)
drawer.text((startX + (boxLength * (i + 0.5)),
startY), int(i+1).__str__(), fill=(0, 0, 0), font=font)
for i in range(len(external)):
if i % 2 == 0:
DrawRectangal(img, 0, i//2, external[i])
elif i % 2 == 1:
DrawRectangal(img, 1, i//2, external[i])
for i in range(len(internal)):
if i % 2 == 0:
DrawRectangal(img, 2, i//2, internal[i])
elif i % 2 == 1:
DrawRectangal(img, 3, i//2, internal[i])
for i in range(len(tank)):
DrawRectangal(img, 4, i, tank[i])
temp = len(tank)
for i in range(len(healer)):
DrawRectangal(img, 4, i+temp, healer[i])
Time = time.strftime("%y-%m-%d-%H-%M-%S", time.localtime(time.time()))
name = init.IMAGE_PATH + Time + '-' + teamdata[0].__str__() + '.jpg'
img.save(name)
return name
def GetImg(id: int) -> str:
return GenerateImage(GetMember(id))