Created
February 5, 2019 19:41
-
-
Save RodrigoCMoraes/defb376cfacd81d4af03d1f82d45451b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from mpl_toolkits.axes_grid1 import ImageGrid | |
import numpy as np | |
import PIL | |
def thumb_grid(im_list, grid_shape, fname_out, scale=1.0, axes_pad=0.07, figsize=(7, 7), dpi=900): | |
assert len(grid_shape) == 2 # Grid must be 2D: | |
assert np.prod(grid_shape) >= len(im_list) # Make sure all images can fit in grid: | |
plt.figure(figsize=figsize, dpi=dpi) | |
grid = ImageGrid(plt.gcf(), 111, grid_shape, axes_pad=axes_pad) | |
shape_index = np.argmin([np.prod(np.array(im.shape)) for im in im_list]) | |
thumb_shape = np.array(np.array(im_list[shape_index]).shape) * scale | |
for i, im in enumerate(im_list): | |
data_orig = im.copy() | |
# Scale image: | |
im = PIL.Image.fromarray(data_orig) | |
im.thumbnail(thumb_shape, PIL.Image.ANTIALIAS) | |
data_thumb = np.array(im) | |
grid[i].imshow(data_thumb) | |
# Turn off axes: | |
grid[i].axes.get_xaxis().set_visible(False) | |
grid[i].axes.get_yaxis().set_visible(False) | |
plt.gca().set_axis_off() | |
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) | |
plt.margins(0,0) | |
plt.gca().xaxis.set_major_locator(plt.NullLocator()) | |
plt.gca().yaxis.set_major_locator(plt.NullLocator()) | |
plt.savefig(fname_out, bbox_inches = 'tight', pad_inches = 0.06) | |
# Usage: thumb_grid(images, (rows, columns), f'filename_output.jpeg') | |
# type(3D ndarray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment