在python中将数组显示为光栅图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display array as raster image in python
提问by robintw
I've got a numpy array in Python and I'd like to display it on-screen as a raster image. What is the simplest way to do this? It doesn't need to be particularly fancy or have a nice interface, all I need to do is to display the contents of the array as a greyscale raster image.
我在 Python 中有一个 numpy 数组,我想将它作为光栅图像显示在屏幕上。什么是最简单的方法来做到这一点?它不需要特别花哨或有一个漂亮的界面,我需要做的就是将数组的内容显示为灰度光栅图像。
I'm trying to transition some of my IDL code to Python with NumPy and am basically looking for a replacement for the tvand tvsclcommands in IDL.
我正在尝试使用 NumPy 将我的一些 IDL 代码转换为 Python,并且基本上是在寻找IDL 中tv和tvscl命令的替代品。
采纳答案by Joe Kington
Depending on your needs, either matplotlib's imshowor glumpyare probably the best options.
根据您的需要,matplotlibimshow或glumpy可能是最佳选择。
Matplotlib is infinitely more flexible, but slower (animations in matplotlib can be suprisingly resource intensive even when you do everything right.). However, you'll have a really wonderful, full-featured plotting library at your disposal.
Matplotlib 更加灵活,但速度较慢(即使您做对了,matplotlib 中的动画也可能会占用大量资源。)。但是,您将拥有一个非常棒的、功能齐全的绘图库供您使用。
Glumpy is perfectly suited for the quick, openGL based display and animation of a 2D numpy array, but is much more limited in what it does. If you need to animate a series of images or display data in realtime, it's a far better option than matplotlib, though.
Glumpy 非常适合快速、基于 openGL 的 2D numpy 数组显示和动画,但它的功能有限。但是,如果您需要为一系列图像设置动画或实时显示数据,那么它比 matplotlib 好得多。
Using matplotlib (using the pyplot API instead of pylab):
使用 matplotlib(使用 pyplot API 而不是 pylab):
import matplotlib.pyplot as plt
import numpy as np
# Generate some data...
x, y = np.meshgrid(np.linspace(-2,2,200), np.linspace(-2,2,200))
x, y = x - x.mean(), y - y.mean()
z = x * np.exp(-x**2 - y**2)
# Plot the grid
plt.imshow(z)
plt.gray()
plt.show()
Using glumpy:
使用笨拙:
import glumpy
import numpy as np
# Generate some data...
x, y = np.meshgrid(np.linspace(-2,2,200), np.linspace(-2,2,200))
x, y = x - x.mean(), y - y.mean()
z = x * np.exp(-x**2 - y**2)
window = glumpy.Window(512, 512)
im = glumpy.Image(z.astype(np.float32), cmap=glumpy.colormap.Grey)
@window.event
def on_draw():
im.blit(0, 0, window.width, window.height)
window.mainloop()
回答by Thomas
Using ipython in the pylab interactive mode, you could do:
在 pylab 交互模式下使用 ipython,您可以执行以下操作:
$ ipython pylab
In [1]: imshow(your_array)
or not in pylab mode:
或不在 pylab 模式下:
$ ipython
In [1]: from pylab import *
In [2]: imshow(your_array)
In [3]: pylab.show()
or without the pylab namespace thing:
或者没有 pylab 命名空间的东西:
$ ipython
In [1]: import matplotlib.pyplot as pyplot
In [2]: pyplot.imshow(your_array)
In [3]: pyplot.show()
回答by mdurant
Quick addition: for displaying with matplotlib, if you want the image to appear "raster", i.e., pixelized without smoothing, then you should include the option interpolation='nearest' in the call to imshow.
快速添加:对于使用 matplotlib 显示,如果您希望图像显示为“光栅”,即像素化而不平滑,那么您应该在对 imshow 的调用中包含选项插值 =“最近”。

