我已经创建了一个数组:

import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]

我想要做的是在512x512图像的中心显示一个红点。(至少开始时……我想我可以从那里弄清楚剩下的)


当前回答

使用枕头的fromarray,例如:

from PIL import Image
from numpy import *

im = array(Image.open('image.jpg'))
Image.fromarray(im).show()

其他回答

使用枕头的fromarray,例如:

from PIL import Image
from numpy import *

im = array(Image.open('image.jpg'))
Image.fromarray(im).show()

以下方法应该有效:

from matplotlib import pyplot as plt
plt.imshow(data, interpolation='nearest')
plt.show()

如果你正在使用Jupyter notebook/lab,在导入matplotlib之前使用这个内联命令:

%matplotlib inline 

一个更有特色的方法是安装ipyml pip安装ipympl并使用

%matplotlib widget 

请参见示例。

注意:这两个api都是先弃用后删除的。

最短路径是使用scipy,像这样:

# Note: deprecated in v0.19.0 and removed in v1.3.0
from scipy.misc import toimage
toimage(data).show()

这也需要安装PIL或Pillow。

类似的方法也需要PIL或Pillow,但可能会引起不同的观众:

# Note: deprecated in v1.0.0 and removed in v1.8.0
from scipy.misc import imshow
imshow(data)

补充使用matplotlib这样做。我发现做计算机视觉任务很方便。假设有dtype = int32的数据

from matplotlib import pyplot as plot
import numpy as np

fig = plot.figure()
ax = fig.add_subplot(1, 1, 1)
# make sure your data is in H W C, otherwise you can change it by
# data = data.transpose((_, _, _))
data = np.zeros((512,512,3), dtype=np.int32)
data[256,256] = [255,0,0]
ax.imshow(data.astype(np.uint8))
import numpy as np
from keras.preprocessing.image import array_to_img
img = np.zeros([525,525,3], np.uint8)
b=array_to_img(img)
b