如何将Python绘制的Mandelbrot集图像背景设为青色?
修改Mandelbrot集图像背景为青色的方法
你的代码里,背景色由mandelbrot函数最后一行的返回值决定——当某个点属于Mandelbrot集(循环1000次仍未逃出边界)时,会返回这个颜色,原来的(0, 0, 0)是黑色,我们只需要把它换成青色的RGB值即可。
修改步骤:
- 找到
mandelbrot函数的最后一行:return (0, 0, 0) - 将其替换为青色的RGB元组:
return (0, 255, 255)(标准青色,你也可以根据需求调整数值,比如浅青色用(100, 255, 255))
修改后的完整代码:
# Python code for Mandelbrot Fractal # Import necessary libraries from PIL import Image from numpy import complex, array import colorsys # setting the width of the output image as 1024 WIDTH = 1024 # a function to return a tuple of colors # as integer value of rgb def rgb_conv(i): color = 255 * array(colorsys.hsv_to_rgb(i / 255.0, 1.0, 0.5)) return tuple(color.astype(int)) # function defining a mandelbrot def mandelbrot(x, y): c0 = complex(x, y) c = 0 for i in range(1, 1000): if abs(c) > 2: return rgb_conv(i) c = c * c + c0 return (0, 255, 255) # 这里改成青色的RGB值 # creating the new image in RGB mode img = Image.new('RGB', (WIDTH, int(WIDTH / 2))) pixels = img.load() for x in range(img.size[0]): # displaying the progress as percentage print("%.2f %%" % (x / WIDTH * 100.0)) for y in range(img.size[1]): pixels[x, y] = mandelbrot((x - (0.75 * WIDTH)) / (WIDTH / 4), (y - (WIDTH / 4)) / (WIDTH / 4)) # to display the created fractal after # completing the given number of iterations img.show()
内容的提问来源于stack exchange,提问作者Neo
相关产品推荐
相关产品推荐

