我已经创建了一个数组:

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

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


当前回答

使用pygame,您可以打开一个窗口,以像素数组的形式获取曲面,并从那里任意操作。不过,您需要将numpy数组复制到surface数组中,这比在pygame表面上进行实际的图形操作要慢得多。

其他回答

您可以使用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()

例如,你的图像在一个名为image的数组中

你要做的就是

plt.imshow(image)
plt.show

这将以图像的形式显示一个数组 另外,不要忘记导入PLT

补充使用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))

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

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