在mandelbrot集中实现平滑着色

时间:2018-03-10 18:54:33

标签: python python-3.x fractals mandelbrot

我正在尝试使用HSV值和PIL库为MandelBrot着色。

即使经过多次尝试摆弄HSV值,我也无法达到预期的效果。

这是我目前拥有的

Current.png

这是期望的效果

Desired.png

这是我正在尝试的代码,如果您可以添加一些技巧来优化下面的代码以更快地计算集合,那么也可能是有益的,我是python的新手

from PIL import Image
import random
import math
from decimal import Decimal


# Size of the Image Canvas
HEIGHT = 500

ZOOM = 0.0
Y_PAN = 0.0


# Range of the Complex Plane
MIN_X = -2.0 + ZOOM
MAX_X = 2.0 - ZOOM


MAX_Y = 2.0 + Y_PAN - ZOOM
MIN_Y = -2.0 + Y_PAN + ZOOM

DATA = []


def map_to_scale_d(x, in_min, in_max, out_min, out_max):
    # returns float
    return float((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)


def map_to_scale(x, in_min, in_max, out_min, out_max):
    # returns int
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min


# Max iterations till Zn
ITER = 200

# loop to traverse every single point in Canvas
for y in xrange(HEIGHT):
    for x in xrange(HEIGHT):

        # convert to complex plane scale
        a = map_to_scale_d(x, 0, HEIGHT, MIN_X, MAX_X)
        b = map_to_scale_d(y, 0, HEIGHT, MAX_Y, MIN_Y)

        # original values
        _a = a  
        _b = b  

        counter = 0
        # start the iteration at (a,b) in complex plane
        # calculate z^2 + c
        while(counter < ITER):

            aa = a * a - b * b
            bb = 2 * a * b

            a = aa + _a
            b = bb + _b

            if((abs(aa + bb)) > 4):
                break

            counter = counter + 1

        # initialise color
        h = 0
        s = map_to_scale(counter, 0, ITER, 0, 100)
        v = map_to_scale(counter, 0, ITER, 0, 100)

        if(counter == ITER):
            h = 0
            s = 0
            v = 0

        # convert to 8-bit
        h = map_to_scale(h, 0, 360, 0, 255)
        s = map_to_scale(s, 0, 100, 0, 255)
        v = map_to_scale(v, 0, 100, 0, 255)

        DATA.append((h, s, v))

img = Image.new('HSV', (HEIGHT, HEIGHT))

img.putdata(DATA)
img.show()
img.convert('RGB').save('test.png')

1 个答案:

答案 0 :(得分:0)

只使用整数0100,然后将它们转换为范围0255,就会丢掉很多颜色范围。这可以通过使用:

来改善
# initialise color
h = 0
s = map_to_scale(counter, 0, ITER, 0, 360)
v = map_to_scale(counter, 0, ITER, 0, 360)

这会将您的结果改善为:

more colours

要使其与所需的输出相同,您需要采用不同的方法。