Skip to content

Instantly share code, notes, and snippets.

@wybert
Forked from lebedov/thumb_grid.py
Created November 16, 2018 10:13
Show Gist options
  • Save wybert/d3c4dd32177330637cdfb6dca35c2de7 to your computer and use it in GitHub Desktop.
Save wybert/d3c4dd32177330637cdfb6dca35c2de7 to your computer and use it in GitHub Desktop.
Display a grid of thumbnails.
#!/usr/bin/env python
"""
Display a grid of thumbnails.
"""
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, scale=0.1, axes_pad=0.07):
# Grid must be 2D:
assert len(grid_shape) == 2
# Make sure all images can fit in grid:
assert np.prod(grid_shape) >= len(im_list)
grid = ImageGrid(plt.gcf(), 111, grid_shape, axes_pad=axes_pad)
for i in range(N):
data_orig = im_list[i]
# Scale image:
im = PIL.Image.fromarray(data_orig)
thumb_shape = [int(scale*j) for j in im.size]
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)
if __name__ == '__main__':
import StringIO
import requests
r = requests.get('http://www.storyofmathematics.com/images2/hilbert.jpg')
im = np.array(PIL.Image.open(StringIO.StringIO(r.content)))
N = 16
im_list = [im for i in range(N)]
thumb_grid(im_list, (4, 4))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment