[Image-SIG] PIL and wxPython?

Janko Hauser jhauser@ifm.uni-kiel.de
Tue, 1 Aug 2000 18:21:51 +0200 (CEST)


Yes it is quite easy if you use the wxpython backend of piddle, which
is probably quite useful for imagegeneration in wxpython.

Here is the baseclass:

class SWXCanvasImage(PiddleWXCanvas):
    def __init__(self, parent, data):
        PiddleWXCanvas.__init__(self, parent)
        self.dataimage = swxm.ArrayToImage(data)

    def DoDrawing(self,dc):
        pdc = PiddleWxDc(dc)
        pdc.drawImage(self.dataimage,0,0)

and swxm.ArrayToImage(data) is the following function:

# file swxm.py
#
import Numeric
import Image

def normalize(a):
    a = Numeric.array(a, copy=1)
    a = (a -
    Numeric.minimum.reduce(a.flat))/(Numeric.maximum.reduce(a.flat) \
                                              -
                                                Numeric.minimum.reduce(a.flat)) 
    return ( a*254 ).astype('b')

def ArrayToImage(a, height=200, scale=1):
    a = normalize(a)
    i = Image.new("L", (a.shape[1], a.shape[0]), color=255)
    i.fromstring(a.tostring())
    if scale != 1:
    i = i.resize((i.size[0]*scale, i.size[1]*scale))
    return i

def ImageToArray(i):
    a = fromstring(i.tostring(), 'b')
    a.shape = i.im.size[1], i.im.size[0]
    return a


HTH,

__Janko

-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser@ifm.uni-kiel.de
  24105 Kiel, Germany