3

I have multiple(in millions) numpy 2-D arrays which need to be saved. One can save an individual image like this:

import numpy as np
import matplotlib.pyplot as plt

surface_profile = np.empty((50,50)) #numpy array to be saved
plt.figure()
plt.imshow(surface_profile)
save_filename='filename.png'
plt.savefig(save_filename)

However this process also displays the image which I don't require. If I keep saving million images like this, I should somehow avoid imshow() function of matplotlib. Any help???

PS: I forgot to mention that I am using Spyder.

Vaibhav Dixit
  • 844
  • 1
  • 9
  • 12
  • Shouldn't happen in general, but it might be related to Jupyter/Spyder. See [this answer](https://stackoverflow.com/a/29931148/6220759). – Josh Friedlander May 24 '20 at 11:55
  • In what environment are you running this code? In a non-interactive environment you should not get any display until you run `plt.show()`. Try adding `plt.ioff()` at the beginning of your script. See https://matplotlib.org/tutorials/introductory/usage.html?highlight=ioff#what-is-interactive-mode – Diziet Asahi May 24 '20 at 11:55
  • Tried with plt.ioff() It still displays images – Vaibhav Dixit May 24 '20 at 12:06

1 Answers1

1

Your problem is using plt.imshow(surface_profile) to create the image as this will always display the image as well.

This can be done using PIL, try the following,

from PIL import Image
import numpy as np

surface_profile = np.empty((50,50)) #numpy array to be saved
im = Image.fromarray(surface_profile)
im = im.convert('RGB')
save_filename="filename.png"
im.save(save_filename, "PNG")
DrBwts
  • 3,470
  • 6
  • 38
  • 62