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