-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
308 lines (237 loc) · 12.8 KB
/
main.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from PIL import Image, ImageDraw, ImageFont
import json
class Picture:
def __init__(self):
# Creating an image with 600px * 600px and a white background
self.img = Image.new('RGBA', (600, 600), (255, 255, 255, 255))
# Get the Font
self.fnt = ImageFont.truetype('font\FreeSerif.ttf', 44)
self.fnt_i = ImageFont.truetype('font\FreeSerifItalic.ttf', 44)
def DrawPoint(self, function_type: int, function_name: str, num: int, point_domain: list, point_range: list, link_order: list):
# Get the img size
x, y = self.img.size
""" Set the position value """
# Start point and the end point for the left line
left_start_point = (50, 150)
left_end_point = (200, 450)
# Start point and the end point for the right line
right_start_point = (x-left_end_point[0], left_start_point[1])
right_end_point = (x-left_start_point[0], left_end_point[1])
# First Point Left Point position
left_first_start_x, left_first_start_y = (left_start_point[0]+70, left_start_point[1]+45)
left_first_end_x, left_first_end_y = (left_start_point[0]+80, left_start_point[1]+55)
# First Point Right Point position
right_first_start_x, right_first_start_y = (right_start_point[0]+70, right_start_point[1]+45)
right_first_end_x, right_first_end_y = (right_start_point[0]+80, right_start_point[1]+55)
# Start point and the end point for the left line
left_x1 , left_y1 = (100,100)
left_x2 , left_y2 = (100,550)
# Start point and the end point for the right line
right_x1 , right_y1 = ( x-left_x1, left_y1 )
right_x2 , right_y2 = ( x-left_x2, left_y2 )
# First Point Left Label position
left_text_x = (left_x1+11)
left_text_y = (left_y1-10)
# First Point Right Label position
right_text_x = (right_x1-35)
right_text_y = (right_y1-10)
# Function name Position
function_x = (x/2)
function_y = (left_y1-20)
# First Domain Value Position
domain_x = left_x1 - 2
domain_y = (left_y1+left_y2)/(num*2) if num > 2 else (left_y1+left_y2)/(num*2+0.25)
# First Range Value Position
range_x = right_x1 - 10
range_y = (right_y1+right_y2)/(num*2) if num > 2 else (right_y1+right_y2)/(num*2+0.25)
# Creating Draw Tools for the picture
self.draw = ImageDraw.Draw(self.img)
self.draw.ellipse((left_start_point, left_end_point), fill=(255, 255, 255), outline=(0, 0, 0), width=3)
self.draw.ellipse((right_start_point, right_end_point), fill=(255, 255, 255), outline=(0, 0, 0), width=3)
# Place the text
if function_type == 0 :
self.draw.text((left_text_x, left_text_y), "A", font=self.fnt, fill=(0, 0, 0, 255))
self.draw.text((right_text_x, right_text_y), "B", font=self.fnt, fill=(0, 0, 0, 255))
self.draw.text((function_x, function_y), "%s" % function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
if function_type == 1 :
self.draw.text((left_text_x, left_text_y), "x", font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.text((right_text_x, right_text_y), "%s(x)" % function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.text((function_x, function_y), "%s" % function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
if function_type == 2 :
self.draw.text((function_x, function_y), function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
#Draw the point
for x in range(num):
CONST = 245 - 45 * num
self.draw.ellipse((left_first_start_x, left_first_start_y+(CONST*x), left_first_end_x, left_first_end_y+(CONST*x)), fill=(0, 0, 0))
self.draw.ellipse((right_first_start_x, right_first_start_y+(CONST*x), right_first_end_x, right_first_end_y+(CONST*x)), fill=(0, 0, 0))
# Set the FontSize
self.fnt = ImageFont.truetype('font\FreeSerif.ttf', 28)
# 2. 35 3.80 4. 120
RANGE_CONST = 35 if num == 2 else 40*(num-1)
# Creating Domain
for i, value in enumerate(point_domain, 1):
# Get the digit
j = len(str(value))
if self.CheckInt(value):
self.draw.text((domain_x-15*(j-1), domain_y*(i)+RANGE_CONST), value, font=self.fnt, fill=(0, 0, 0, 255))
else :
self.draw.text((domain_x-15*(j-1), domain_y*(i)+RANGE_CONST), value, font=self.fnt_i, fill=(0, 0, 0, 255))
# Creating Range
for i, value in enumerate(point_range, 1):
# Get the digit
j = len(str(value))
if self.CheckInt(value):
if i <= 2:
self.draw.text((range_x+20*(j-1), range_y*(i)+RANGE_CONST), value, font=self.fnt, fill=(0, 0, 0, 255))
elif i > 2:
self.draw.text((range_x+20*(j-1), range_y*(i)+RANGE_CONST), value, font=self.fnt, fill=(0, 0, 0, 255))
else :
if i <= 2:
self.draw.text((range_x+20*(j-1), range_y*(i)+RANGE_CONST), value, font=self.fnt_i, fill=(0, 0, 0, 255))
elif i > 2:
self.draw.text((range_x+20*(j-1), range_y*(i)+RANGE_CONST), value, font=self.fnt_i, fill=(0, 0, 0, 255))
i = 1
#Draw the line
for x in link_order:
# link_order in [[1,2],[3,4]] form
# x in [1,2] form
for y in x :
self.DrawPointLine(num, i, y)
i += 1
"""
function_type: int,
0 => A|B
1 => x-f(x)
2=> None
"""
def DrawFunction(self, function_type: int, function_name: str, number_point: int, point_domain: list, point_range: list, link_order: list):
"""Initialize the variable"""
# Constant position to arrange the range point
CONST = 0.25
# Get the img size
x , y = self.img.size
# Get the Font
self.fnt = ImageFont.truetype('font\FreeSerif.ttf', 44)
self.fnt_i = ImageFont.truetype('font\FreeSerifItalic.ttf', 44)
""" Set the position value """
# Start point and the end point for the left line
left_x1 , left_y1 = (100,100)
left_x2 , left_y2 = (100,550)
# Start point and the end point for the right line
right_x1 , right_y1 = ( x-left_x1, left_y1 )
right_x2 , right_y2 = ( x-left_x2, left_y2 )
# First Point Left Label position
left_text_x = (left_x1-15)
left_text_y = (left_y1-50)
# First Point Right Label position
right_text_x = (right_x1-15)
right_text_y = (right_y1-50)
# Function name Position
function_x = (x/2)
function_y = (left_y1-60)
# First Domain Value Position
domain_x = left_x1 - 40
domain_y = (left_y1+left_y2)/(number_point+2)
self.domain_y = domain_y
# First Range Value Position
range_x = right_x1+20
range_y = (right_y1+right_y2)/(number_point+2)
# Label Line Position Value
label_line_x1, label_line_x2 = (domain_x+30, domain_x+50)
label_line_x3, label_line_x4 = (range_x-30, range_x-10)
""" Start drawing all the picture """
# Creating Draw Tools for the picture
self.draw = ImageDraw.Draw(self.img)
# Draw the line on the picture
self.draw.line((left_x1, left_y1, left_x2, left_y2), fill=(0, 0, 0), width=3)
self.draw.line((right_x1, right_y1, right_x2, right_y2), fill=(0, 0, 0), width=3)
# Place the text
if function_type == 0 :
self.draw.text((left_text_x, left_text_y), "A", font=self.fnt, fill=(0, 0, 0, 255))
self.draw.text((right_text_x, right_text_y), "B", font=self.fnt, fill=(0, 0, 0, 255))
self.draw.text((function_x, function_y), "%s" % function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
if function_type == 1 :
self.draw.text((left_text_x, left_text_y), "x", font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.text((right_text_x, right_text_y), "%s(x)" % function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.text((function_x, function_y), "%s" % function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
if function_type == 2 :
self.draw.text((function_x, function_y), function_name, font=self.fnt_i, fill=(0, 0, 0, 255))
# Creating Domain and Label Line
for i, value in enumerate(point_domain, 1):
# Get the digit
j = len(str(value))
if self.CheckInt(value):
self.draw.text((domain_x-15*(j-1), domain_y*(i)), value, font=self.fnt, fill=(0, 0, 0, 255))
self.draw.line((label_line_x1, domain_y*(i+CONST), label_line_x2, domain_y*(i+CONST)), fill=(0, 0, 0), width=2)
else :
self.draw.text((domain_x-15*(j-1), domain_y*(i)), value, font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.line((label_line_x1, domain_y*(i+CONST), label_line_x2, domain_y*(i+CONST)), fill=(0, 0, 0), width=2)
# Creating Range and Label Line
for i, value in enumerate(point_range, 1):
# Get the digit
j = len(str(value))
if self.CheckInt(value):
if i <= 2:
self.draw.text((range_x+20*(j-1), range_y*(i+CONST/i)), value, font=self.fnt, fill=(0, 0, 0, 255))
self.draw.line((label_line_x3, range_y*(i+0.25+CONST/i), label_line_x4, range_y*(i+0.25+CONST/i)), fill=(0, 0, 0), width=2)
elif i > 2:
self.draw.text((range_x+20*(j-1), range_y*(i-CONST/(5-i))), value, font=self.fnt, fill=(0, 0, 0, 255))
self.draw.line((label_line_x3, range_y*(i+0.25-CONST/(5-i)), label_line_x4, range_y*(i+0.25-CONST/(5-i))), fill=(0, 0, 0), width=2)
else :
if i <= 2:
self.draw.text((range_x+20*(j-1), range_y*(i+CONST/i)), value, font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.line((label_line_x3, range_y*(i+0.25+CONST/i), label_line_x4, range_y*(i+0.25+CONST/i)), fill=(0, 0, 0), width=2)
elif i > 2:
self.draw.text((range_x+20*(j-1), range_y*(i-CONST/(5-i))), value, font=self.fnt_i, fill=(0, 0, 0, 255))
self.draw.line((label_line_x3, range_y*(i+0.25-CONST/(5-i)), label_line_x4, range_y*(i+0.25-CONST/(5-i))), fill=(0, 0, 0), width=2)
# Draw the line
for first, last in enumerate(link_order, 1):
self.DrawLine(number_point, first, last)
# To Check the value
def CheckInt(self, s):
try:
int(s)
return True
except ValueError:
return False
# Draw the point line
def DrawPointLine(self, number_point: int, start: int, end: int):
# Read the setting file
with open("./data.json") as f:
data = json.load(f)
# Set the value
size = data["image-size"]
data = data["point"]["%s" % number_point]
data = data["%s" % start]["%s" % end]
#Create the Mapping Line
img_arrow = Image.open(
'image\line with middle arrow.png').convert('RGBA')
img_arrow = img_arrow.rotate(data["angle"], expand=True)
img_arrow = img_arrow.resize(
(round(img_arrow.size[0]*data["image-size"]), round(img_arrow.size[1]*size)))
self.img.paste(
img_arrow, (data["position-x"], data["position-y"]), img_arrow)
# Draw the line
def DrawLine(self, number_point: int, start: int, end: int):
# Read the setting file
with open("./data.json") as f:
data = json.load(f)
# Set the value
size = data["image-size"]
data = data["%s" % number_point]
data = data["%s" % start]["%s" % end]
#Create the Mapping Line
img_arrow = Image.open(
'image\line with middle arrow.png').convert('RGBA')
img_arrow = img_arrow.rotate(data["angle"], expand=True)
img_arrow = img_arrow.resize(
(round(img_arrow.size[0]*data["image-size"]), round(img_arrow.size[1]*size)))
self.img.paste(
img_arrow, (data["position-x"], int(self.domain_y*(data["position-y"]))), img_arrow)
if __name__ == '__main__':
# Case 4 point
img = Picture()
#img.DrawFunction(0, "f", 4, ["1","2","3","4"], ["1","2","3","4"], [1,2,3,4])
img.DrawPoint(0, "f", 3, ["1","2","3"], ["1","2","3"], [[1,2],[2,1],[3,2]])
img = img.img
img.show()