gpt4 book ai didi

python - 使用 NumPy 和 Pillow 绘制 Mandelbrot 时,程序输出明显的噪音

转载 作者:行者123 更新时间:2023-11-28 22:31:32 25 4
gpt4 key购买 nike

之前,我使用 turtle 在 python 中创建了一个 Mandelbrot 生成器。现在,我正在重新编写程序以使用 Python 图像库,以提高速度并减少对图像大小的限制。

然而,下面的程序只输出RGB废话,几乎是噪声。我认为这与 NumPy 和 PIL 处理数组的方式不同有关,因为说 l[x,y] = [1,1,1] where l = np .zeros((height,width,3))img = Image.fromarray(l) 时,不会 仅使 1 个像素变白img.show() 被执行。

def imagebrot(mina=-1.25, maxa=1.25, minb=-1.25, maxb=1.25, width=100, height=100, maxit=300, inf=2):
l,b = np.zeros((height,width,3), dtype=np.float64), minb

for y in range(0, height):
a = mina
for x in range(0, width):

ab = mandel(a, b, maxit, inf)

if ab[0] == maxit:
l[x,y:] = [1,1,1]

#if ab[0] < maxit:
#smoothit = mandelc(ab[0], ab[1], ab[2])
#l[x, y] = colorsys.hsv_to_rgb(smoothit, 1, 1)

a += abs(mina-maxa)/width
b += abs(minb-maxb)/height

img = Image.fromarray(l, "RGB")
img.show()

def mandel(re, im, maxit, inf):
z = complex(re, im)
c,it = z,0

for i in range(0, maxit):
if abs(z) > inf:
break
z,it = z*z+c,it+1
return it,z,inf

def mandelc(it,z,inf):
return (it+1-log(log(abs(z)))/log(2))

更新 1:

我意识到这个程序中的主要错误之一(我敢肯定有很多)是我使用 x,y 坐标作为复数系数!所以,0 到 100 而不是 -1.25 到 1.25!我已经更改了它,以便代码现在使用变量 a、b 来描述它们,并以我从 turtle 版本中的一些代码中窃取的方式递增。上面的代码已相应更新。由于平滑着色算法代码目前已被注释掉以供调试,因此 inf 变量的大小已减小到 2

更新 2:

我在一位优秀用户的帮助下编辑了 numpy 索引。当设置为 200 x 200 时,程序现在输出:

Failed Mandelbrot

如您所见,它确实显示了一些数学形状,但充满了所有这些奇怪的红色、绿色和蓝色像素!为什么这些会在这里?我的程序只能将 RGB 值设置为 [1,1,1] 或将其保留为默认值 [0,0,0]。它不能是 [1,0,0] 或类似的东西 - 这一定是一个严重的缺陷...

更新 3:

我认为 NumPy 和 PIL 的集成存在错误。如果我制作 l = np.zeros((100, 100, 3)) 然后声明 l[0,0,:] = 1 最后 img = Image.fromarray(l) & img.show(),这就是我们得到的:

Not just 1 white pixel!

这里我们得到了一系列彩色像素。 需要另一个问题。

更新 4:

我不知道之前发生了什么,但似乎对于 np.uint8 数组,Image.fromarray() 使用 0-255 的颜色值。有了这一点智慧,我离理解这个曼德尔虫又近了一步!

现在,我确实得到了一些模糊的数学结果,但它仍然输出奇怪的东西。

Black circle in White Background.

这个点就是全部...如果我更改为 np.uint16,我会得到更奇怪的东西,我推测是由于不同的字节形状和编码方案。

最佳答案

你对 3D 数组 l 的索引不正确,试试

l[x,y,:] = [1,1,1]

相反。有关如何访问和修改 numpy 数组的更多详细信息,请查看 numpy indexing

作为旁注:quickstart documentation numpy 实际上有一个 mandelbrot 集生成和绘图的实现。

关于python - 使用 NumPy 和 Pillow 绘制 Mandelbrot 时,程序输出明显的噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524343/

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