-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageMerger.py
176 lines (135 loc) · 5.35 KB
/
ImageMerger.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
import csv
import getopt
import os
import sys
from PIL import Image, ImageColor
def merge_images(orientation: str, size: int, mode: str, fill_color: tuple, image_paths: list[str]) -> tuple[Image.Image, bool]:
try:
images = []
final_width = 0
final_height = 0
current_width = 0
current_height = 0
current_size = 0
for image_path in image_paths:
image = Image.open(image_path)
images.append(image)
if size == 0 or current_size < size:
current_size += 1
else:
final_width = max(final_width, current_width)
final_height = max(final_height, current_height)
current_width = 0
current_height = 0
current_size = 1
if orientation == "v":
current_width = max(current_width, final_width + image.width)
current_height += image.height
else:
current_width += image.width
current_height = max(
current_height, final_height + image.height)
final_width = max(final_width, current_width)
final_height = max(final_height, current_height)
mergedImage = Image.new(mode=mode, size=(
final_width, final_height), color=fill_color)
current_size = 0
current_primary_pixel = 0
current_secondary_pixel = 0
next_secondary_pixel = 0
for image in images:
if size == 0 or current_size < size:
current_size += 1
else:
current_size = 1
current_primary_pixel = 0
current_secondary_pixel = next_secondary_pixel
next_secondary_pixel = 0
if orientation == "v":
mergedImage.paste(
image, (current_secondary_pixel, current_primary_pixel))
current_primary_pixel += image.height
next_secondary_pixel = max(
next_secondary_pixel, current_secondary_pixel + image.width)
else:
mergedImage.paste(
image, (current_primary_pixel, current_secondary_pixel))
current_primary_pixel += image.width
next_secondary_pixel = max(
next_secondary_pixel, current_secondary_pixel + image.height)
return mergedImage, True
except:
return None, False
def merge_and_save(orientation: str, size: int, mode: str, fill_color: tuple, optimize: bool, quality: int, merge_path: str, image_paths: list[str]) -> tuple[Image.Image, bool]:
try:
finalImage, success = merge_images(
orientation=orientation, size=size, mode=mode, fill_color=fill_color, image_paths=image_paths)
if success:
dirname = os.path.dirname(merge_path)
if len(dirname) > 0:
os.makedirs(dirname, exist_ok=True)
finalImage.save(optimize=optimize, quality=quality, fp=merge_path)
return finalImage, True
else:
return None, False
except:
return None, False
def main(argv):
orientation = "h"
size = 0
mode = "RGBA"
fill_color = "#00000000"
optimize = True
quality = 75
verbose = False
merge_path = "merge.png"
option_values, image_paths = getopt.getopt(args=argv, shortopts="", longopts=[
"orientation=", "size=", "mode=", "fill-color=", "optimize=", "quality=", "verbose", "merge-path="])
for option_name, option_value in option_values:
if option_name == "--orientation":
try:
if option_value[0].lower() == "v":
orientation = "v"
except:
pass
elif option_name == "--size":
try:
size = int(option_value)
except:
pass
elif option_name == "--mode":
mode = option_value
elif option_name == "--fill-color":
fill_color = ImageColor.getrgb(option_value)
elif option_name == "--optimize":
try:
if option_value[0].lower() in ["f", "n"]:
optimize = False
except:
pass
elif option_name == "--quality":
try:
quality = int(option_value)
except:
pass
elif option_name == "--verbose":
verbose = True
elif option_name == "--merge-path":
merge_path = option_value
if verbose:
print("--- Merge Arguments ---")
print("orientation:", orientation)
print(" size:", size)
print(" mode:", mode)
print(" fill_color:", fill_color)
print(" optimize:", optimize)
print(" quality:", quality)
print(" merge_path:", merge_path)
print("image_paths:", image_paths)
print("-----------------------")
_, success = merge_and_save(orientation=orientation, size=size, mode=mode, fill_color=fill_color,
optimize=optimize, quality=quality, merge_path=merge_path, image_paths=image_paths)
print("Merge", image_paths, "into", merge_path,
"SUCCESS ☑" if success else "FAILURE ☒")
if __name__ == "__main__":
main(sys.argv[1:])