468,537 Members | 2,109 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

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

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

Jul 18 '05 #1
2 15936
On Thu, 29 Jul 2004, Mathias wrote:
- 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.).

Jul 18 '05 #2
>>>>> "Mathias" == Mathias <no_sp@m_please.cc> writes:

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/sc...l#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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thubaiti | last post: by
6 posts views Thread by Wijaya Edward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.