我已经创建了一个数组:
import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]
我想要做的是在512x512图像的中心显示一个红点。(至少开始时……我想我可以从那里弄清楚剩下的)
我已经创建了一个数组:
import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]
我想要做的是在512x512图像的中心显示一个红点。(至少开始时……我想我可以从那里弄清楚剩下的)
当前回答
以下方法应该有效:
from matplotlib import pyplot as plt
plt.imshow(data, interpolation='nearest')
plt.show()
如果你正在使用Jupyter notebook/lab,在导入matplotlib之前使用这个内联命令:
%matplotlib inline
一个更有特色的方法是安装ipyml pip安装ipympl并使用
%matplotlib widget
请参见示例。
其他回答
如何显示存储在numpy数组中的图像与示例(工作在Jupyter笔记本)
我知道有更简单的答案,但这个答案会让你理解如何从numpy数组中绘制图像。
负载的例子
from sklearn.datasets import load_digits
digits = load_digits()
digits.images.shape #this will give you (1797, 8, 8). 1797 images, each 8 x 8 in size
显示一个图像的数组
digits.images[0]
array([[ 0., 0., 5., 13., 9., 1., 0., 0.],
[ 0., 0., 13., 15., 10., 15., 5., 0.],
[ 0., 3., 15., 2., 0., 11., 8., 0.],
[ 0., 4., 12., 0., 0., 8., 8., 0.],
[ 0., 5., 8., 0., 0., 9., 8., 0.],
[ 0., 4., 11., 0., 1., 12., 7., 0.],
[ 0., 2., 14., 5., 10., 12., 0., 0.],
[ 0., 0., 6., 13., 10., 0., 0., 0.]])
为可视化100张图像创建空的10 × 10子图
import matplotlib.pyplot as plt
fig, axes = plt.subplots(10,10, figsize=(8,8))
绘制100幅图像
for i,ax in enumerate(axes.flat):
ax.imshow(digits.images[i])
结果:
什么是坐标轴?平做什么? 它创建了一个numpy枚举器,以便您可以遍历轴,以便在其上绘制对象。 例子:
import numpy as np
x = np.arange(6).reshape(2,3)
x.flat
for item in (x.flat):
print (item, end=' ')
使用pygame,您可以打开一个窗口,以像素数组的形式获取曲面,并从那里任意操作。不过,您需要将numpy数组复制到surface数组中,这比在pygame表面上进行实际的图形操作要慢得多。
这可能是一个可能的代码解决方案:
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))
使用枕头的fromarray,例如:
from PIL import Image
from numpy import *
im = array(Image.open('image.jpg'))
Image.fromarray(im).show()