如何将 numpy 数组转换为(并显示)图像?
我因此创建了一个数组:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用
plt.imshow
创建图形,并使用plt.show
显示它:对于 Jupyter 笔记本,在导入 matplotlib 之前添加此行:
对于 Jupyter 中的交互式绘图 [demo],安装 ipyml
pip install ipympl
,然后使用:Use
plt.imshow
to create the figure, andplt.show
to display it:For Jupyter notebooks, add this line before importing matplotlib:
For interactive plots in Jupyter [demo], install ipyml
pip install ipympl
, then use:您可以使用 PIL 创建(并显示)图像:
You could use PIL to create (and display) an image:
注意:这两个 API 首先已被弃用,然后被删除。
最短路径是使用
scipy
,如下所示:这还需要安装 PIL 或 Pillow。
类似的方法也需要 PIL 或 Pillow,但可能会调用不同的查看器是:
Note: both these APIs have been first deprecated, then removed.
Shortest path is to use
scipy
, like this: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:
如何通过示例显示存储在 numpy 数组中的图像(适用于 Jupyter 笔记本)
我知道有更简单的答案,但这个答案将使您了解如何实际从 numpy 数组中绘制图像。
加载示例
显示一张图像的数组
创建空的 10 x 10 子图以可视化 100 张图像
绘制 100 张图像
结果:
axes.flat 的作用是什么?
它创建一个 numpy 枚举器,以便您可以迭代 axis 以便在其上绘制对象。
示例:
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
Display array of one image
Create empty 10 x 10 subplots for visualizing 100 images
Plotting 100 images
Result:
What does
axes.flat
do?It creates a numpy enumerator so you can iterate over axis in order to draw objects on them.
Example:
使用pillow的fromarray,例如:
Using pillow's fromarray, for example:
使用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.
使用 matplotlib 进行此操作的补充。我发现它很方便执行计算机视觉任务。假设您获得了 dtype = int32 的数据
Supplement for doing so with matplotlib. I found it handy doing computer vision tasks. Let's say you got data with dtype = int32
例如,您的图像位于名为“image”的数组中,
您所做的就是
这将以图像的形式显示数组
另外,不要忘记导入 PLT
For example your image is in an array names 'image'
All you do is
This will display an array in the form of an image
Also, dont forget to import PLT
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
这可能是一个可能的代码解决方案:
this could be a possible code solution: