将Numpy转换为PIL图像后图像失真

时间:2018-12-04 02:40:09

标签: python numpy matplotlib python-imaging-library

我想将一些Numpy转换为Image,所以我在PIL中使用了Image

from PIL import Image
im1 = Image.fromarray(np.uint8(X1)) # X1 dtype uint8, scale 0~255 image
im1.save("img.png")

im2= Image.fromarray(np.uint8(X2*255))   #X2 dtype uint16, sacke 0~1 mask ,so  X2*255
im2.save("mask.png")

X1,X2是numpy数组。 然后发生了一些混乱。一些图像看起来不错,但其他图像会失真。

enter image description here enter image description here

第一个是扭曲,第二个是正常。

1 个答案:

答案 0 :(得分:0)

在阅读 @Mark Setchell 评论后进行了编辑:由于PNG不会扭曲值,因此我猜它们使用JPG压缩。如果您希望原样保存图片,请尝试以下操作:

import cv2 

cv2.imwrite("img.png", img)

Here是文档。

相关问题