-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_grid.py
More file actions
192 lines (149 loc) · 7.28 KB
/
create_grid.py
File metadata and controls
192 lines (149 loc) · 7.28 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
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
import os
import sys
import random
from PIL import Image, ImageDraw
# Get the input directory from the command line arguments
if len(sys.argv) < 3:
print("Please provide the input directory and output name as command line arguments.")
sys.exit(1)
input_dir = sys.argv[1]
output_name = sys.argv[2]
# Get a list of files in the input directory
files = os.listdir(input_dir)
# Shuffle the files list for different order
random.shuffle(files)
# Calculate the dimensions for the grid and spacing
grid_size = (6, 6)
spacing = 50
image_size = (250, 250)
extension_percentage = 0.05
border_thickness = 10
# Randomly select 25 images (if available) for the first collage
selected_files = random.sample(files, min(grid_size[0] * grid_size[1], len(files)))
# Calculate the extended image size
extended_image_size = (
int(image_size[0] * (1 + extension_percentage)),
int(image_size[1] * (1 + extension_percentage))
)
# Calculate the size of the final collage image
collage_width = grid_size[0] * extended_image_size[0] + (grid_size[0] - 1) * spacing
collage_height = grid_size[1] * extended_image_size[1] + (grid_size[1] - 1) * spacing
collage_size = (collage_width, collage_height)
# Create a new blank image for the first collage
collage1 = Image.new("RGBA", collage_size, (0, 0, 0, 0))
# Open the background image
background_path = "background.png"
background = Image.open(background_path).resize(collage_size)
# Make the background image 20% transparent
background = background.convert("RGBA")
background_with_transparency = Image.new("RGBA", background.size)
for x in range(background.width):
for y in range(background.height):
r, g, b, a = background.getpixel((x, y))
background_with_transparency.putpixel((x, y), (r, g, b, int(a * 0.5)))
# Paste the background with transparency onto the first collage
collage1.paste(background_with_transparency, (0, 0))
# Iterate over the selected files and paste them onto the first collage
for i, file in enumerate(selected_files):
# Calculate the position to paste the current image
x = (i % grid_size[0]) * (extended_image_size[0] + spacing)
y = (i // grid_size[0]) * (extended_image_size[1] + spacing)
position = (x, y)
# Open the image file
image_path = os.path.join(input_dir, file)
image = Image.open(image_path)
# Calculate the extended background size
extended_background_size = (
int(extended_image_size[0] + 2 * (extension_percentage * image_size[0])),
int(extended_image_size[1] + 2 * (extension_percentage * image_size[1]))
)
# Create a new image with extended background
extended_image = Image.new("RGBA", extended_background_size, (0, 0, 0, 0))
# Calculate the position to paste the image on the extended background
image_position = (
int(extension_percentage * image_size[0]) + 3,
int(extension_percentage * image_size[1])
)
# Paste the image onto the extended background
extended_image.paste(image, image_position)
# Resize the extended image to the desired size
resized_image = extended_image.resize(extended_image_size)
# Create a new image with rounded rectangle border
border_image = Image.new("RGBA", extended_image_size, (0, 0, 0, 0))
border_draw = ImageDraw.Draw(border_image)
border_radius = 20
border_draw.rounded_rectangle(
[(0, 0), (extended_image_size[0] - 1, extended_image_size[1] - 1)],
border_radius,
fill=None, # Set fill to None for transparent fill
outline=(255, 165, 0, 255), # Set alpha to 255 (fully opaque)
width=border_thickness # Set the border thickness
)
# Composite the border image onto the first collage
collage1.alpha_composite(border_image, position)
# Create a new image with orange background and 20% transparency
orange_background = Image.new("RGBA", extended_image_size, (255, 165, 0, int(0.2 * 255)))
# Composite the resized image onto the orange background
orange_background.paste(resized_image, (0, 0), resized_image)
# Composite the image with orange background onto the first collage
collage1.alpha_composite(orange_background, position)
# Save the first collage image
output_dir = "./out"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
collage1.save("out/" + output_name + "_1.png")
print("First collage created and saved as " + output_name + "_1.png.")
# Randomly select 25 images (if available) for the second collage
#selected_files = random.sample(files, min(25, len(files)))
random.shuffle(selected_files)
# Create a new blank image for the second collage
collage2 = Image.new("RGBA", collage_size, (0, 0, 0, 0))
# Paste the background with transparency onto the second collage
collage2.paste(background_with_transparency, (0, 0))
# Iterate over the selected files and paste them onto the second collage
for i, file in enumerate(selected_files):
# Calculate the position to paste the current image
x = (i % grid_size[0]) * (extended_image_size[0] + spacing)
y = (i // grid_size[0]) * (extended_image_size[1] + spacing)
position = (x, y)
# Open the image file
image_path = os.path.join(input_dir, file)
image = Image.open(image_path)
# Calculate the extended background size
extended_background_size = (
int(extended_image_size[0] + 2 * (extension_percentage * image_size[0])),
int(extended_image_size[1] + 2 * (extension_percentage * image_size[1]))
)
# Create a new image with extended background
extended_image = Image.new("RGBA", extended_background_size, (0, 0, 0, 0))
# Calculate the position to paste the image on the extended background
image_position = (
int(extension_percentage * image_size[0]) + 3,
int(extension_percentage * image_size[1])
)
# Paste the image onto the extended background
extended_image.paste(image, image_position)
# Resize the extended image to the desired size
resized_image = extended_image.resize(extended_image_size)
# Create a new image with rounded rectangle border
border_image = Image.new("RGBA", extended_image_size, (0, 0, 0, 0))
border_draw = ImageDraw.Draw(border_image)
border_radius = 20
border_draw.rounded_rectangle(
[(0, 0), (extended_image_size[0] - 1, extended_image_size[1] - 1)],
border_radius,
fill=None, # Set fill to None for transparent fill
outline=(255, 165, 0, 255), # Set alpha to 255 (fully opaque)
width=border_thickness # Set the border thickness
)
# Composite the border image onto the second collage
collage2.alpha_composite(border_image, position)
# Create a new image with orange background and 20% transparency
orange_background = Image.new("RGBA", extended_image_size, (255, 165, 0, int(0.2 * 255)))
# Composite the resized image onto the orange background
orange_background.paste(resized_image, (0, 0), resized_image)
# Composite the image with orange background onto the second collage
collage2.alpha_composite(orange_background, position)
# Save the second collage image
collage2.save("out/" + output_name + "_2.png")
print("Second collage created and saved as " + output_name + "_2.png.")