0

can someone please look over this code and see if they can make it run faster. if you make any changes will you comment it so i can see what you did. this is a personal project.
thanks you very much

#Image manipulation library
from PIL import Image
import datetime, random

#Declares variables
num_lines = 0
print_percent = 1
a_percent = 0
b_percent = 0
tempcolor=''
temp=""
maximum=1000
infinity=0
res=0
width=300
height=0
min_x=-2.0
min_y=-1.0
max_x=1.0
max_y=1.0
point_x=0
point_y=0
c=complex(-2.0,1.0)
z=complex(0.0,0.0)
most_in=0
most_out=0
iterations=0
temploop=[0]
loop=[9000]
color_type=0
amount_red=255
amount_blue=255
redbluetype=0
redspread=2
greenspread=1
outside_bluespread=1
testloop=0
lines = 0

#Defines function repeat for testing for loop, ### NO LONGER DEFINES function testingloop to simplify testing loop
def repeat(x): return temploop[x]==temploop[x-1]

        

#Gets user input
print ("~~~Welcome to mandelbrot set grapher version 3.0~~~")
temp=raw_input("\n\nChoose the width of the image (in pixels) (default 300): ")

#Sets width
if (len(temp)>0):
    width=int(temp)

#Continues getting user input
temp=raw_input("\n\nSet the maximum number of iterations (default 1000): ")

#Sets maximum iterations
if (len(temp)>0):
    maximum=int(temp)

#Gets input on whether to test for loops
temp=raw_input("\n\nChoose whether or not to test for loops (usefull only if maximum iterations\nis larger than 50,000) (enter 1 to turn on, 0 for off) (default off)")
if (len(temp)!=0):
    testloop=int(temp)

#Continues getting user input
print ("\n\nOutside color types:\n\tType 0: int(255*float(iterations)/most_out)\n\tType 1: int(most_out/float(iterations))")
tempcolor=raw_input("\nChoose outside color type (1/0), or press enter to have the generator autochoose\n")
temp=raw_input("\n\nInput green spread (default 1) (float or int): ")
if (len(temp)!=0):
    greenspread=float(temp)
temp=raw_input("\n\nInput outside blue spread (default 1) (float or int): ")
if (len(temp)!=0):
    outside_bluespread=float(temp)



temp=raw_input("\n\nInput minimum x value (default -2.0): ")
if (len(temp)!=0):
    min_x=float(temp)
temp=raw_input("\n\nInput maximum x value (default 1.0): ")
if (len(temp)!=0):
    max_x=float(temp)
temp=raw_input("\n\nInput minimum y value (default -1.0): ")
if (len(temp)!=0):
    min_y=float(temp)
temp=raw_input("\n\nInput maximum y value (default 1.0): ")
if (len(temp)!=0):
    max_y=float(temp)
temp=raw_input("\n\nInput amount of red (default 255): ")
if (len(temp)!=0):
    amount_red=int(temp)
temp=raw_input("\n\nInput amount of blue (default 255): ")
if (len(temp)!=0):
    amount_blue=int(temp)
temp=raw_input("\n\nInside color types:\n\tBlue completely covering, red overlapping blue (1)\n\tBlue only up to red (0)\n\nChoose an inside color type (default 0): ")
if (len(temp)!=0):
    redbluetype=int(temp)
temp=raw_input("\n\nInput inside red spread (default 2) (float or int): ")
if (len(temp)!=0):
    redspread=float(temp)
print ("\n\nGenerating...")

#Finds starting point
point_x=min_x
point_y=min_y

#Finds height and resolution
res=abs(max_x-min_x)/float(width)
height=(abs(min_y-max_y))/res

#Sets color type
if (len(tempcolor)==0):
    if (res<.01):
        colortype=0
    else:
        colortype=1
else:
    colortype=int(tempcolor)

#Gets basic file name, creates directory if missing
outfile=('Mandelgenerator/'+str(datetime.datetime.now())+'.png')
if (not os.access('Mandelgenerator', os.F_OK)):
    os.mkdir('Mandelgenerator')

#Removes ':'s from filename, does some minor rearranging
outfile=outfile.replace(':','.')

#Creates an image to graph on
# open(outfile, 'w')
im=Image.new("RGB", (int(width), int(height)))
im.save(outfile)
im = Image.open(outfile)

#Begins graphing loop
while (1):
    #Sets z to check correct point
    c=complex(point_x,point_y)

    #Resets certain variables
    z=complex(0.0,0.0)
    temploop=[0]
    loop=[9000,9001,9002,9003]
    iterations=0

    #Begins testing loop
    while (1):
        iterations+=1
    
        #Calculates next value in series
        z=z*z+c

        #Adds item to loop tester, tests for repeated values
        if testloop:
            loop.append(abs(z))
            loop.remove(loop[0])
            temploop=loop[:]
            temploop.sort()
            if (1==len(filter(repeat, range(1, len(temploop))))):
                infinity=0
                break

        #Stops if iterations is more than 1000
        if (iterations>maximum):
            infinity=0
            break

        #Infinity tester
        if (z.real>2):
            infinity=1
            break

        #End of testing loop

    #Sets the color of the pixel being tested

    #Calculates most number of iterations outside
    if (iterations>most_out and infinity==1):
        most_out=iterations

    #Calculates most number of iterations inside
    if (iterations>most_in and infinity==0):
        most_in=iterations
    
    if (infinity==0):
        if (redbluetype==1):
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(height-(int(point_y/res)-int(min_y/res)))-1), (int(amount_red*(point_y*(point_x-1)*redspread)),0,amount_blue))
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(point_y/res)-int(min_y/res)), (int(amount_red*(point_y*(point_x-1)*redspread)),0,amount_blue))
        if (redbluetype==0):
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(height-(int(point_y/res)-int(min_y/res)))-1), (int(amount_red*(point_y*(point_x-1)*redspread)),0,amount_blue-int(amount_red*(point_y*(point_x-1)*redspread))))
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(point_y/res)-int(min_y/res)), (int(amount_red*(point_y*(point_x-1)*redspread)),0,amount_blue-int(amount_red*(point_y*(point_x-1)*redspread))))
    if (infinity==1):
        if (color_type==1):
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(height-(int(point_y/res)+int(max_y/res)))-1), (0,int((255-int(255*float(iterations)/most_out))*greenspread),int(255*float(iterations)/most_out*outside_bluespread)))
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(point_y/res)+int(max_y/res)), (0,int((255-int(255*float(iterations)/most_out))*greenspread),int(255*float(iterations)/most_out*outside_bluespread)))
        if (color_type==0):
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(height-(int(point_y/res)-int(min_y/res)))-1), (0,255-int(most_out/float(iterations)*greenspread),int(most_out/float(iterations)*outside_bluespread)))
            im.putpixel((int(point_x/res)-(int(min_x/res)), int(point_y/res)-int(min_y/res)), (0,255-int(most_out/float(iterations)*greenspread),int(most_out/float(iterations)*outside_bluespread)))

    #Moves point being tested
    point_x+=res
    if (point_x>max_x-res):
        point_x=min_x
        point_y+=res
        
        percent_done=abs(point_y-min_y)/abs(max_y-min_y)*200
        num_lines+=1
        if (int(num_lines/print_percent)==float(num_lines)/print_percent):
            for i in range(0, 100, 1):
                a_percent = int(percent_done)
                if i == int(percent_done) and a_percent != b_percent:
                    print int(percent_done), "% done"
                    a_percent = int(percent_done)
                    b_percent = a_percent
                    try:
                        im.save(outfile)
                    except:
                        print'Got an error, thank god I didnt crash.'
    
        
    if (point_y>((min_y+max_y)/2)):
        print "\n\n~~~Done~~~"
        im.save(outfile)
        print "\n~~~ Saved as:", outfile, "~~~"
        break
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pyTony
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.