平滑着色\归一化迭代与朱利亚集合 - Python

0 投票
1 回答
1552 浏览
提问于 2025-04-18 16:29

我一直在用Python写一个朱利亚集合的代码。虽然我不是最开始写这个代码的人,但我稍微修改了一下,达到了我想要的效果……除了颜色不够平滑!我查过怎么做,但还是有点困惑。

这是我在尝试颜色平滑之前的代码:

def julia():
    while True:
        try:
            julwidth = int(input("What width would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            julheight = int(input("What height would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            iter = int(input("Max iterations? If you don't know, 255 is a good number \n"))
            break
        except ValueError:
            print("Enter an integer, please")
    print("Running Julia fractal... please wait...")
    imgx = julwidth
    imgy = julheight
    image = Image.new("RGB", (imgx, imgy))

    # drawing area
    xa = -2.0
    xb = 2.0
    ya = -1.5
    yb = 1.5
    maxIt = iter # max iterations allowed
    Time = time.clock()
    #print(time)
    # find a good Julia set point using the Mandelbrot set
    while True:
        cx = random.random() * (xb - xa) + xa
        cy = random.random() * (yb - ya) + ya
        c = cx + cy * 1j
        z = c
        for i in range(maxIt):
            if abs(z) > 2.0:
                break 
            z = z * z + c
        if i > 10 and i < 100:
            break

    # draw the Julia set
    for y in range(imgy):
        zy = y * (yb - ya) / (imgy - 1)  + ya
        for x in range(imgx):
            zx = x * (xb - xa) / (imgx - 1)  + xa
            z = zx + zy * 1j
            for i in range(maxIt):
                if abs(z) > 2.0:
                    break 
                z = z * z + c
            red = i % 8 *32
            green = i % 16 * 16
            blue = i % 32 * 8
                #print(red, green, blue)
            image.putpixel((x, y), (red, green, blue))
    print("Done computing. \n")

这是我尝试添加平滑效果后的代码:

def julia():
    while True:
        try:
            julwidth = int(input("What width would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            julheight = int(input("What height would you like the image? ex. 256, 512, etc. \nLarger numbers take longer to compute.\n"))
            iter = int(input("Max iterations? If you don't know, 255 is a good number \n"))
            break
        except ValueError:
            print("Enter an integer, please")
    print("Running Julia fractal... please wait...")
    imgx = julwidth
    imgy = julheight
    image = Image.new("RGB", (imgx, imgy))

    # drawing area
    xa = -2.0
    xb = 2.0
    ya = -1.5
    yb = 1.5
    maxIt = iter # max iterations allowed
    Time = time.clock()
    #print(time)
    # find a good Julia set point using the Mandelbrot set
    while True:
        cx = random.random() * (xb - xa) + xa
        cy = random.random() * (yb - ya) + ya
        c = cx + cy * 1j
        z = c
        for i in range(maxIt):
            if abs(z) > 2.0:
                break 
            z = z * z + c
            smooth = (abs(z))
        if i > 10 and i < 100:
            break

    # draw the Julia set
    for y in range(imgy):
        zy = y * (yb - ya) / (imgy - 1)  + ya
        for x in range(imgx):
            zx = x * (xb - xa) / (imgx - 1)  + xa
            z = zx + zy * 1j
            for i in range(maxIt):
                if abs(z) > 2.0:
                    break 
                z = z * z + c
                smooth += (abs(z))
                #print(smooth)
            red = ceil(smooth % 8 * 32)
            green = ceil(red*red)
            blue = ceil(green*red)
                #print(red, green, blue)
            image.putpixel((x, y), (red, green, blue))
    print("Done computing. \n")

如果你看不出来,我添加了一个叫做smooth的变量,想用它来通过复数变量z来平滑颜色。运行我加了平滑效果的代码后,结果是……并不是我想要的效果。出来的是一幅奇怪的蓝色图像,效果不好。我该怎么给这个代码加上平滑效果呢?我可能对平滑的理解不够,所以如果能解释一下就太好了。

1 个回答

2

计算分数逃逸次数,并根据这个值设置颜色:

ec = N + 1 - log (log |Z(N)|) / log 2 这里的 N 是当值 'z' 逃逸时的逃逸次数,Z(N)z 逃逸时的值,而 |z|z 的模,也就是 sqrt (x*x + y*y)

撰写回答