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

Andy Fraser afraser at lanl.gov
Tue Dec 15 14:48:19 EST 2009


Users,

I have used scipy for a few years.  Now I want to write a GUI in fltk
to look at intermediate results of image manipulations, and I'm having
trouble getting started.  I get segmentation faults.

I started with the "hello world" example at
http://pyfltk.sourceforge.net/examples.php

As a next step, I tried to display a small random image.  Following
the code for the fltk backend in matplotlib, I wrote the code I'll
include below.  When I run the code, it produces:

    ->python GUI.py 
    before fltk.fl_draw_image
    Segmentation fault

I suspect that argument "bits.data" is an inappropriate argument of
fltk.fl_draw_image.  Does anyone have a different idea, or advice on
how to construct a correct argument?

Andy

#file GUI.py
import sys, fltk, numpy
W = 180
H = 90
D = 3
X = 5
Y = 200
bits = numpy.empty( (W,H,3), numpy.uint8)
# class derived from /usr/share/pyshared/matplotlib/backends/backend_fltkagg.py

class FltkCanvas(fltk.Fl_Widget):

    def __init__(self,x,y,w,h):
        fltk.Fl_Widget.__init__(self, 0, 0, w, h, "canvas")
        self._oldsize=(None,None)
        self._draw_overlay = False
        self._button = None
        self._key = None


    def draw(self):
        newsize=(self.w(),self.h())
        if(self._oldsize !=newsize):
            self._oldsize =newsize
	print 'before fltk.fl_draw_image'
        fltk.fl_draw_image(bits.data,0,0,int(W),int(H),3,0)
	print 'did fltk.fl_draw_image'
        self.redraw()

def theCancelButtonCallback(ptr):
	sys.exit(0)

window = fltk.Fl_Window(100,100,200,400)
canvas =  FltkCanvas(X,Y,W,H)
canvas.draw()
window.end()
window.show(len(sys.argv), sys.argv)
fltk.Fl.run()



More information about the SciPy-User mailing list