1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Strange black borders on count changes with cmath on my python fractal render

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I recently wrote a program which renders the mandelbrot set and encountered some irrelevent issues, which some helpful users solved for me. One user reccommended I change an aspect of my code for optimisation, and it drastically reduced the time it takes to render the set, but there are strange black lines around the areas where the iteration count increases.

    This is the whole progam:

    from PIL import Image
    from PIL import ImageShow
    from PIL import ImageColor as ImageColour
    import cmath as cmaths
    colours = ["navy","darkblue","blue","cornflowerblue","lightsteelblue","lightskyblue","turquoise","palegreen","lawngreen","greenyellow","yellowgreen","goldenrod","gold","yellow","darkorange","orange","brown","maroon","red","deeppink","darkmagenta","mediumorchid","magenta","darkviolet","slateblue"]
    def testpoint(c,zoom):
    zofn = 0.0
    count = 0
    while not cmaths.isinf(zofn) and count < (5*zoom):
    #Change the formula following the "zofn =" to change the fractal formula.
    zofn = (zofn*zofn)+c
    count = count + 1
    if str(zofn)[1] != "n":
    return -1
    else:
    return count

    def mainprogram():
    zoom = zoom = int(input("Set Zoom Level. (Default: 10) ") or "10")*10
    centre_x = int(input("Input centre x value. (Default: -50) ") or "-50")
    centre_y = int(input("Input centre y value. (Default: 0) ") or "0")
    halfw = result.width // 2
    halfh = result.height // 2
    for i in range((centre_y - halfh),(centre_y + halfh)):
    for x in range((centre_x - halfw),(centre_x + halfw)):
    coordinate = complex((x/zoom),(i/zoom))
    value = testpoint(coordinate,zoom)
    if value == -1:
    colour = (0,0,0)
    else:
    colour = ImageColour.getrgb(colours[value % len(colours)])
    result.putpixel(((x + halfw) % result.width, (i + halfh) % result.height),colour)
    print("line",(i+250),"done")
    ImageShow.show(result)
    mainprogram()
    result = Image.new("RGB",(1000,500),"Black")
    mainprogram()


    In my original code, the while loop worked like this:

    while str(zofn)[1] != "n" and count > (5*zoom):


    Which took around 40s on my machine to fully render the image, and outputs this:

    [​IMG] The set rendered on default settings.

    Using cmath (imported as cmaths cause I'm british), the while loop worked like this:

    while not cmath.isinf(zofn) and count > (5*zoom)


    Which took around 12s on my machine to fully render the image, and outputs this:

    [​IMG] The set again rendered on default settings, with the modified while loop.

    The only way those black lines could appear is if cmaths.isinf() considers those points not to diverge, which sucks because it's ugly and inaccurate, but it's such a vast optimisation that I can't just abandon the concept. Is there anyway I can remove these lines, or any other way to optimise my code such that I don't need to use cmaths.isinf()?

    Continue reading...

Compartilhe esta Página