You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 06:55:14