matplotlib.pyplot.imshow() 显示空白 canvas
matplotlib.pyplot.imshow() shows blank canvas
我遇到了一个奇怪的问题,到目前为止互联网无法解决。如果我读入一个 .png 文件,然后尝试显示它,它会完美运行(在下面的示例中,文件是一个蓝色像素)。但是,如果我尝试手动创建此图像数组,它只会显示空白 canvas。有什么想法吗?
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
im = Image.open('dot.png') # A single blue pixel
im1 = np.asarray(im)
print im1
# [[[ 0 162 232 255]]]
plt.imshow(im1, interpolation='nearest')
plt.show() # Works fine
npArray = np.array([[[0, 162, 232, 255]]])
plt.imshow(npArray, interpolation='nearest')
plt.show() # Blank canvas
npArray = np.array([np.array([np.array([0, 162, 232, 255])])])
plt.imshow(npArray, interpolation='nearest')
plt.show() # Blank canvas
P.S。我也尝试用 np.asarray() 替换所有 np.array(),但结果是一样的。
X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)
Display the image in `X` to current axes. `X` may be a float
array, a uint8 array or a PIL image.
所以X
可能是一个数据类型为uint8
的数组。
当你没有指定数据类型时,
In [63]: np.array([[[0, 162, 232, 255]]]).dtype
Out[63]: dtype('int64')
NumPy 可能会默认创建 dtype int64
或 int32
的数组(不是 uint8
)。
如果您明确指定 dtype='uint8'
,则
import matplotlib.pyplot as plt
import numpy as np
npArray = np.array([[[0, 162, 232, 255]]], dtype='uint8')
plt.imshow(npArray, interpolation='nearest')
plt.show()
产量
PS。如果你勾选
im = Image.open('dot.png') # A single blue pixel
im1 = np.asarray(im)
print(im1.dtype)
你会发现 im1.dtype
也是 uint8
。
我遇到了一个奇怪的问题,到目前为止互联网无法解决。如果我读入一个 .png 文件,然后尝试显示它,它会完美运行(在下面的示例中,文件是一个蓝色像素)。但是,如果我尝试手动创建此图像数组,它只会显示空白 canvas。有什么想法吗?
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
im = Image.open('dot.png') # A single blue pixel
im1 = np.asarray(im)
print im1
# [[[ 0 162 232 255]]]
plt.imshow(im1, interpolation='nearest')
plt.show() # Works fine
npArray = np.array([[[0, 162, 232, 255]]])
plt.imshow(npArray, interpolation='nearest')
plt.show() # Blank canvas
npArray = np.array([np.array([np.array([0, 162, 232, 255])])])
plt.imshow(npArray, interpolation='nearest')
plt.show() # Blank canvas
P.S。我也尝试用 np.asarray() 替换所有 np.array(),但结果是一样的。
X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)
Display the image in `X` to current axes. `X` may be a float
array, a uint8 array or a PIL image.
所以X
可能是一个数据类型为uint8
的数组。
当你没有指定数据类型时,
In [63]: np.array([[[0, 162, 232, 255]]]).dtype
Out[63]: dtype('int64')
NumPy 可能会默认创建 dtype int64
或 int32
的数组(不是 uint8
)。
如果您明确指定 dtype='uint8'
,则
import matplotlib.pyplot as plt
import numpy as np
npArray = np.array([[[0, 162, 232, 255]]], dtype='uint8')
plt.imshow(npArray, interpolation='nearest')
plt.show()
产量
PS。如果你勾选
im = Image.open('dot.png') # A single blue pixel
im1 = np.asarray(im)
print(im1.dtype)
你会发现 im1.dtype
也是 uint8
。