[6991] | 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 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 | |
---|