简体   繁体   English

Matplotlib:如何以全分辨率保存图像?

[英]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.我可以在 matplotlib 查看器中很好地查看此图像,并且可以放大以查看所有小图片。 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.我有一个 numpy 数组中的 rgb 图像,所以如果有其他更合适的库也可以使用。

This should work :这应该工作:

from PIL import Image

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

I think you're being mislead by Windows's Photos Application here, which applies blur automatically if you zoom too much.我认为您在这里被 Windows 的照片应用程序误导了,如果您放大太多,它会自动应用模糊。

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. Matplotlib 使用所有像素值正确保存图像,您可以通过再次缩放加载图像的像素来检查它。

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

Another option is the small library that I wrote called numpngw .另一种选择是我编写的名为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:如果 numpy 数组是img (形状为 (m, n, 3) 的 8 位或 16 位无符号整数数组),则可以使用:

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.) (如果数组是浮点数,则必须将值转换为无符号整数。PNG 文件格式不存储浮点值。)

You can try imageio library: https://imageio.readthedocs.io/en/stable/userapi.html#imageio.imwrite你可以试试imageio库: https ://imageio.readthedocs.io/en/stable/userapi.html#imageio.imwrite

from imageio import imwrite

imwrite("image.png", array)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM