Efficiently stack a list of images into a grid with optional spacing. input: a list of list, each element is a list of images representing a row, each image is a numpy array of shape (H, W, 3) output: a numpy array of shape (H*nrow + spacing*(nrow-1), W*ncol + spacing*(ncol-1), 3)
(image_rows: List[List[np.ndarray]], spacing=5, spacing_color=0)
| 41 | |
| 42 | |
| 43 | def stack_images(image_rows: List[List[np.ndarray]], spacing=5, spacing_color=0) -> np.ndarray: |
| 44 | ''' |
| 45 | Efficiently stack a list of images into a grid with optional spacing. |
| 46 | input: a list of list, each element is a list of images representing a row, each image is a numpy array of shape (H, W, 3) |
| 47 | output: a numpy array of shape (H*nrow + spacing*(nrow-1), W*ncol + spacing*(ncol-1), 3) |
| 48 | ''' |
| 49 | nrow = len(image_rows) |
| 50 | |
| 51 | ncol = len(image_rows[0]) |
| 52 | H, W = image_rows[0][0].shape[:2] |
| 53 | |
| 54 | # Calculate the final grid size, only add spacing between images, not at the edges |
| 55 | grid_height = nrow * H + spacing * (nrow - 1) |
| 56 | grid_width = ncol * W + spacing * (ncol - 1) |
| 57 | image_grid = np.ones((grid_height, grid_width, 3), dtype=np.uint8) * spacing_color |
| 58 | for i in range(nrow): |
| 59 | for j in range(ncol): |
| 60 | y = i * (H + spacing) |
| 61 | x = j * (W + spacing) |
| 62 | image_grid[y:y+H, x:x+W] = image_rows[i][j] |
| 63 | return image_grid |
nothing calls this directly
no outgoing calls
no test coverage detected