Vaibhav Dixit
Vaibhav Dixit

Reputation: 894

How to save images using matplotlib without displaying them?

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.

Upvotes: 3

Views: 4543

Answers (1)

DrBwts
DrBwts

Reputation: 3657

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")

Upvotes: 2

Related Questions