-
Notifications
You must be signed in to change notification settings - Fork 0
/
mondrian_generator_4.py
95 lines (69 loc) · 2.76 KB
/
mondrian_generator_4.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
import matplotlib.pyplot as plt
import time
import random
import os
from PIL import Image
def create_pattern_and_save(nshapes, shape, filename, dpi, grayscale):
save_directory = "mondrians"
if not os.path.exists(save_directory):
os.makedirs(save_directory)
if grayscale:
colors = [(intensity/255,)*3 for intensity in [50, 100, 130, 180, 220]]
else:
colors = ['r', 'lawngreen', 'blue', 'yellow']
fig, ax = plt.subplots(figsize=(5, 5))
ax.axis('off')
for c in range(nshapes):
xpos = random.randrange(0, 100)/100
ypos = random.randrange(0, 100)/100
s_size = random.randrange(5, 20)/100
#print(xpos, ypos)
if shape == 'c':
curr_shape = plt.Circle((xpos, ypos), s_size, color= random.choice(colors))
else:
curr_shape = plt.Rectangle((xpos, ypos), s_size, s_size, color= random.choice(colors))
ax.add_patch(curr_shape)
savepath = os.path.join(save_directory, f'{filename}.png')
fig.savefig(savepath, bbox_inches='tight', pad_inches = 0, dpi = dpi)
#convert to grayscale - add if
#img = Image.open(savepath).convert('L')
#img.save(savepath)
def ask_user_input():
while True:
shape = input("Do you want circles (default) or squares? [type c or s]: ")
if shape == '':
shape = 'c'
break
elif shape not in ['c', 's']:
print("Enter the lowercase 'c' for circels or 's' for squares")
continue
else:
break
while True:
grayscale = input("Want colors (default; otherwise grayscale)? [y/n] ")
if grayscale == '':
grayscale = False
break
elif grayscale not in ['y', 'n']:
print("Enter the lowercase 'c' for colors or 'g' for grayscale")
continue
else:
grayscale = True
break
tgt_patterns = input("How many patterns (default 3): ")
shapes_per_pattern = input("Number of circles per pattern (default 1500): ")
dpi = input("Figure DPI (default 96; determines size): ")
if tgt_patterns == "": tgt_patterns = 3
else: tgt_patterns = int(tgt_patterns)
if shapes_per_pattern == "": shapes_per_pattern = 1500
else: shapes_per_pattern = int(shapes_per_pattern)
if dpi == "": dpi = 96.0
else: dpi = float(dpi)
return tgt_patterns, shape, shapes_per_pattern, dpi, grayscale
if __name__ == "__main__":
#get user input
tgt_patterns, shape, shapes_per_pattern, dpi, grayscale = ask_user_input()
for i in range(tgt_patterns):
filename= "mpattern_"+str(i+1)
create_pattern_and_save(shapes_per_pattern, shape, filename, dpi, grayscale)
print("created", i+1, "out of", tgt_patterns)