Caspertijmen1
Caspertijmen1

Reputation: 371

Matplotlib: How to save an image at full resolution?

I've created a mozaic image, such that a large picture consists of many tiny pictures. I can view this image just fine in the matplotlib viewer and I can zoom in to see all the tiny pictures. But when I save the image, no matter the extension, the image loses the zoom ability such that the tiny images become blurred when zooming in. Is there a way to save the image in full resolution? I have the image in rgb in a numpy array so if there are other libraries more suitable that would work too.

Upvotes: 1

Views: 2787

Answers (4)

ffsedd
ffsedd

Reputation: 305

You can try imageio library: https://imageio.readthedocs.io/en/stable/userapi.html#imageio.imwrite

from imageio import imwrite

imwrite("image.png", array)

Upvotes: 0

Warren Weckesser
Warren Weckesser

Reputation: 114946

Another option is the small library that I wrote called numpngw. If the numpy array is, say, img (an array of 8-bit or 16-bit unsigned integers with shape (m, n, 3)), you could use:

from numpngw import write_png


write_png('output.png', img)

(If the array is floating point, you'll have to convert the values to unsigned integers. The PNG file format does not store floating point values.)

Upvotes: 0

user14977424
user14977424

Reputation:

I think you're being mislead by Windows's Photos Application here, which applies blur automatically if you zoom too much.

The image is being saved correctly with all pixel values by Matplotlib, you can check it by zooming again on the pixels of the loaded image.

import numpy as np

import matplotlib.pyplot as plt
import matplotlib.image as mpimg


# save image

array = np.random.random((4000, 4000, 3))
plt.imsave("save.png", array)


# load image

img = mpimg.imread("save.png")
plt.imshow(img)
plt.show()

Upvotes: 2

RandomGuy
RandomGuy

Reputation: 1207

This should work :

from PIL import Image

Image.fromarray(numpy_img).save("img_path.png")

Upvotes: 3

Related Questions