When plotting Mandelbrot using NumPy & Pillow, program outputs apparent noise

Question

Previously, I had created a Mandelbrot generator in python using turtle. Now, I am re-writing the program to use the Python Imaging Library in order to increase speed and reduce limits on size of images.

However, the program below only outputs RGB nonsense, almost noise. I think it is something to do with a difference in the way NumPy and PIL deal with arrays, since saying l[x,y] = [1,1,1] where l = np.zeros((height,width,3)) doesn't just make 1 pixel white when img = Image.fromarray(l) and img.show() are performed.

def imagebrot(mina=-1.25, maxa=1.25, minb=-1.25, maxb=1.25, width=100, height=100, maxit=300, inf=2):
    l,b = np.zeros((height,width,3), dtype=np.float64), minb

    for y in range(0, height):
        a = mina
        for x in range(0, width):

            ab = mandel(a, b, maxit, inf)

            if ab[0] == maxit:
                l[x,y:] = [1,1,1]

            #if ab[0] < maxit:
                #smoothit = mandelc(ab[0], ab[1], ab[2])
                #l[x, y] = colorsys.hsv_to_rgb(smoothit, 1, 1)

            a += abs(mina-maxa)/width
        b += abs(minb-maxb)/height

    img = Image.fromarray(l, "RGB")
    img.show()

def mandel(re, im, maxit, inf):
    z = complex(re, im)
    c,it = z,0

    for i in range(0, maxit):
        if abs(z) > inf:
            break
        z,it = z*z+c,it+1
    return it,z,inf

def mandelc(it,z,inf):
    return (it+1-log(log(abs(z)))/log(2))

UPDATE 1:

I realised that one of the major errors in this program (I'm sure there are many) is the fact that I was using the x,y coords as the complex coefficients! So, 0 to 100 instead of -1.25 to 1.25! I have changed this so that the code now uses variables a,b to describe them, incremented in a manner I've stolen from some of my code in the turtle version. The code above has been updated accordingly. Since the Smooth Colouring Algorithm code is currently commented out for debugging, the inf variable has been reduced to 2 in size.

UPDATE 2:

I have edited the numpy index with help from a great user. The program now outputs this when set to 200 by 200:

Failed Mandelbrot

As you can see, it definitely shows some mathematical shape and yet is filled with all these strange red, green and blue pixels! Why could these be here? My program can only set RGB values to [1,1,1] or leave it as a default [0,0,0]. It can't be [1,0,0] or anything like that - this must be a serious flaw...

UPDATE 3:

I think there is an error with NumPy and PIL's integration. If I make l = np.zeros((100, 100, 3)) and then state l[0,0,:] = 1 and finally img = Image.fromarray(l) & img.show(), this is what we get:

Not just 1 white pixel!

Here we get a series of coloured pixels. This calls for another question.

UPDATE 4:

I have no idea what was happening previously, but it seems with a np.uint8 array, Image.fromarray() uses colour values from 0-255. With this piece of wisdom, I move one step closer to understanding this Mandelbug!

Now, I do get something vaguely mathematical, however it still outputs strange things.

Black circle in White Background.

This dot is all there is... I get even stranger things if I change to np.uint16, I presume due to the different byte-shape and encoding scheme.


Show source
| numpy   | python   | python-imaging-library   | complex-numbers   | mandelbug   2017-01-07 18:01 1 Answers

Answers to When plotting Mandelbrot using NumPy &amp; Pillow, program outputs apparent noise ( 1 )

  1. 2017-01-07 20:01

    You are indexing the 3D array l incorrectly, try

    l[x,y,:] = [1,1,1]
    

    instead. For more details on how to access and modify numpy arrays have a look at numpy indexing

    As a side note: the quickstart documentation of numpy actually has an implementation of the mandelbrot set generation and plotting.

Leave a reply to - When plotting Mandelbrot using NumPy & Pillow, program outputs apparent noise

◀ Go back