[SciPy-User] How to draw an image in fltk from numpy array?

Andy Fraser afraser at lanl.gov
Fri Dec 18 12:57:58 EST 2009


I've done some more looking around and found answers to my own
questions.  The key was to fetch the source for pyfltk from
http://pyfltk.sourceforge.net/index.php#download and look at the
examples (in particular draw.py) in pyFltk-1.1.4/fltk/test.

I don't understand why, but after removing the line:

canvas.draw()

from my code, it ran.  The code also runs if the first argument to
fl_draw_image is bits rather than bits.data.

I include some of my question for context.

>>>>> "AF" == Andy Fraser <afraser at lanl.gov> writes:

    AF> [...] I want to write a GUI in fltk to look at intermediate
    AF> results of image manipulations, [...] I get segmentation
    AF> faults.

    AF> [...]  code [...] below.

    AF> #file GUI.py
    AF> import sys, fltk, numpy
    AF> W = 180
    AF> H = 90
    AF> D = 3
    AF> X = 5
    AF> Y = 200
    AF> bits = numpy.empty( (W,H,3), numpy.uint8)
    AF> # class derived from /usr/share/pyshared/matplotlib/backends/backend_fltkagg.py
    AF> 
    AF> class FltkCanvas(fltk.Fl_Widget):
    AF> 
    AF>     def __init__(self,x,y,w,h):
    AF>         fltk.Fl_Widget.__init__(self, 0, 0, w, h, "canvas")
    AF>         self._oldsize=(None,None)
    AF>         self._draw_overlay = False
    AF>         self._button = None
    AF>         self._key = None
    AF> 
    AF> 
    AF>     def draw(self):
    AF>         newsize=(self.w(),self.h())
    AF>         if(self._oldsize !=newsize):
    AF>             self._oldsize =newsize
    AF> 	print 'before fltk.fl_draw_image'
    AF>         fltk.fl_draw_image(bits.data,0,0,int(W),int(H),3,0)
    AF> 	print 'did fltk.fl_draw_image'
    AF>         self.redraw()
    AF> 
    AF> def theCancelButtonCallback(ptr):
    AF> 	sys.exit(0)
    AF> 
    AF> window = fltk.Fl_Window(100,100,200,400)
    AF> canvas =  FltkCanvas(X,Y,W,H)
    AF> canvas.draw()
    AF> window.end()
    AF> window.show(len(sys.argv), sys.argv)
    AF> fltk.Fl.run()

Andy



More information about the SciPy-User mailing list