Scipy imsave 和 imread 改变格式
Scipy imsave and imread change format
当我保存图像时,它的格式是 numpy.uint16
,当我加载它时,它是 numpy.uint8
,它把我的整个管道搞得一团糟。我该如何防止这种情况发生?
我在打电话
from scipy.misc import imread, imsave
image = imread(path)
imread(image_path)
imsave
和 imread
方法已弃用,将在 SciPy 的未来版本中删除。使用 imageio.imwrite
和 imageio.imread
应该可以解决这个问题。
>>> import imageio
>>> img = imageio.imread('img.jpg')
>>> img.dtype
dtype('uint8')
>>> imageio.imwrite('img_saved.jpg', img)
>>> img_read = imageio.imread('img_saved.jpg')
>>> img_read.dtype
dtype('uint8')
我会像这样在 TIFF 中写入和读取 16 位灰度数据:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Make a greyscale image of random 16-bit ints in range 0..65535
arr = np.random.randint(0,65535,(320,240), dtype=np.uint16)
# Make PIL image from numpy array and save
im=Image.fromarray(arr).save("result.tif")
# Re-read from file
reloadedim = Image.open("result.tif")
reloadedarr = np.array(reloadedim)
# Calculate difference
diff = arr - reloadedarr
print("Number of different pixels: {}".format(sum(diff.ravel())))
它打印 "Number of different pixels: 0".
当我保存图像时,它的格式是 numpy.uint16
,当我加载它时,它是 numpy.uint8
,它把我的整个管道搞得一团糟。我该如何防止这种情况发生?
我在打电话
from scipy.misc import imread, imsave
image = imread(path)
imread(image_path)
imsave
和 imread
方法已弃用,将在 SciPy 的未来版本中删除。使用 imageio.imwrite
和 imageio.imread
应该可以解决这个问题。
>>> import imageio
>>> img = imageio.imread('img.jpg')
>>> img.dtype
dtype('uint8')
>>> imageio.imwrite('img_saved.jpg', img)
>>> img_read = imageio.imread('img_saved.jpg')
>>> img_read.dtype
dtype('uint8')
我会像这样在 TIFF 中写入和读取 16 位灰度数据:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Make a greyscale image of random 16-bit ints in range 0..65535
arr = np.random.randint(0,65535,(320,240), dtype=np.uint16)
# Make PIL image from numpy array and save
im=Image.fromarray(arr).save("result.tif")
# Re-read from file
reloadedim = Image.open("result.tif")
reloadedarr = np.array(reloadedim)
# Calculate difference
diff = arr - reloadedarr
print("Number of different pixels: {}".format(sum(diff.ravel())))
它打印 "Number of different pixels: 0".