-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_shape_image_generator.py
190 lines (151 loc) · 6.15 KB
/
random_shape_image_generator.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
# Random Shape Image Generator
from tkinter import *
import tkinter.messagebox as mbox
import tkinter as tk
from datetime import datetime
from PIL import Image, ImageDraw, ImageTk
from random import randint, randrange
# created a main window
window = Tk()
window.title("Random Shape Image Generator")
window.geometry('1000x700')
# top label
start1 = tk.Label(text = "Random Shape Image\nGenerator", font=("Arial", 50, "underline"), fg="magenta") # same way bg
start1.place(x = 150, y = 10)
# top label
start1 = tk.Label(text = "Click on below button to generate\nrandom shape images", font=("Arial", 40), fg="green") # same way bg
start1.place(x = 100, y = 180)
# function defined to generate random images with some overlapping
def overlap_fun():
window.destroy()
WIDTH, HEIGHT = 1000, 700
COUNT = 40
# Use datetime (somehow), to generate random int.
def datetimeToInt():
y, m, d, hour, min, sec = datetime.now().timetuple()[0:6]
return y + m + d + hour + min + sec
def randRgb():
return (randint(0, 255), randint(0, 255), randint(0, 255))
def randTriangle():
x1, y1 = randrange(0, WIDTH), randrange(0, HEIGHT)
x2, y2 = randrange(0, WIDTH), randrange(0, HEIGHT)
x3, y3 = randrange(0, WIDTH), randrange(0, HEIGHT)
return [(x1, y1), (x2, y2), (x3, y3)]
def randRect():
x1, y1 = randrange(0, WIDTH), randrange(0, HEIGHT)
x2, y2 = randrange(0, WIDTH), randrange(0, HEIGHT)
return [(x1, y1), (x2, y2)]
randEllipse = randRect
# Map: random shape creation functions -> ImageDraw methods
shapeFactories = [
(randTriangle, ImageDraw.ImageDraw.polygon),
(randRect, ImageDraw.ImageDraw.rectangle),
(randEllipse, ImageDraw.ImageDraw.ellipse)
]
shapeFactoriesCount = len(shapeFactories)
composite = Image.new('RGBA', (WIDTH, HEIGHT), '#00000000')
draw = ImageDraw.Draw(composite)
for x in range(COUNT):
# Get random index, within full range:
# randIdx = randrange(0, shapeFactoriesCount)
# Use random int, generated from datetime (somehow):
randIdx = datetimeToInt() % shapeFactoriesCount
shapeFactory, drawMethod = shapeFactories[randIdx]
drawMethod( # passing 'self'/'draw' explicitly to method:
draw, shapeFactory(), fill=randRgb(), outline=randRgb()
)
root1 = tk.Tk()
root1.title("Random Shape Images with Overlapping")
compositeTk = ImageTk.PhotoImage(composite)
tk.Label(root1,image=compositeTk).pack()
root1.mainloop()
# created button
exitb = Button(window, text="GENERATE WITH OVERLAPPING",command=overlap_fun,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
exitb.place(x =275 , y =350 )
# function defined to generate random images without some overlapping
def normal_fun():
window.destroy()
WIDTH, HEIGHT = 50, 60
COUNT = 100
# Use datetime (somehow), to generate random int.
def datetimeToInt():
y, m, d, hour, min, sec = datetime.now().timetuple()[0:6]
return y + m + d + hour + min + sec
def randRgb():
return (randint(0, 255), randint(0, 255), randint(0, 255))
def randTriangle():
x1, y1 = randrange(0, WIDTH), randrange(0, HEIGHT)
x2, y2 = randrange(0, WIDTH), randrange(0, HEIGHT)
x3, y3 = randrange(0, WIDTH), randrange(0, HEIGHT)
return [(x1, y1), (x2, y2), (x3, y3)]
def randRect():
x1, y1 = randrange(0, WIDTH), randrange(0, HEIGHT)
x2, y2 = randrange(0, WIDTH), randrange(0, HEIGHT)
return [(x1, y1), (x2, y2)]
return
randEllipse = randRect
# Map: random shape creation functions -> ImageDraw methods
shapeFactories = [
(randTriangle, ImageDraw.ImageDraw.polygon),
(randRect, ImageDraw.ImageDraw.rectangle),
(randEllipse, ImageDraw.ImageDraw.ellipse)
]
shapeFactoriesCount = len(shapeFactories)
imgOpenList = []
imgClosedList = []
for x in range(COUNT):
# Get random index, within full range:
# randIdx = randrange(0, shapeFactoriesCount)
# Use random int, generated from datetime (somehow):
randIdx = datetimeToInt() % shapeFactoriesCount
shapeFactory, drawMethod = shapeFactories[randIdx]
im_open = Image.new('RGBA', (WIDTH, HEIGHT), '#00000000')
draw = ImageDraw.Draw(im_open)
drawMethod( # passing 'self'/'draw' explicitly to method:
draw, shapeFactory(), fill=randRgb(), outline=randRgb()
)
imgOpenList.append(im_open)
imgClosedList.append(im_open.rotate(90))
# The rest is just for displaying the resulting images.
import tkinter as tk
from math import floor, sqrt
root2 = tk.Tk()
root2.title("Random Shape Images without Overlapping")
imgOpenList = [
ImageTk.PhotoImage(img) for img in imgOpenList
]
imgClosedList = [
ImageTk.PhotoImage(img) for img in imgClosedList
]
imgsPerAxis = floor(sqrt(COUNT)) # rough approximation
canvas = tk.Canvas(
root2,
width=WIDTH * imgsPerAxis * 2, # x2: open & closed images
height=HEIGHT * imgsPerAxis
)
canvas.pack()
for i in range(imgsPerAxis):
for j in range(imgsPerAxis):
canvas.create_image(
2 * j * WIDTH, i * HEIGHT,
image=imgOpenList[i * imgsPerAxis + j],
anchor=tk.NW
)
canvas.create_image(
(2 * j + 1) * WIDTH, i * HEIGHT,
image=imgClosedList[i * imgsPerAxis + j],
anchor=tk.NW
)
root2.mainloop()
# created button
exitb = Button(window, text="GENERATE WITHOUT OVERLAPPING",command=normal_fun,font=("Arial", 20), bg = "yellow", fg = "blue", borderwidth=3, relief="raised")
exitb.place(x =250 , y =450 )
# function for exiting
def exit_win():
if mbox.askokcancel("Exit", "Do you want to exit?"):
window.destroy()
# created exit button
exitb = Button(window, text="EXIT",command=exit_win,font=("Arial", 20), bg = "red", fg = "blue", borderwidth=3, relief="raised")
exitb.place(x =460 , y =580 )
window.protocol("WM_DELETE_WINDOW", exit_win)
window.mainloop()