用python将mandelbrot集写入图像

用python将mandelbrot集写入图像,python,mandelbrot,Python,Mandelbrot,我试图用python编写一个mandelbrot集到一个图像,但我的一个函数有问题 问题是:虽然我期待类似的事情。我得到一张纯白的照片。这是我的密码: 代码的快速摘要: 检查值是否在集合中,如果在集合中,则在布尔数组中将其标记为true。然后,根据布尔数组绘制图像,将真图像着色,保留假图像 import math import numpy as np import scipy.misc as smp from PIL import PILLOW_VERSION from PIL import I

我试图用python编写一个mandelbrot集到一个图像,但我的一个函数有问题

问题是:虽然我期待类似的事情。我得到一张纯白的照片。这是我的密码:

代码的快速摘要: 检查值是否在集合中,如果在集合中,则在布尔数组中将其标记为true。然后,根据布尔数组绘制图像,将真图像着色,保留假图像

import math
import numpy as np
import scipy.misc as smp
from PIL import PILLOW_VERSION
from PIL import Image

def iterate(x, y, iterationNum):
    z = 0
    coord = complex(x, y)
    for a in xrange(iterationNum):
        #Don't use fabs. It can be negative.
        z = z * z + coord
        #This is a comparison between complex and int. It probably won't work.
        #You want |Z| which is: z.real ** 2 + z.imag ** 2 > 4
        if math.fabs(z) > 2:
            return False
    return True

def pixel(image,x,y,r,g,b):
   """Place pixel at pos=(x,y) on image, with color=(r,g,b)"""
   image.put("#%02x%02x%02x" % (r,g,b), (y, x))

#here's some example coloring code that may help:
def draw(grid):
    #Create a white image with the size of the grid as the number of pixels
    img = Image.new('RGB', (len(grid), len(grid)), "white")
    pixels = img.load()
    for row in xrange(len(grid)):
        for col in xrange(len(grid[row])):
            if grid[row][col] == True:
                #If that point is True (it's in the set), color it blue
                pixels[row, col] = (0, 0, 255)
    return img

def mandelbrot():
    #you should probably use a square, it's easier to deal with
    #The mandelbrot set fits completely within (-2, 2) and (2, -2)
    #(-200, 200), (200, -200) is way too big!
    TopLeftX = -2; BottomRightX = 2
    TopLeftY = 2; BottomRightY = -2
    #increment should be calculated based on the size of the bounds and the number of pixels
    #For example, if you're between -2 and 2 on the X-Plane, and your image is 400 pixels wide
    #Then your increment = (2 - (-2)) / 400 = 4 / 400 = .01 so that each pixel is 1/400th of the
    #Total width of the bounding area
    increment = 0.01
    maxIt = 100
    w = BottomRightX - TopLeftX
    h = TopLeftY - BottomRightY
    #This should be based on the size of the image, one spot in the area for one pixel
    npArr = np.zeros((w / increment, h / increment), dtype=bool)
    #Use the increment variable from above. It won't work with xrange because that doesn't
    #Support decimals. You probably want to use a while loop or something
    x = -2
    y = 2
    while TopLeftX <= x <= BottomRightX:
        while TopLeftY <= y <= BottomRightY:
            #I recommend using True or False in here (in the set or not)
            #And then do your color calculations as I explained above
            #Saves a lot of memory
            if iterate(x, y, maxIt):
                npArr[x, y] = True
            y += increment
    #once you've calculated the Trues and Falses, you'd call the draw() function
    #using the npArr as the parameter. I haven't tested the code, so there may
    #be a few bugs, but it should be helpful!
        x += increment
    return npArr

img = draw(mandelbrot())
img.save("mandelbrot.png")
导入数学
将numpy作为np导入
将scipy.misc作为smp导入
从PIL导入枕型
从PIL导入图像
def iterate(x,y,iterationNum):
z=0
坐标=复数(x,y)
对于xrange(iterationNum)中的对象:
#不要使用晶圆厂。它可能是负面的。
z=z*z+coord
#这是complex和int之间的比较。它可能不起作用。
#你想要| Z |,即:Z.real**2+Z.imag**2>4
如果math.fabs(z)>2:
返回错误
返回真值
def像素(图像,x,y,r,g,b):
“”“将像素放置在图像上的位置=(x,y),颜色=(r,g,b)”
图像。放置(#%02x%02x%02x“%(r,g,b),(y,x))
#下面是一些可能有帮助的着色代码示例:
def绘图(网格):
#创建一个白色图像,网格大小为像素数
img=Image.new('RGB',(透镜(网格),透镜(网格)),“白色”)
像素=img.load()
对于xrange(len(grid))中的行:
对于xrange中的列(len(网格[行]):
如果网格[行][列]==真:
#如果该点为真(在集合中),则将其涂成蓝色
像素[行,列]=(0,0,255)
返回img
def mandelbrot():
#你可能应该用正方形,这样比较容易处理
#mandelbrot集合完全符合(-2,2)和(2,-2)的范围
#(-200200),-200,-200)太大了!
TopLeftX=-2;BottomRightX=2
TopLeftY=2;BottomRightY=-2
#增量应根据边界的大小和像素数计算
#例如,如果在X平面上介于-2和2之间,并且图像的宽度为400像素
#然后你的增量=(2-(-2))/400=4/400=.01,这样每个像素就是
#边界区域的总宽度
增量=0.01
最大值=100
w=右下角X-左上角X
h=左上角-右下角
#这应该基于图像的大小,一个像素区域中的一个点
npArr=np.zero((w/增量,h/增量),dtype=bool)
#使用上面的增量变量。它不适用于xrange,因为它不适用于xrange
#支持小数。您可能想使用while循环或其他方法
x=-2
y=2

而TopLeftX您对
y
坐标的处理有问题。外部循环从

y = 2
并将循环条件设置为

while TopLeftY <= y <= BottomRightY:
但是
y
已经处于范围的顶端。此外,您无法为每个内部循环重置
y

总之,循环应该是

x = TopLeftX                                # use the value you already defined!
while TopLeftX <= x <= BottomRightX:
    y = TopLeftY                            # moved to inside x loop
    while TopLeftY >= y >= BottomRightY:    # change the loop condition
        # ... the Mandelbrot iteration
        y -= increment                      # reverse direction
    x += increment
使用您已经定义的值! TopLeftX=BottomRightY:#更改循环条件 # ... 曼德布罗特迭代 y-=增量#反向 x+=增量

我不是Python专家,所以可能还有其他问题。

对于
iterationNum
的任何“truthy”值,
iteration
都不可能返回
True
,因为它有一个只包含
返回False
的无限循环。你的意思是为循环执行某种类型的
,或者可能更改循环中的
iterationNum
的值吗?看起来你总是将
100
传递到
iteration
,所以它确实可能只返回
False
@smarx,这就是我想要做的。@smarx那么你认为我应该从1开始,迭代次数达到100次?我不知道你想做什么。:-)你写的代码。。。你想发生什么?非常感谢你的回答!我已经解决了你指出的所有问题,但我会把你的问题作为正确答案。谢谢D
y += increment    
x = TopLeftX                                # use the value you already defined!
while TopLeftX <= x <= BottomRightX:
    y = TopLeftY                            # moved to inside x loop
    while TopLeftY >= y >= BottomRightY:    # change the loop condition
        # ... the Mandelbrot iteration
        y -= increment                      # reverse direction
    x += increment