在python中将数组显示为光栅图像 [英] Display array as raster image in python

查看:1083
本文介绍了在python中将数组显示为光栅图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个numpy数组,我想在屏幕上显示为光栅图像。最简单的方法是什么?它不需要特别花哨或有一个很好的界面,我需要做的就是将数组的内容显示为灰度光栅图像。

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.

I我试图用NumPy将我的一些IDL代码转换为Python,并且基本上是在寻找 tv tvscl IDL中的命令。

I'm trying to transition some of my IDL code to Python with NumPy and am basically looking for a replacement for the tv and tvscl commands in IDL.

推荐答案

根据您的需要, matplotlib的 imshow glumpy 可能是最好的选择。

Depending on your needs, either matplotlib's imshow or glumpy are probably the best options.

Matplotlib无限灵活,但速度较慢(即使你做的一切都正确,matplotlib中的动画也可能是资源密集型的。)但是,您将拥有一个非常精彩,功能齐全的绘图库。

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.

Glumpy非常适合基于OpenGL的快速显示和2D动画numpy数组,但它的作用更加有限。如果你需要实时制作一系列图像或显示数据,那么它比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.

使用matplotlib(使用pyplot API而不是pylab ):

Using matplotlib (using the pyplot API instead of 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()

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

这篇关于在python中将数组显示为光栅图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆