-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbstractArt.py
130 lines (122 loc) · 5.26 KB
/
AbstractArt.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
from PIL import Image, ImageDraw
import random
import math
import concurrent.futures
class AbstractArtGenerator:
def __init__(self, size=(1920, 1080), num_shapes=500):
self.size = size
self.num_shapes = num_shapes
self.image = None
self.draw = None
def generate(self):
self.image = Image.new("RGB", self.size)
self.draw = ImageDraw.Draw(self.image)
gradient = [
(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for _ in range(2)
]
for y in range(self.size[1]):
color = tuple(
int(
gradient_channel[0]
+ (gradient_channel[1] - gradient_channel[0]) * y / self.size[1]
)
for gradient_channel in zip(gradient[0], gradient[1])
)
self.draw.line((0, y, self.size[0], y), fill=color)
def draw_shape(shape_type, shape_size, shape_position, fill_color):
if shape_type == "circle":
self.draw.ellipse(
(
shape_position,
(shape_position[0] + shape_size, shape_position[1] + shape_size),
),
fill=fill_color,
)
elif shape_type == "triangle":
vertices = [
(shape_position[0] + shape_size / 2, shape_position[1]),
(shape_position[0], shape_position[1] + shape_size),
(shape_position[0] + shape_size, shape_position[1] + shape_size),
]
self.draw.polygon(vertices, fill=fill_color)
elif shape_type == "square":
self.draw.rectangle(
(
shape_position,
(shape_position[0] + shape_size, shape_position[1] + shape_size),
),
fill=fill_color,
)
elif shape_type == "leaf":
leaf_points = []
for angle in range(0, 360, 10):
x = shape_size * (math.sin(angle) ** 3)
y = shape_size * (math.cos(angle) - math.cos(2 * angle) / 2)
leaf_points.append(
(
shape_position[0] + shape_size / 2 + x,
shape_position[1] + shape_size / 2 + y,
)
)
self.draw.polygon(leaf_points, fill=fill_color)
elif shape_type == "star":
star_points = []
for angle in range(0, 360, 72):
x = shape_size * math.sin(math.radians(angle))
y = shape_size * math.cos(math.radians(angle))
star_points.append(
(
shape_position[0] + shape_size / 2 + x,
shape_position[1] + shape_size / 2 + y,
)
)
x = shape_size / 2 * math.sin(math.radians(angle + 36))
y = shape_size / 2 * math.cos(math.radians(angle + 36))
star_points.append(
(
shape_position[0] + shape_size / 2 + x,
shape_position[1] + shape_size / 2 + y,
)
)
self.draw.polygon(star_points, fill=fill_color)
elif shape_type == "droplet":
droplet_points = []
for angle in range(-60, 120, 10):
x = shape_size * (math.sin(math.radians(angle)) ** 3)
y = shape_size * (
math.cos(math.radians(angle)) - math.cos(math.radians(angle * 2)) / 2
)
droplet_points.append(
(
shape_position[0] + shape_size / 2 + x,
shape_position[1] + shape_size / 2 + y,
)
)
self.draw.polygon(droplet_points, fill=fill_color)
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for i in range(self.num_shapes):
shape_type = random.choice(
["circle", "triangle", "square", "leaf", "star", "droplet"]
)
shape_size = random.randint(50, 200)
shape_position = (
random.randint(0, self.size[0] - shape_size),
random.randint(0, self.size[1] - shape_size),
)
shape_color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
alpha = random.randint(64, 192)
fill_color = shape_color + (alpha,)
futures.append(
executor.submit(draw_shape, shape_type, shape_size, shape_position, fill_color)
)
for future in concurrent.futures.as_completed(futures):
future.result()
def save(self, filename):
if self.image is not None:
self.image.save(filename)