Problem displaying Numeric Array as image

Eric Frey frey at weasel.bme.unc.edu
Thu Jun 15 08:14:50 EDT 2000


I a trying to display a floating point Numeric (NumPy) array as a
grayscale image via Tkinter.  The way I am trying to do it is via a
PhotoImage created via PIL. The problem is that the image I get looks like
it is a binary (black or white) image. Here is sample code

from Numeric import *
from Tkinter import *
import Image, ImageTk
#...
# im is a floating point array computed using numeric
bits=im.tostring()
pilim=Image.fromstring('F',(width, height), bits, "raw", 'F')
print pilim.format, pilim.size, pilim.mode

root=Tk()
pilphoto=ImageTk.PhotoImage(pilim)
l=Label(root,image=pilphoto,bd=0)
l.pack()
b=Button(root,text="Quit",command=root.quit)
b.pack()
root.mainloop()
---
I have tried lots of things including using astype to convert the im
to various other types (Int16, Int8, Int32) (and modifying the mode
strings in the Image.fromstring call. I have also tried setting the
maximum pixel value to various values using
im = (im/(max(im.flat))*NNN
where NNN is the value I want the maximum to be mapped to. This which
pixels are displayed as if there is some kind of thresholding going on.
What I really want is a way to display floating point pixels as a
grayscale/pseudocolor image in some kind of gui. Is Tkinter+PIL the way
to go? Would I be better of with PyGtk or some other toolkit? Thanks
for any advice.



=======================================================================
Eric C. Frey  
Assistant Professor
Department of Biomedical Engineering and Department of Radiology
The University of North Carolina at Chapel HIll



More information about the Python-list mailing list