1 | """Plotting routine for use with the mandelbrot set |
---|
2 | |
---|
3 | Ole Nielsen, SUT 2003 |
---|
4 | """ |
---|
5 | |
---|
6 | |
---|
7 | def plot(A, kmax = None): |
---|
8 | """Plot matrix A as an RGB image using the Python Imaging Library and Tkinter |
---|
9 | |
---|
10 | A is converted to an RGB image using PIL and saved to disk |
---|
11 | Then it is displayed using PhotoImage |
---|
12 | |
---|
13 | A must be square, integer valued, two dimensional and non-negative |
---|
14 | |
---|
15 | If kmax is omitted it will be set to the max value of A |
---|
16 | |
---|
17 | Ole Nielsen, SUT 2003 |
---|
18 | """ |
---|
19 | |
---|
20 | #from Tkinter import Frame, Canvas, TOP, NW, PhotoImage |
---|
21 | from pylab import imshow, show |
---|
22 | from Numeric import transpose, Float |
---|
23 | #from Image import new # PIL |
---|
24 | import time |
---|
25 | |
---|
26 | t0 = time.time() |
---|
27 | |
---|
28 | # User definable parameters |
---|
29 | |
---|
30 | #imtype = 'ppm' # Image format (e.g 'ppm', 'bmp', 'tiff') |
---|
31 | #filename ='mandel' # Base filename |
---|
32 | |
---|
33 | exponent = 0.998 # Exponent for morphing colors, good with kmax = 2**15 |
---|
34 | rgbmax = 2**24 # Normalisation constant for RGB |
---|
35 | |
---|
36 | |
---|
37 | # Input check |
---|
38 | assert len(A.shape) == 2, 'Matrix must be 2 dimensional' |
---|
39 | assert A.shape[0] == A.shape[1], 'Matrix must be square' |
---|
40 | assert A.typecode() in 'ilu1swb', 'A must contain integers' |
---|
41 | assert min(min(A))>=0, 'A must be non-negative' |
---|
42 | |
---|
43 | if kmax is None: |
---|
44 | kmax = max(max(A)) |
---|
45 | |
---|
46 | # Convert values from A into RGB values (0 to 255) in each band |
---|
47 | N = A.shape[0] |
---|
48 | A = transpose(A).astype(Float) |
---|
49 | |
---|
50 | #im = new("RGB", A.shape) |
---|
51 | |
---|
52 | |
---|
53 | L = [] |
---|
54 | try: |
---|
55 | from mandelplot_ext import normalise_and_convert |
---|
56 | normalise_and_convert(A, L, kmax, rgbmax, exponent) |
---|
57 | |
---|
58 | except: |
---|
59 | print 'WARNING: Could not import C extension from mandelplot_ext' |
---|
60 | |
---|
61 | |
---|
62 | for i in range(A.shape[0]): |
---|
63 | for j in range(A.shape[1]): |
---|
64 | |
---|
65 | c = A[i,j]/kmax |
---|
66 | |
---|
67 | if c == 1: c = 0 #Map convergent point (kmax) to black (0) |
---|
68 | c = c**exponent #Morph slightly |
---|
69 | |
---|
70 | c = int(c * rgbmax) #Normalise to 256 levels per channel |
---|
71 | |
---|
72 | red = c / 256 / 256 |
---|
73 | green = (c / 256) % 256 |
---|
74 | blue = c % 256 |
---|
75 | |
---|
76 | L.append( (red, green, blue) ) |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | # Save image to file |
---|
81 | imshow(A) |
---|
82 | show() |
---|
83 | |
---|
84 | #im.save(filename + '.' + imtype, imtype) |
---|
85 | print 'Computed plot in %.2f seconds: ' %(time.time()-t0) |
---|
86 | |
---|
87 | # Display image on screen |
---|
88 | #answer = raw_input('Show image [Y/N][Y]?') |
---|
89 | #if answer.lower() in ['n', 'no']: |
---|
90 | # import sys |
---|
91 | # sys.exit() |
---|
92 | |
---|
93 | #import os |
---|
94 | #os.system('xv %s' %(filename + '.' + imtype)) |
---|
95 | |
---|
96 | |
---|