我不知道为什么相同的代码适用于朱莉娅,但不适用于曼德博?



我有以下生成Mandelbrot图像的代码。图像周围的空白,必须去除。

import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from numpy import NaN
def mandelbrot(C):
z = 0
for n in range(1, 10):
z = z**2 + C
if abs(z) > 2:
return n
return NaN
def plot():
X = np.arange(-2.0, 1.0, 0.05)
Y = np.arange(-1.5, 1.5, 0.05)
pixel = np.zeros((len(Y), len(X)))
for x_iter, x in enumerate(X):
for y_iter, y in enumerate(Y):
pixel[y_iter, x_iter] = mandelbrot(x + 1j * y)
imshow(pixel, cmap = 'gray', extent = (X.min(), X.max(), Y.min(), Y.max()))
return pixel
pixel = mandelbrot(-0.7 + 0.27015j)
plt.axis('off')  
plot()
plt.show()
from PIL import Image
min_value = np.nanmin(pixel)
max_value = np.nanmax(pixel)
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
# sample LUT from matplotlib
lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow
pixel_rgb = lut[pixel_int]
# changing NaNs to a chosen color
nan_color = [0,0,0,0] # Transparent NaNs
for i,c in enumerate(nan_color):
pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])
# apply LUT and display
img = Image.fromarray(pixel_rgb, 'RGBA')
print(pixel)

但事实证明IndexError:对于行来说,数组的索引太多

pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])

请问,怎么修?

事实上,为了消除图像周围的空白,几周前,Julia而不是Mandelbrot使用了相同的代码(同一行(。以下生成Julia图像的代码将去除图像周围的空白。

import numpy as np
import matplotlib.pyplot as plt
def julia(C):
X = np.arange(-1.5, 1.5, 0.05)
Y = np.arange(-1.5, 1.5, 0.05)
pixel = np.zeros((len(Y), len(X)))
for x_iter, x in enumerate(X):
for y_iter, y in enumerate(Y):
z = x + 1j * y
intensity = np.nan
r = np.empty((100, 100)) # Unused at the moment
for n in range(1, 1024):
if abs(z) > 2:
intensity = n
break
z = z**2 + C
pixel[y_iter, x_iter] = intensity
r.fill(intensity) # Unused at the moment
# We return pixel matrix
return pixel
# Compute Julia set image
pixel = julia(-0.7 + 0.27015j)
# Plotting
print(pixel)
plt.show()
from PIL import Image
min_value = np.nanmin(pixel)
max_value = np.nanmax(pixel)  
#want to set all the 255 pixels to removed
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
# sample LUT from matplotlib,If lut is not None it must be an integer giving the number of entries desired in the lookup table
lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow
pixel_rgb = lut[pixel_int]
# changing NaNs to a chosen color
nan_color = [0,0,0,0] # Transparent NaNs
for i,c in enumerate(nan_color):
pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])
# apply LUT and display
img = Image.fromarray(pixel_rgb, 'RGBA')
img.save('julia.tiff')
Image.open('julia.tiff').show()
print(min_value, max_value)

现在,我只是不知道为什么这个去除图像周围空白的代码不适用于Mandelbrot?!请帮我解决这个问题!

您的直接问题是,在Julia情况下,pixel_rgb是一个三维数组,而在Mandelbrot情况中,pixel_rgb是一个一维数组。因此,你试图对它们中的每一个应用三维变换,这在Mandelbrot的情况下会失败,因为你正在操作的只有一个维度,而不是三个维度。

我没有更多的时间来完全理解和处理您的代码,但在Mandelbrot的情况下,mandelbrot()函数似乎只返回一个值,其中julia()函数返回一个2D数组。在Mandelbrot情况下,返回2D数组的是plot()函数。所以我快速猜测你想做的改变是改变这个:

pixel = mandelbrot(-0.7 + 0.27015j)
plt.axis('off')
plot()

到此:

# pixel = mandelbrot(-0.7 + 0.27015j)
plt.axis('off')
pixel = plot()

这允许Mandelbrot代码在不崩溃的情况下运行。我不知道它是否正是在做你想做的事。

最新更新