mckabue commited on
Commit
8603a32
·
verified ·
1 Parent(s): da90469

RE_UPLOAD-REBUILD-RESTART

Browse files
Files changed (1) hide show
  1. utils/show_tile_images.py +62 -0
utils/show_tile_images.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import numpy as np
3
+ from PIL import Image
4
+ from typing import List
5
+ import io
6
+
7
+ def fig2img(fig: plt.Figure):
8
+ """Convert a Matplotlib figure to a PIL Image and return it"""
9
+ plt.close()
10
+ buf = io.BytesIO()
11
+ fig.savefig(buf)
12
+ buf.seek(0)
13
+ img = Image.open(buf)
14
+ return img
15
+
16
+ def show_tile_images(
17
+ images: List[np.ndarray | Image.Image],
18
+ width_parts: int,
19
+ figsize = None,
20
+ space = 0.0,
21
+ pad = False,
22
+ figcolor = 'white',
23
+ titles: List[str] = None,
24
+ title_color: str = None,
25
+ title_background_color: str = None,
26
+ title_font_size: int = None):
27
+ '''
28
+ Show images in a tile format
29
+ Args:
30
+ images: List of images to show
31
+ width_parts: Number of images to show in a row
32
+ figsize: Size of the figure
33
+ space: Space between images
34
+ pad: Whether to pad the images or not
35
+ figcolor: Background color of the figure
36
+ titles: Titles of the images
37
+ title_color: Color of the title
38
+ title_background_color: Background color of the title
39
+ title_font_size: Font size of the title
40
+ Returns:
41
+ Image: Image of the figure
42
+ '''
43
+ height = int(np.ceil(len(images) / width_parts))
44
+ fig, axs = plt.subplots(height, width_parts, figsize=figsize if figsize != None else (8 * 2, 12 * height))
45
+ fig.patch.set_facecolor(figcolor)
46
+ axes = axs.flatten() if isinstance(axs, np.ndarray) else [axs]
47
+ titles = (titles or []) + np.full(len(images) - len(titles or []), None).tolist()
48
+ for img, ax, title in zip(images, axes, titles):
49
+ if title:
50
+ params = {k: v for k, v in { 'color': title_color, 'backgroundcolor': title_background_color, 'fontsize': title_font_size }.items() if v is not None}
51
+ ax.set_title(title, **params)
52
+ ax.imshow(img.convert("RGB") if not isinstance(img, np.ndarray) else img)
53
+ ax.axis('off')
54
+ if pad:
55
+ fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=space, hspace=space)
56
+ fig.tight_layout(h_pad=space, w_pad = space, pad = space)
57
+ else:
58
+ fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=space, hspace=space)
59
+ fig.tight_layout(h_pad=space, w_pad = space, pad = 0)
60
+ plt.margins()
61
+ plt.close()
62
+ return fig2img(fig)