Skip to content

Instantly share code, notes, and snippets.

@hlzz
Created February 28, 2018 09:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hlzz/ba6da6e692beceb4da8b4d5387a8eacc to your computer and use it in GitHub Desktop.
Save hlzz/ba6da6e692beceb4da8b4d5387a8eacc to your computer and use it in GitHub Desktop.
Generate sprite images for tensorboard
#!/usr/bin/python
from PIL import Image
import math
test_image_list = '/home/tianwei/Data/sfm_data/test_image_list' # path to the image list
with open(test_image_list, 'r') as f:
test_images = f.readlines()
test_images = map(str.strip, test_images)
grid = int(math.sqrt(len(test_images))) + 1
image_height = int(8192 / grid) # tensorboard supports sprite images up to 8192 x 8192
image_width = int(8192 / grid)
big_image = Image.new(
mode='RGBA',
size=(image_width * grid, image_height * grid),
color=(0,0,0,0)) # fully transparent
for i in range(len(test_images)):
row = i / grid
col = i % grid
img = Image.open(test_images[i])
img = img.resize((image_height, image_width), Image.ANTIALIAS)
row_loc = row * image_height
col_loc = col * image_width
big_image.paste(img, (col_loc, row_loc)) # NOTE: the order is reverse due to PIL saving
print(row_loc, col_loc)
big_image.save('sprite_image.jpg', transparency=0)
@hlzz
Copy link
Author

hlzz commented Feb 28, 2018

This is used for making sprite images for the embedding projector.
http://projector.tensorflow.org/

@sdcubber
Copy link

sdcubber commented Dec 31, 2018

Hi, I'm trying to use your code to visualize images in tensorflow projector. PIL does not allow to save a .jpg image in RGBA format (the transparency channel is not supported). Any idea on how to approach this? The sprite looks nice, but I cannot get it to work in the projector.

Specifically, I get the "Number of tensors (196) do not match the number of lines in metadata (202)." error when trying to load the sprite as metadata. I am trying to plot 196 images.

@patakk
Copy link

patakk commented Mar 20, 2019

Have you found a solution to this problem? I'm also very positive that I've created the sprite the right way, but I just can't find a way of uploading it. If I upload it instead of the .tsv I get the number of tensors/lines mismatch error.

@nahilsobh
Copy link

nahilsobh commented Aug 27, 2019

This worked for me.
changed:

  1. RGBA --> RGB
  2. color=(0,0,0,0) --> color=(0,0,0)
  3. row = i / grid --> row = i // grid
  4. big_image.save('sprite_image.jpg', transparency=0) --> big_image.save('sprite_image.jpg')
big_image = Image.new( mode='RGB',
                       size=(image_width * grid, image_height * grid),
                       color=(0,0,0) )  # reduced it to RGB

for i in range(len(test_images)):

    row     = i // grid  # added integer divide
    col     = i % grid
    img     = Image.open(test_images[i])
    img     = img.resize((image_height, image_width), Image.ANTIALIAS)
    row_loc = row * image_height
    col_loc = col * image_width

    big_image.paste(img, (col_loc, row_loc)) # NOTE: the order is reverse due to PIL saving
    print(row_loc, col_loc)

big_image.save('sprite_image.jpg') # removed transparency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment