gpt4 book ai didi

python - 在Mandelbrot中实现平滑着色

转载 作者:行者123 更新时间:2023-11-28 19:03:58 25 4
gpt4 key购买 nike

我试图用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')

最佳答案

你只使用整数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
要使它与所需的输出相同,您需要不同的方法。

关于python - 在Mandelbrot中实现平滑着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com