-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile_image.py
More file actions
97 lines (63 loc) · 2.89 KB
/
Copy pathtile_image.py
File metadata and controls
97 lines (63 loc) · 2.89 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
import random
import sys
import argparse
import numpy as np
import cv2
def main(argv):
parser = argparse.ArgumentParser(
prog="ImageTiler",
description="what it says on the tin"
)
parser.add_argument("src_file")
parser.add_argument("out_file")
parser.add_argument("-x", "--num-x", help="number of tiles in x (horizontal) direction", required=True, type=int)
parser.add_argument("-y", "--num-y", help="number of tiles in y (vertical) direction", required=True, type=int)
parser.add_argument("-w", "--width", help="desired width of output image (can be inferred from height)", type=int)
parser.add_argument("--height", help="desired height of output image (can be inferred from width)", type=int)
parser.add_argument("-n", "--noshow", action="store_true", help="just save the image, don't show a preview")
parser.add_argument("--random_dimming", help="applies random dimming between 0.0 and supplied value to each tile", type=float, default=0.0)
args = parser.parse_args(argv[1:])
num_x, num_y = args.num_x, args.num_y
w, h = args.width, args.height
random_dimming: float = args.random_dimming
if w is None and h is None:
print("ERROR: either width or height must be specified")
sys.exit(1)
if h is None:
h = int((num_y / num_x) * w)
if w is None:
w = int((num_x / num_y) * h)
src_image = cv2.imread(args.src_file)
if src_image is None:
print(f"ERROR: bad path: {args.src_file}")
sys.exit(1)
# resize preemptively (doesn't get final size exact, but pretty close)
src_width = int(w / num_x + 0.999)
src_height = int(h / num_y + 0.999)
print(f"shrinking source to {src_width}, {src_height}")
src_image = cv2.resize(src_image, (src_width, src_height), interpolation=cv2.INTER_CUBIC)
if random_dimming == 0:
horizontal_seq = [src_image] * num_x
horizontal_strip = np.hstack(horizontal_seq)
vertical_seq = [horizontal_strip] * num_y
unresized_image = np.vstack(vertical_seq)
else:
src_image_float = src_image.astype(float) / 255
rows = []
for y in range(num_y):
seq = []
for x in range(num_x):
dim_amount = 1 - random.uniform(0.0, random_dimming)
dimmed = src_image_float * dim_amount
seq.append(dimmed)
rows.append(np.hstack(seq))
unresized_image = np.vstack(rows)
unresized_image = (unresized_image * 255).astype(np.uint8)
print(f"final size: {w}, {h} (shrinking from {unresized_image.shape[1]}, {unresized_image.shape[0]})")
resized_image = cv2.resize(unresized_image, (w, h), interpolation=cv2.INTER_CUBIC)
if not args.noshow:
cv2.imshow("tiled", resized_image)
cv2.waitKey(0)
cv2.imwrite(args.out_file, resized_image)
if __name__ == "__main__":
main(sys.argv)