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.

3
  • Shouldn't happen in general, but it might be related to Jupyter/Spyder. See this answer. Commented May 24, 2020 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 matplotlib.org/tutorials/introductory/… Commented May 24, 2020 at 11:55
  • Tried with plt.ioff() It still displays images Commented May 24, 2020 at 12:06

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.