forked from kylemcdonald/python-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_images.py
More file actions
28 lines (22 loc) · 955 Bytes
/
plot_images.py
File metadata and controls
28 lines (22 loc) · 955 Bytes
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
import numpy as np
def plot_images(images, xy, blend=np.maximum, canvas_shape=(512,512), fill=0):
h,w = images.shape[1:3]
if images.ndim == 4:
canvas_shape = (canvas_shape[0], canvas_shape[1], images.shape[3])
min_xy = np.amin(xy, 0)
max_xy = np.amax(xy, 0)
min_canvas = np.array((0, 0))
max_canvas = np.array((canvas_shape[0] - h, canvas_shape[1] - w))
xy_mapped = min_canvas + (xy - min_xy) * (max_canvas - min_canvas) / (max_xy - min_xy)
xy_mapped = xy_mapped.astype(int)
canvas = np.full(canvas_shape, fill)
for image, pos in zip(images, xy_mapped):
x_off, y_off = pos
sub_canvas = canvas[y_off:y_off+h, x_off:x_off+w]
sub_image = image[:h, :w]
try:
canvas[y_off:y_off+h, x_off:x_off+w] = blend(sub_canvas, sub_image)
except ValueError:
print(pos, h, w, min_canvas, max_canvas)
raise
return canvas