Skip to content

Instantly share code, notes, and snippets.

@AndrewBMartin
Last active June 20, 2019 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewBMartin/ab06f4708124ccb4cacc4b158c3cef12 to your computer and use it in GitHub Desktop.
Save AndrewBMartin/ab06f4708124ccb4cacc4b158c3cef12 to your computer and use it in GitHub Desktop.
Create a sprite for Tensorboard from an array of images
import numpy as np
def create_sprite(data):
"""
Tile images into sprite image.
Add any necessary padding
"""
# For B&W or greyscale images
if len(data.shape) == 3:
data = np.tile(data[...,np.newaxis], (1,1,1,3))
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = ((0, n ** 2 - data.shape[0]), (0, 0), (0, 0), (0, 0))
data = np.pad(data, padding, mode='constant',
constant_values=0)
# Tile images into sprite
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3, 4))
# print(data.shape) => (n, image_height, n, image_width, 3)
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
# print(data.shape) => (n * image_height, n * image_width, 3)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment