使用 imageio.imsave 保存 NumPy 数组会扭曲图像
Saving a NumPy array with imageio.imsave distorts the image
我正在 Python 中使用 imageio 库处理图像,但得到了意想不到的结果。
我用下面的代码测试了这个问题:
import imageio
import numpy as np
imgarray = np.zeros((3, 4032, 3024), dtype=np.uint8)
imgarray[0, 0::2, 1::2] = 255
print('Original\n', imgarray)
imageio.imsave('test.jpg', imgarray.transpose(1, 2, 0))
img = imageio.imread('test.jpg')
imgarray = np.array(img).transpose(2, 0, 1)
print('\n\nSave and load\n', imgarray)
打印结果为:
Original
[[[ 0 255 0 ... 255 0 255]
[ 0 0 0 ... 0 0 0]
[ 0 255 0 ... 255 0 255]
...
[ 0 0 0 ... 0 0 0]
[ 0 255 0 ... 255 0 255]
[ 0 0 0 ... 0 0 0]]
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]]
Save and load
[[[ 50 112 48 ... 120 56 118]
[ 45 46 45 ... 45 45 46]
[ 45 116 55 ... 112 52 129]
...
[ 53 45 45 ... 50 45 45]
[ 48 114 49 ... 117 53 119]
[ 45 47 46 ... 45 45 46]]
[[ 0 48 0 ... 56 0 54]
[ 0 0 0 ... 0 0 0]
[ 0 52 0 ... 48 0 65]
...
[ 0 0 0 ... 0 0 0]
[ 0 50 0 ... 53 0 55]
[ 0 0 0 ... 0 0 0]]
[[ 0 48 0 ... 56 0 54]
[ 0 0 0 ... 0 0 0]
[ 0 52 0 ... 48 0 65]
...
[ 0 0 0 ... 0 0 0]
[ 0 50 0 ... 53 0 55]
[ 0 0 0 ... 0 0 0]]]
为什么原来的numpy数组和保存加载后的numpy数组不一样?
我认为保存和加载后它们应该是相同的。
我在使用不同的库时遇到同样的问题,例如cv2 或 PIL.Image.
有没有办法在保存和加载后保持数据不变?
JPEG is an image format that uses lossy compression. The algorithm is designed to reduce file size by removing details that the human eye would usually not perceive. If you need to retain the exact pixel information, use an image format with lossless compression, such as PNG.
要使您的代码按预期工作,您只需更改文件结尾:test.png
而不是 test.jpg
。 ImageIO 会处理剩下的事情。
我正在 Python 中使用 imageio 库处理图像,但得到了意想不到的结果。
我用下面的代码测试了这个问题:
import imageio
import numpy as np
imgarray = np.zeros((3, 4032, 3024), dtype=np.uint8)
imgarray[0, 0::2, 1::2] = 255
print('Original\n', imgarray)
imageio.imsave('test.jpg', imgarray.transpose(1, 2, 0))
img = imageio.imread('test.jpg')
imgarray = np.array(img).transpose(2, 0, 1)
print('\n\nSave and load\n', imgarray)
打印结果为:
Original
[[[ 0 255 0 ... 255 0 255]
[ 0 0 0 ... 0 0 0]
[ 0 255 0 ... 255 0 255]
...
[ 0 0 0 ... 0 0 0]
[ 0 255 0 ... 255 0 255]
[ 0 0 0 ... 0 0 0]]
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]]
Save and load
[[[ 50 112 48 ... 120 56 118]
[ 45 46 45 ... 45 45 46]
[ 45 116 55 ... 112 52 129]
...
[ 53 45 45 ... 50 45 45]
[ 48 114 49 ... 117 53 119]
[ 45 47 46 ... 45 45 46]]
[[ 0 48 0 ... 56 0 54]
[ 0 0 0 ... 0 0 0]
[ 0 52 0 ... 48 0 65]
...
[ 0 0 0 ... 0 0 0]
[ 0 50 0 ... 53 0 55]
[ 0 0 0 ... 0 0 0]]
[[ 0 48 0 ... 56 0 54]
[ 0 0 0 ... 0 0 0]
[ 0 52 0 ... 48 0 65]
...
[ 0 0 0 ... 0 0 0]
[ 0 50 0 ... 53 0 55]
[ 0 0 0 ... 0 0 0]]]
为什么原来的numpy数组和保存加载后的numpy数组不一样?
我认为保存和加载后它们应该是相同的。
我在使用不同的库时遇到同样的问题,例如cv2 或 PIL.Image.
有没有办法在保存和加载后保持数据不变?
JPEG is an image format that uses lossy compression. The algorithm is designed to reduce file size by removing details that the human eye would usually not perceive. If you need to retain the exact pixel information, use an image format with lossless compression, such as PNG.
要使您的代码按预期工作,您只需更改文件结尾:test.png
而不是 test.jpg
。 ImageIO 会处理剩下的事情。