![](/img/trans.png)
[英]Matplotlib - unable to save image in same resolution as original image
[英]Matplotlib: How to save an image at full resolution?
我创建了一个马赛克图像,这样一张大图片由许多小图片组成。 我可以在 matplotlib 查看器中很好地查看此图像,并且可以放大以查看所有小图片。 但是当我保存图像时,无论扩展名如何,图像都会失去缩放能力,以至于放大时微小的图像变得模糊。有没有办法以全分辨率保存图像? 我有一个 numpy 数组中的 rgb 图像,所以如果有其他更合适的库也可以使用。
这应该工作:
from PIL import Image
Image.fromarray(numpy_img).save("img_path.png")
我认为您在这里被 Windows 的照片应用程序误导了,如果您放大太多,它会自动应用模糊。
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()
另一种选择是我编写的名为numpngw
的小型库。 如果 numpy 数组是img
(形状为 (m, n, 3) 的 8 位或 16 位无符号整数数组),则可以使用:
from numpngw import write_png
write_png('output.png', img)
(如果数组是浮点数,则必须将值转换为无符号整数。PNG 文件格式不存储浮点值。)
你可以试试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.