Skip to content

Commit 593db3b

Browse files
committed
Generalized image_grid and border and created arrange module.
1 parent e3a2861 commit 593db3b

File tree

5 files changed

+62
-46
lines changed

5 files changed

+62
-46
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,6 @@ cython_debug/
142142
# Example output
143143
*.ppm
144144
!examples/colorspace/barn.ppm
145+
146+
# Project planning document
147+
TODO.md

dmtools/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
from . import colorspace
1818
from . import netpbm
1919
from . import sound
20+
from . import arrange

dmtools/arrange.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import numpy as np
2+
from typing import List
3+
4+
def image_grid(images: List[np.ndarray], w: int, h: int, b: int,
5+
color: int = "white", k: int = 255) -> np.ndarray:
6+
"""Create a w * h grid of images with a border of width b.
7+
8+
Args:
9+
images (List[np.ndarray]): images (of same dimension) for grid.
10+
w (int): number of images in each row of the grid.
11+
h (int): number of images in each column of the grid.
12+
b (int): width of the border/margin.
13+
color (int): color of border {'white', 'black'} (defaults to white).
14+
k (int): white point (defaults to 255).
15+
16+
Returns:
17+
np.ndarray: grid layout of the images.
18+
"""
19+
n,m = images[0].shape
20+
c = {'white': k, 'black': 0}[color]
21+
h_border = c*np.ones((b, w*m + (w+1)*b))
22+
v_border = c*np.ones((n, b))
23+
grid_layout = h_border
24+
p = 0
25+
for i in range(h):
26+
row = v_border
27+
for j in range(w):
28+
row = np.hstack((row, images[p]))
29+
row = np.hstack((row, v_border))
30+
p += 1
31+
grid_layout = np.vstack((grid_layout, row))
32+
grid_layout = np.vstack((grid_layout, h_border))
33+
return grid_layout.astype(int)
34+
35+
36+
def border(image: np.ndarray, b: int,
37+
color: int = "white", k: int = 255) -> np.ndarray:
38+
"""Add a border of width b to the image.
39+
40+
Args:
41+
image (Netpbm): Netpbm image to add a border to
42+
b (int): width of the border/margin.
43+
color (int): color of border {'white', 'black'} (defaults to white).
44+
k (int): white point (defaults to 255).
45+
46+
Returns:
47+
np.ndarray: Image with border added.
48+
"""
49+
return image_grid([image], w=1, h=1, b=b, color=color, k=k)

dmtools/netpbm.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -202,49 +202,3 @@ def read_netpbm(path: str) -> Netpbm:
202202
else:
203203
# P4, P5, P6 are the binary (raw) formats
204204
return _parse_binary_netpbm(path)
205-
206-
207-
def image_grid(images: List[Netpbm], w: int, h: int, b: int,
208-
color: int = "white") -> Netpbm:
209-
"""Create a w * h grid of images with a border of width b.
210-
211-
Args:
212-
images (List[Netpbm]): images to be put in the grid (same dimensions).
213-
w (int): number of images in each row of the grid.
214-
h (int): number of images in each column of the grid.
215-
b (int): width of the border/margin.
216-
color (int): color of border {'white', 'black'} (defaults to white).
217-
218-
Returns:
219-
Netpbm: grid layout of the images.
220-
"""
221-
n,m = images[0].M.shape
222-
k = images[0].k
223-
c = {'white': k, 'black': 0}[color]
224-
h_border = c*np.ones((b, w*m + (w+1)*b))
225-
v_border = c*np.ones((n, b))
226-
grid_layout = h_border
227-
p = 0
228-
for i in range(h):
229-
row = v_border
230-
for j in range(w):
231-
row = np.hstack((row, images[p].M))
232-
row = np.hstack((row, v_border))
233-
p += 1
234-
grid_layout = np.vstack((grid_layout, row))
235-
grid_layout = np.vstack((grid_layout, h_border))
236-
return Netpbm(P=images[0].P, k=k, M=grid_layout.astype(int))
237-
238-
239-
def border(image: Netpbm, b: int, color: int = "white") -> Netpbm:
240-
"""Add a border of width b to the image
241-
242-
Args:
243-
image (Netpbm): Netpbm image to add a border to
244-
b (int): width of the border/margin.
245-
color (int): color of border {'white', 'black'} (defaults to white).
246-
247-
Returns:
248-
Netpbm: Image with border added.
249-
"""
250-
return image_grid([image], w=1, h=1, b=b, color=color)

docs/dmtools.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ dmtools.sound module
4040
:members:
4141
:undoc-members:
4242
:show-inheritance:
43+
44+
45+
dmtools.arrange module
46+
----------------------
47+
48+
.. automodule:: dmtools.arrange
49+
:members:
50+
:undoc-members:
51+
:show-inheritance:

0 commit comments

Comments
 (0)