Repository navigation
Expand file tree
/
Copy pathtext.py
More file actions
156 lines (127 loc) · 4.87 KB
/
Copy pathtext.py
File metadata and controls
156 lines (127 loc) · 4.87 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
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import random
# using the data from sql we create a lot of text object, they all are in the text_object_list
# with a for loop we calculate their length (calculate_length method)
# the order list class method creates a list with the texts, ordered by the lengths of them
# the x coordinates are fixed, the find_place instance method sets up all the y coordinates (in a for loop)
# we use the place method, again in a for loop - all the text is written
class Text():
text_object_list = []
ordered_list = []
not_empty_pixels = []
check_ratio = [1, 1]
def __init__(self, content, size, color, font, opacity):
self.content = content
self.size = size
self.color = color
if self.color == ():
self.color = (0, 0, 0)
self.font = font
self.font_type = None
self.opacity = opacity
self.length = 0
self.y = 0
self.x = 0
Text.text_object_list.append(self)
def place(self, img, rotate_rate=0):
self.font_type = ImageFont.truetype(self.font, self.size)
img = img.rotate(rotate_rate, expand=1)
draw = ImageDraw.Draw(img)
draw.text((self.x, self.y), self.content, self.color, self.font_type)
img = img.rotate(rotate_rate*(-1), expand=1)
return img
def findPlace_halfPyramid(self):
number_x = 0
for text in Text.ordered_list:
if self.content == text.content:
break
else:
number_x += 1
y_coordinate = 0
if number_x == 0:
y_coordinate = 0
else:
for text in Text.ordered_list[:number_x]:
y_coordinate += text.size
self.y = y_coordinate
def calculate_length(self, img):
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(self.font, self.size)
content = self.content
text_size = draw.textsize(content, font)
self.length = text_size[0]
@classmethod
def order_list(cls):
list_of_lengths = []
for text in cls.text_object_list:
list_of_lengths.append(text.length)
list_of_lengths = reversed(sorted(list_of_lengths))
for length in list_of_lengths:
for text in cls.text_object_list:
if text.length == length:
cls.ordered_list.append(text)
cls.text_object_list.remove(text)
def occupyPlace(self):
for x in range(self.x, (self.x + self.length+1)):
for y in range(self.y, (self.y+self.size+1), Text.check_ratio[1]):
Text.not_empty_pixels.append([x, y])
for y in range(self.y, (self.y + self.size + 1)):
for x in range(self.y, (self.x + self.length + 1), Text.check_ratio[0]):
Text.not_empty_pixels.append([x, y])
for x in range(self.x, self.x + self.length+1):
Text.not_empty_pixels.append([x, self.y+self.size])
for y in range(self.y, self.y + self.size+1):
Text.not_empty_pixels.append([self.x + self.length, y])
def checkOverLap(self):
for x in range(self.x, (self.x + self.length)):
coordinates = [x, self.y]
if coordinates in Text.not_empty_pixels:
return True
else:
continue
for x in range(self.x, (self.x + self.length)):
coordinates = [x, self.y+self.size]
if coordinates in Text.not_empty_pixels:
return True
else:
continue
for y in range(self.y, (self.y + self.size)):
coordinates = [self.x, y]
if coordinates in Text.not_empty_pixels:
return True
else:
continue
for y in range(self.y, (self.y + self.size)):
coordinates = [self.x + self.length, y]
if coordinates in Text.not_empty_pixels:
return True
else:
continue
return False
def randomPlace(self, img):
placed = False
while not placed:
self.x = random.randint(100, 1100-self.length)
self.y = random.randint(100, 1100-self.size)
self.calculate_length(img)
is_overlap = self.checkOverLap()
if is_overlap is False:
placed = True
self.occupyPlace()
img = self.place(img)
return img
else:
print("...")
continue
@classmethod
def checkOccupyRatio(cls):
lengths = []
for text in cls.text_object_list:
lengths.append(text.length)
cls.check_ratio[0] = (min(lengths))-1
heights = []
for text in cls.text_object_list:
heights.append(text.size)
cls.check_ratio[1] = (min(heights))-1