source: misc/tools/acceptance_tests/mandelbrot/mandelplot.py @ 7276

Last change on this file since 7276 was 7276, checked in by ole, 15 years ago

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

  • Property svn:executable set to *
File size: 2.7 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 numpy import transpose
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    msg = 'A must contain integers, I got %c' %A.dtype.char
40    assert A.dtype.char in 'iIbBhHl', msg
41    assert min(A.flat)>=0, 'A must be non-negative'
42
43    if kmax is None:
44        kmax = max(A.flat)
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('d') # Cast as double         
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    im.putdata(L)
82    im.save(filename + '.' + imtype, imtype)
83    print 'Computed plot in %.2f seconds: ' %(time.time()-t0)
84
85    # Display image on screen
86    #answer = raw_input('Show image [Y/N][Y]?')
87    #if answer.lower() in ['n', 'no']:
88    #   import sys
89    #   sys.exit()
90
91    # Try to display using a image viewer
92    import os
93    os.system('eog %s' %(filename + '.' + imtype))
94    #os.system('xv %s' %(filename + '.' + imtype))   
95
96
97
98 
Note: See TracBrowser for help on using the repository browser.