| 1 |
import collections |
| 2 |
import math |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
|
| 6 |
from .centerize import centerize |
| 7 |
from .color import gray2rgb |
| 8 |
from .color import rgb2rgba |
| 9 |
|
| 10 |
|
| 11 |
def _tile(imgs, shape, border=None, border_width=None): |
| 12 |
y_num, x_num = shape |
| 13 |
tile_h, tile_w, channel = imgs[0].shape |
| 14 |
|
| 15 |
if border is None: |
| 16 |
border_width = 0 |
| 17 |
|
| 18 |
dst = np.zeros( |
| 19 |
( |
| 20 |
tile_h * y_num + border_width * (y_num - 1), |
| 21 |
tile_w * x_num + border_width * (x_num - 1), |
| 22 |
channel, |
| 23 |
), |
| 24 |
dtype=np.uint8, |
| 25 |
) |
| 26 |
if border is not None: |
| 27 |
dst[...] = border |
| 28 |
|
| 29 |
for y in range(y_num): |
| 30 |
for x in range(x_num): |
| 31 |
i = x + y * x_num |
| 32 |
if i < len(imgs): |
| 33 |
y1 = y * tile_h + y * border_width |
| 34 |
y2 = y1 + tile_h |
| 35 |
x1 = x * tile_w + x * border_width |
| 36 |
x2 = x1 + tile_w |
| 37 |
dst[y1:y2, x1:x2] = imgs[i] |
| 38 |
return dst |
| 39 |
|
| 40 |
|
| 41 |
def _get_tile_shape(num, hw_ratio=1): |
| 42 |
r_num = int(round(math.sqrt(num / hw_ratio))) |
| 43 |
c_num = 0 |
| 44 |
while r_num * c_num < num: |
| 45 |
c_num += 1 |
| 46 |
while (r_num - 1) * c_num >= num: |
| 47 |
r_num -= 1 |
| 48 |
return r_num, c_num |
| 49 |
|
| 50 |
|
| 51 |
def tile( |
| 52 |
imgs, |
| 53 |
shape=None, |
| 54 |
cval=None, |
| 55 |
border=None, |
| 56 |
border_width=None, |
| 57 |
): |
| 58 |
"""Tile images. |
| 59 |
|
| 60 |
Parameters |
| 61 |
---------- |
| 62 |
imgs: numpy.ndarray |
| 63 |
Image list which should be tiled. |
| 64 |
shape: tuple of int |
| 65 |
Tile shape. |
| 66 |
cval: array-like, optional |
| 67 |
Color to fill the background. Default is (0, 0, 0). |
| 68 |
border: array-like, optional |
| 69 |
Color for the border. If None, the border is not drawn. |
| 70 |
border_width: int |
| 71 |
Pixel size of the border. |
| 72 |
|
| 73 |
Returns |
| 74 |
------- |
| 75 |
dst: numpy.ndarray |
| 76 |
Tiled image. |
| 77 |
|
| 78 |
""" |
| 79 |
imgs = list(imgs) |
| 80 |
|
| 81 |
|
| 82 |
max_h, max_w = np.array([img.shape[:2] for img in imgs]).max(axis=0) |
| 83 |
|
| 84 |
if shape is None: |
| 85 |
shape = _get_tile_shape(len(imgs), hw_ratio=1.0 * max_h / max_w) |
| 86 |
else: |
| 87 |
assert isinstance(shape, collections.abc.Iterable) |
| 88 |
assert len(shape) == 2 |
| 89 |
assert isinstance(shape[0], int) |
| 90 |
assert isinstance(shape[1], int) |
| 91 |
if shape[0] < 0 and shape[1] > 0: |
| 92 |
shape = (math.ceil(len(imgs) / shape[1]), shape[1]) |
| 93 |
elif shape[1] < 0 and shape[0] > 0: |
| 94 |
shape = (shape[0], math.ceil(len(imgs) / shape[0])) |
| 95 |
else: |
| 96 |
assert shape[0] > 0 and shape[1] > 0 |
| 97 |
|
| 98 |
imgs = imgs[: shape[0] * shape[1]] |
| 99 |
|
| 100 |
if cval is None: |
| 101 |
cval = 0 |
| 102 |
|
| 103 |
if border is not None: |
| 104 |
border = np.asarray(border, dtype=np.uint8) |
| 105 |
|
| 106 |
if border_width is None: |
| 107 |
border_width = 3 |
| 108 |
|
| 109 |
ndim = max(img.ndim for img in imgs) |
| 110 |
if ndim == 3: |
| 111 |
channel = max(img.shape[2] for img in imgs if img.ndim == 3) |
| 112 |
else: |
| 113 |
ndim = 3 |
| 114 |
channel = 3 |
| 115 |
assert channel in [3, 4] |
| 116 |
|
| 117 |
|
| 118 |
for i in range(shape[0] * shape[1]): |
| 119 |
if i < len(imgs): |
| 120 |
img = imgs[i] |
| 121 |
assert img.dtype == np.uint8 |
| 122 |
|
| 123 |
if ndim == 3 and img.ndim == 2: |
| 124 |
img = gray2rgb(img) |
| 125 |
if channel == 4 and img.shape[2] == 3: |
| 126 |
img = rgb2rgba(img) |
| 127 |
|
| 128 |
img = centerize(src=img, shape=(max_h, max_w, channel), cval=cval) |
| 129 |
imgs[i] = img |
| 130 |
else: |
| 131 |
img = np.full((max_h, max_w, channel), cval, dtype=np.uint8) |
| 132 |
imgs.append(img) |
| 133 |
|
| 134 |
return _tile(imgs=imgs, shape=shape, border=border, border_width=border_width) |