我已经创建了一个数组:

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

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


当前回答

这可能是一个可能的代码解决方案:

from skimage import io
import numpy as np
data=np.random.randn(5,2)
io.imshow(data)

其他回答

您可以使用PIL创建(并显示)一个图像:

from PIL import Image
import numpy as np

w, h = 512, 512
data = np.zeros((h, w, 3), dtype=np.uint8)
data[0:256, 0:256] = [255, 0, 0] # red patch in upper left
img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.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 

请参见示例。

这可能是一个可能的代码解决方案:

from skimage import io
import numpy as np
data=np.random.randn(5,2)
io.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