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

9 views
Skip to first unread message

Andy Fraser

unread,
Dec 15, 2009, 2:48:19 PM12/15/09
to scipy...@scipy.org
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()
_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

Andy Fraser

unread,
Dec 18, 2009, 12:57:58 PM12/18/09
to SciPy Users List
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 <afr...@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

Christopher Barker

unread,
Dec 18, 2009, 3:05:46 PM12/18/09
to SciPy Users List
Andy Fraser wrote:
> 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

yup -- I actually poked around a little looking for an answer for you,
but docs are very sparse!

> The code also runs if the first argument to
> fl_draw_image is bits rather than bits.data.

I figured -- numpy arrays expose the buffer interface, so you should be
able to pass one into anything expecting a buffer.

Glad you got it figured out.

-Chris


--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Reply all
Reply to author
Forward
0 new messages