Howto display an array as an image (like imagesc in matlab)

M

Mathias

Dear NG,

I currently ty to switch from matlab to python/scipy but have a lot of
trouble with images. What I need is a function for subsequently
displaying a number of 2D-matrices as an image.
I tried:
- Image.show() which is nice but slow and I don't seem to be
able to either close the window nor draw again to the same
window, so I end up with 100 pictures on the screen...
- scipy.xplt.imagesc() and scipy.plt.imagesc() crash
consistently even with fresh installs of wxPython and scipy and
python2.3 on Linux (RH 9) as well as on Win XP.
- Use Tkinter to display the matrix (but then I need to store
the matrix as a gif file first... don't I?)

Are the scipy image/imagesc routines really so increadibly unstable? Are
there alternatives?

Thanks a lot,
Mathias
 
C

Christopher T King

- Image.show() which is nice but slow and I don't seem to be
able to either close the window nor draw again to the same
window, so I end up with 100 pictures on the screen...

I'm not too familiar with SciPy, but is this Image class the PIL Image
class? Because if that's the case, PIL images can be trivially integrated
with Tkinter by using PIL's ImageTk module:

from Tkinter import Label
import ImageTk

mylabel = Label()
mylabel.pack()

while True:
image = process_and_return_image_data_with_scipy()
label['image'] = ImageTk.PhotoImage(image)

I'm not sure how fast this is though (whether ImageTk.PhotoImage() creates
a copy of the original image, etc.).
 
J

John Hunter

Mathias> Dear NG, I currently ty to switch from matlab to
Mathias> python/scipy but have a lot of trouble with images. What

matplotlib (by yours truly) is designed as a matlab compatible
plotting library for python. It has 'imshow' to display images. You
can manage several figures, axes, and images as in matlab with the
matlab compatible commands figure / subplot / axes / gcf / gca / close
/ clf / cla / hold.

http://matplotlib.sf.net

Here is a simple script to create and display an image

from matplotlib.matlab import *

delta = 0.025
x = y = arange(-3.0, 3.0, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)

# difference of Gaussians
im = imshow(Z2-Z1, interpolation='bilinear')
axis('off')
show()

Additional matlab compatible functions for color mapping are provided
(colorbar, jet, gray) and a variety of interpolation methods are
available (nearest neighbor, bilinear, bicubic, much more).

In addition, it supports features I don't believe matlab has, like the
ability to automatically blend multiple images with different alpha
values while 'hold' is on - see for example
http://matplotlib.sourceforge.net/screenshots.html#layer_images

Outputs to png, ps, svg (image support for svg only in CVS). Also in
CVS is imread to load images into arrays.

If you are very familiar with matlab, matplotlib will be a breeze -
there is a brief tutorial at
http://matplotlib.sourceforge.net/tutorial.html

On win32, if you are using the enthought edition of python
(recommended!), you can use matplotlib out of the box with either the
tkinter or wxpython backends.

JDH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top