Statistics
| Revision:

root / PyOpenGL-Demo / dek / MandelImage.py @ 1

History | View | Annotate | Download (1.9 kB)

1 1 equemene
#!/usr/bin/python
2 1 equemene
3 1 equemene
# This is statement is required by the build system to query build info
4 1 equemene
if __name__ == '__build__':
5 1 equemene
        raise Exception
6 1 equemene
7 1 equemene
## This isn't really a PyOpenGL demo, but it's a nice
8 1 equemene
## example of how Numeric, Tkinter, and PIL can be used
9 1 equemene
## together to create all sorts of images.
10 1 equemene
## In this case, it's the Mandelbrot set.
11 1 equemene
## i used the Numerical python text example, but modified it to
12 1 equemene
## work with PIL
13 1 equemene
14 1 equemene
try:
15 1 equemene
        import numpy as Numeric
16 1 equemene
except ImportError, err:
17 1 equemene
        try:
18 1 equemene
                import Numeric
19 1 equemene
        except ImportError, err:
20 1 equemene
                print "This demo requires the numpy or Numeric extension, sorry"
21 1 equemene
                import sys
22 1 equemene
                sys.exit()
23 1 equemene
import FFT
24 1 equemene
import Tkinter
25 1 equemene
import Image
26 1 equemene
import ImageTk
27 1 equemene
import sys
28 1 equemene
29 1 equemene
w = 256
30 1 equemene
h = 256
31 1 equemene
32 1 equemene
class Test:
33 1 equemene
        def draw(self,LowX, HighX, LowY, HighY, maxiter=30):
34 1 equemene
                xx=Numeric.arange(LowX,HighX,(HighX-LowX)/w*2)
35 1 equemene
                yy=Numeric.arange(HighY,LowY,(LowY-HighY)/h*2)*1j
36 1 equemene
                c=Numeric.ravel(xx+yy[:,Numeric.NewAxis])
37 1 equemene
                z=Numeric.zeros(c.shape,Numeric.Complex)
38 1 equemene
                output=Numeric.resize(Numeric.array(0,),c.shape)
39 1 equemene
40 1 equemene
                for iter in range(maxiter):
41 1 equemene
                        print "iter",iter
42 1 equemene
                        z=z*z+c
43 1 equemene
                        finished=Numeric.greater(abs(z),2.0)
44 1 equemene
                        c=Numeric.where(finished,0+0j,c)
45 1 equemene
                        z=Numeric.where(finished,0+0j,z)
46 1 equemene
                        output=Numeric.where(finished,iter,output)
47 1 equemene
48 1 equemene
                ## scale output a bit to make it brighter
49 1 equemene
##      output * output * 1000
50 1 equemene
                output = (output + (256*output) + (256**2)*output)*8
51 1 equemene
                self.mandel = output.tostring()#"raw", "RGBX", 0, -1)
52 1 equemene
                print len(self.mandel)
53 1 equemene
54 1 equemene
        def createImage(self):
55 1 equemene
                self.im = Image.new("RGB", (w/2,h/2))
56 1 equemene
                self.draw(-2.1, 0.7, -1.2, 1.2)
57 1 equemene
                print len(self.im.tostring("raw", "RGBX", 0, -1))
58 1 equemene
                self.im.fromstring(self.mandel, "raw", "RGBX", 0, -1)
59 1 equemene
60 1 equemene
        def createLabel(self):
61 1 equemene
                self.image = ImageTk.PhotoImage(self.im)
62 1 equemene
                self.label = Tkinter.Label(self.root, image=self.image)
63 1 equemene
                self.label.pack()
64 1 equemene
65 1 equemene
66 1 equemene
        def __init__(self):
67 1 equemene
                self.root = Tkinter.Tk()
68 1 equemene
                self.i = 0
69 1 equemene
                self.createImage()
70 1 equemene
                self.createLabel()
71 1 equemene
                self.root.mainloop()
72 1 equemene
73 1 equemene
demo = Test
74 1 equemene
75 1 equemene
if __name__ == '__main__':
76 1 equemene
        demo()