[Python] Mandelbrot Zoom

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    I recently watched a video on creating a Mandelbrot Zoom in Python - https://youtu.be/HtNUFdh2sjg?si=07JVGOlksJtmzev-

    After manually copying the file to use in Pycharm, I commented out what I thought would be a color plot. I also changed the center point(s) but to no avail.

    Here is the code.

    Main Function that Implements the Mandelbrot Set


    def MandelbrotPoints(iteration_maximum: int, c:complex):
    #create a function that determines which points are in the
    #Mandelbrot Set and which points are not
    z = 0
    #begin with z = 0
    stability_value = 1
    for i in range(iteration_maximum):
    #a loop to go through all iterations up until the
    #iteration_maximum
    z = z ** 2 + c
    #equation that defines the Mandelbrot Set
    if abs(z) > 2.0:
    #check if the magnitude of z is larger than 2 to see
    #if the point escapes
    stability_value = i/iteration_maximum
    return stability_value
    #return the stability_value. 1 means that the point
    #is still in the set
    # and less than 1 means it escaped

    return stability_value


    #############################################################

    ######################### Main Loop ######################## for i in range(100): image = Image.new(mode="RGB", size=(1024,1024)) # create new image, size is (width,height) in pixels, # change color mode as desired. "L" is black and white #(grayscale) # image = Image.new(mode="RGB", size=(512, 512)) # #"RGB" is a colored option

    #center_point = 0 + 0j # center point of image
    #center_point = -0.761577 -0.08472907899j
    center_point = -0.10715093446959 - 0.91210639325904j
    # center_point = 0.25100997358377 + 0.000063j

    desired_width = (1 / (2 ** (i + 1)))
    # this pattern of changing width should work decently for a
    # zoom, but can be changed as desired
    # desired_width = (1/(i+1))

    scale = desired_width / image.width # these 3 steps scale
    #the original image size to the desired size
    final_width = scale * image.width
    final_height = scale * image.height

    for a in range(image.width):
    # loop to color pixels based on whether points are in the
    #Mandelbrot Set/how fast they escape
    for b in range(image.height):
    # it first converts a pixel to a coordinate in the
    #complex plane and then assigns the appropriate color
    pixel = image.getpixel((a, b))
    complex_coordinate = complex(a, -b) * scale + center_point + complex(-final_width, final_height) / 2
    instability_value = 1

    MandelbrotPoints(iteration_maximum=1000,c=complex_coordinate)
    image.putpixel((a, b), int((instability_value) * 255))
    #use code below if select "RGB" color option##########
    if(instability_value == 0):
    image.putpixel((a,b),(0,0,0))
    else:
    image.putpixel((a,b), (int((1-instability_value) * 255),
    int((instability_value)*255),int(175)))
    #experiment with (r,g,b) values to produce different colored
    #schemes


    image.show()

    ############################

    I have pillow version 5.3 and python version 3.7.1

    The compatibility chart shows that they are compatible.

    Python Support - Pillow (PIL Fork) 11.2.1 documentation

    I exported all the data to excel to ensure that the data was correct and that the Stability and Instability values were changing.

    Yes, I am new at Python but I had assumed that the default viewer would allow the Zoom feature but I must be missing something. Is there a module that includes a Zoom app?

    Thanks in advance.

    Continue reading...

Compartilhe esta Página