Created
February 28, 2018 09:03
-
-
Save hlzz/ba6da6e692beceb4da8b4d5387a8eacc to your computer and use it in GitHub Desktop.
Generate sprite images for tensorboard
This file contains 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
#!/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) |
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.
This worked for me.
changed:
- RGBA --> RGB
- color=(0,0,0,0) --> color=(0,0,0)
- row = i / grid --> row = i // grid
- 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
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.