Python 无法撤消numpy数组中保留的缩放图像
我将图像作为像素数据加载到numpy数组(subjectImage)中。以下代码行成功地将numpy阵列还原回图像并显示它:Python 无法撤消numpy数组中保留的缩放图像,python,image,numpy,scipy,python-imaging-library,Python,Image,Numpy,Scipy,Python Imaging Library,我将图像作为像素数据加载到numpy数组(subjectImage)中。以下代码行成功地将numpy阵列还原回图像并显示它: subjectImagePath = 'pathToFile/cat.0.jpg' subjectImage = misc.imresize(misc.imread(subjectImagePath), (224,224,3)) img = Image.fromarray(subjectImage, 'RGB') img.show() 但是,如果将图像的
subjectImagePath = 'pathToFile/cat.0.jpg'
subjectImage = misc.imresize(misc.imread(subjectImagePath), (224,224,3))
img = Image.fromarray(subjectImage, 'RGB')
img.show()
但是,如果将图像的像素值缩放到0和1之间,则无法将图像恢复到其原始形式。(它显示一组噪音)
Numpy甚至告诉我数组是一样的
orig = subjectImage
subjectImage = subjectImage/255
print(np.array_equal(orig, subjectImage*255)) # => Prints True
我想知道这可能是什么原因?任何帮助都会很好
使用的图书馆:
import numpy as np
from PIL import Image
from scipy import misc
浮点表示和数据类型的有趣示例。。。检查以下示例。您可以打印数组以查看不等式存在的位置。以下内容简化了结果和比较
>>> a = np.arange(5*5*3, dtype=np.int64)
>>> b = a/(5*5)
>>> c = b*(5*5)
>>> d = np.around(b*(5*5))
>>> a[a!=c]
array([ 7, 14, 28, 29, 55, 56, 57, 58])
>>> a[a!=d]
array([], dtype=int64)
问题在于,乘255除255后的数组将成为浮点数组:
>>> a = misc.imread(path)
>>> a.dtype
dtype('uint8')
>>> b = a / 255
>>> b = b * 255
>>> b.dtype
dtype('float64')
我猜img.show()
函数不知道如何显示浮点数。它可能会将浮动解释为uint8或类似的内容,并试图以某种方式显示它们。不幸的是,forimg.show()
没有告诉我们它是如何工作的
但是,Scipy的misc
模块有自己的功能,可以正常工作:
>>> misc.imshow(b)
另一方面,如果您考虑同时使用
scipy.misc
和PIL
/枕头
,它们处理数组的方式似乎有些不同。例如,参见问题。在计算的各个阶段检查dtype
。我认为这不是问题所在。OP的数组检查是否相等。。。show()
@Praveen更可能是出现了一些问题,这就是测试显示存在的可能导致模糊性的不等式,可能show()确保了正确的转换返回..看起来高阶位关闭了。如果我将numpy数组强制转换为unsigned int,则可以解决问题。谢谢
>>> misc.imshow(b)