How to get the image torchvision.utils.save_image saves, without reading it back from disk?

Viewed 4125
from torchvision.utils import save_image

...
save_image(im, f'im_name.png')

In my case (standard mnist), using code from here, im is a Tensor:96, and save_image works.

I want that image in memory to show it in other plots, and I don't want to read it back after saving it, which seems kind of stupid.

Is there a way to separate the functionality of generating the image and of saving it?


Edit

clarification: I want an equivalent to

save_image(im, f'im_name.png')
reread = plt.imread(f'im_name.png')

without saving the image and reading it back. I just want the image, and I want to save it later. the save_image function does some work, like stacking multiple images into one, converting the tensor to images of correct sizes and so on. I want only that part without the saving to disk.

1 Answers

About 2 weeks later, I stumbled upon the solution by accident.

grid = torchvision.utils.make_grid(im)

grid will be the image save_image was just saving.

Related