source: inundation/pypar_dist/mandelbrot_example/mandelplot.py @ 3444

Last change on this file since 3444 was 1852, checked in by ole, 19 years ago

Added prerequisites for mandelbrot_example

File size: 2.4 KB
Line 
1"""Plotting routine for use with the mandelbrot set
2
3   Ole Nielsen, SUT 2003
4"""
5
6
7def 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 Numeric import transpose, Float
22    from Image import new             # PIL   
23    import time
24
25    t0 = time.time()
26
27    # User definable parameters
28
29    imtype = 'ppm'      # Image format (e.g 'ppm', 'bmp', 'tiff')
30    filename ='mandel'  # Base filename
31   
32    exponent = 0.998    # Exponent for morphing colors, good with kmax = 2**15
33    rgbmax = 2**24      # Normalisation constant for RGB
34   
35
36    # Input check
37    assert len(A.shape) == 2, 'Matrix must be 2 dimensional'
38    assert A.shape[0] == A.shape[1], 'Matrix must be square'
39    assert A.typecode() in 'ilu1swb', 'A must contain integers'
40    assert min(min(A))>=0, 'A must be non-negative'
41
42    if kmax is None:
43        kmax = max(max(A))
44
45    # Convert values from A into RGB values (0 to 255) in each band
46    N = A.shape[0]
47    A = transpose(A).astype(Float)         
48
49    im = new("RGB", A.shape)
50
51   
52    L = []       
53    try:
54        from mandelplot_ext import normalise_and_convert
55        normalise_and_convert(A, L, kmax, rgbmax, exponent)
56       
57    except:
58        print 'WARNING: Could not import C extension from mandelplot_ext'
59               
60   
61        for i in range(A.shape[0]):
62            for j in range(A.shape[1]):   
63               
64                c = A[i,j]/kmax
65
66                if c == 1: c = 0       #Map convergent point (kmax) to black (0)
67                c = c**exponent        #Morph slightly
68               
69                c = int(c * rgbmax)    #Normalise to 256 levels per channel
70               
71                red   = c / 256 / 256
72                green = (c / 256) % 256
73                blue  = c % 256
74   
75                L.append( (red, green, blue) )
76           
77           
78   
79    # Save image to file       
80    im.putdata(L)
81    im.save(filename + '.' + imtype, imtype)
82    print 'Computed plot in %.2f seconds: ' %(time.time()-t0)
83
84    # Display image on screen
85    #answer = raw_input('Show image [Y/N][Y]?')
86    #if answer.lower() in ['n', 'no']:
87    #   import sys
88    #   sys.exit()
89       
90    import os
91    os.system('xv %s' %(filename + '.' + imtype))
92
93
Note: See TracBrowser for help on using the repository browser.