如何将 numpy 数组转换为(并显示)图像?

发布于 2024-08-29 10:03:11 字数 199 浏览 10 评论 0原文

我因此创建了一个数组:

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

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

I have created an array thusly:

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

What I want this to do is display a single red dot in the center of a 512x512 image. (At least to begin with... I think I can figure out the rest from there)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

心头的小情儿 2024-09-05 10:03:11

使用 plt.imshow 创建图形,并使用 plt.show 显示它:

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

对于 Jupyter 笔记本,在导入 matplotlib 之前添加此行:

%matplotlib inline 

对于 Jupyter 中的交互式绘图 [demo],安装 ipyml pip install ipympl,然后使用:

%matplotlib widget 

Use plt.imshow to create the figure, and plt.show to display it:

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

For Jupyter notebooks, add this line before importing matplotlib:

%matplotlib inline 

For interactive plots in Jupyter [demo], install ipyml pip install ipympl, then use:

%matplotlib widget 
温柔戏命师 2024-09-05 10:03:11

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

You could use PIL to create (and display) an image:

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()
虐人心 2024-09-05 10:03:11

注意:这两个 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)

Note: both these APIs have been first deprecated, then removed.

Shortest path is to use scipy, like this:

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

This requires PIL or Pillow to be installed as well.

A similar approach also requiring PIL or Pillow but which may invoke a different viewer is:

# Note: deprecated in v1.0.0 and removed in v1.8.0
from scipy.misc import imshow
imshow(data)
合约呢 2024-09-05 10:03:11

如何通过示例显示存储在 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.]])

创建空的 10 x 10 子图以可视化 100 张图像

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])

结果:

“在此处输入图像描述"

axes.flat 的作用是什么?
它创建一个 numpy 枚举器,以便您可以迭代 axis 以便在其上绘制对象。
示例:

import numpy as np
x = np.arange(6).reshape(2,3)
x.flat
for item in (x.flat):
    print (item, end=' ')

How to show images stored in numpy array with example (works in Jupyter notebook)

I know there are simpler answers but this one will give you understanding of how images are actually drawn from a numpy array.

Load example

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

Display array of one image

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.]])

Create empty 10 x 10 subplots for visualizing 100 images

import matplotlib.pyplot as plt
fig, axes = plt.subplots(10,10, figsize=(8,8))

Plotting 100 images

for i,ax in enumerate(axes.flat):
    ax.imshow(digits.images[i])

Result:

enter image description here

What does axes.flat do?
It creates a numpy enumerator so you can iterate over axis in order to draw objects on them.
Example:

import numpy as np
x = np.arange(6).reshape(2,3)
x.flat
for item in (x.flat):
    print (item, end=' ')
土豪我们做朋友吧 2024-09-05 10:03:11

使用pillow的fromarray,例如:

from PIL import Image
from numpy import *

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

Using pillow's fromarray, for example:

from PIL import Image
from numpy import *

im = array(Image.open('image.jpg'))
Image.fromarray(im).show()
此刻的回忆 2024-09-05 10:03:11
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
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
々眼睛长脚气 2024-09-05 10:03:11

使用pygame,您可以打开一个窗口,以像素数组的形式获取表面,并进行操作你想要从那里。然而,您需要将 numpy 数组复制到表面数组中,这比在 pygame 表面本身上执行实际图形操作要慢得多。

Using pygame, you can open a window, get the surface as an array of pixels, and manipulate as you want from there. You'll need to copy your numpy array into the surface array, however, which will be much slower than doing actual graphics operations on the pygame surfaces themselves.

昔日梦未散 2024-09-05 10:03:11

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

Supplement for doing so with matplotlib. I found it handy doing computer vision tasks. Let's say you got data with 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))
王权女流氓 2024-09-05 10:03:11

例如,您的图像位于名为“image”的数组中,

您所做的就是

plt.imshow(image)
plt.show

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

For example your image is in an array names 'image'

All you do is

plt.imshow(image)
plt.show

This will display an array in the form of an image
Also, dont forget to import PLT

沉鱼一梦 2024-09-05 10:03:11

Python 图像库 可以使用 Numpy 数组显示图像。查看此页面的示例代码:

编辑:正如该页面底部的注释所说,您应该检查最新的发行说明,这使这变得更简单:

http://effbot.org/zone/pil-changes-116.htm

The Python Imaging Library can display images using Numpy arrays. Take a look at this page for sample code:

EDIT: As the note on the bottom of that page says, you should check the latest release notes which make this much simpler:

http://effbot.org/zone/pil-changes-116.htm

奈何桥上唱咆哮 2024-09-05 10:03:11

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

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

this could be a possible code solution:

from skimage import io
import numpy as np
data=np.random.randn(5,2)
io.imshow(data)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文