1

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.

1
  • The fig.savefig method has a dpi option. Commented Jul 9, 2021 at 13:38

4 Answers 4

3

This should work :

from PIL import Image

Image.fromarray(numpy_img).save("img_path.png")
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

0

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

from imageio import imwrite

imwrite("image.png", array)

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.