How to draw Buddhabrot fractal? - python

I'm trying to implement Buddhabrot fractal in Python. I read a lot of articles and posts but I think that I missunderstood something (just see the image). Someone can write a pseudocode?
My code is this:
from multiprocessing import Pool
from random import randrange
import matplotlib.pyplot as plt
import numpy as np
from math import ceil
maxiter = 1000
points = 1000
xmin, xmax = -2, 1
ymin, ymax = -2, 1
cores = 4
width, height = 200, 200
maxn = width * height
incrx, incry = abs(xmax - xmin) / width, abs(ymax - ymin) / height
def randomComplexGenerator():
for i in range(points):
n = randrange(maxn)
yield complex(n // height * incrx, n % width * incry)
def buddhabrot(c):
m, z, i = np.zeros((width, height)), c, 0
while i < maxiter and abs(z) < 2:
x, y = ceil(z.real / incrx), ceil(z.imag / incry)
m[x, y] += 1
z = z ** 2 + c
i += 1
return m if i == maxiter else 0
if __name__ == '__main__':
a = np.linspace(xmin, xmax, width)
b = np.linspace(ymin, ymax, height)
with Pool(cores) as p:
ms = p.map(buddhabrot, (c for c in randomComplexGenerator()))
res = 0
for m in ms:
res += m
plt.axis('off')
plt.imshow(res)
plt.show()
The image generated with my code is this (lel):

After days, this is the code that I created, which seems to generate appropriately the fractal. Any performance suggestion is welcome.
from multiprocessing import Pool
from random import randrange
import matplotlib.pyplot as plt
import numpy as np
cores = 4
maxiter = 10000
points = 1000000
width, height = 200, 200
rdom, idom = (-2, 2), (-2, 2)
xdom, ydom = (0, width - 1), (0, height - 1)
def randomComplex():
r = np.interp(randrange(xdom[0], xdom[1]), xdom, rdom)
i = np.interp(randrange(ydom[0], ydom[1]), ydom, idom)
return (r, i)
def complex2pixel(c):
x = int(np.interp(c[0], rdom, xdom))
y = int(np.interp(c[1], idom, ydom))
return (x, y)
def escapedPixels(c):
pixels, z = {}, c
for i in range(maxiter):
z2 = (z[0] * z[0], z[1] * z[1])
if z2[0] + z2[1] > 4: break
p = complex2pixel(z)
try: pixels[p] += 1
except: pixels[p] = 1
z = (z2[0] - z2[1] + c[0], 2 * z[0] * z[1] + c[1])
return pixels if i < maxiter - 1 else {}
if __name__ == '__main__':
with Pool(cores) as p:
ds = p.map(escapedPixels, (randomComplex() for i in range(points)))
m = np.zeros((width, height))
for d in ds:
for p in d:
m[p] += d[p]
plt.axis('off')
plt.imshow(m)
plt.show()

Related

plt.matshow().set_data() is not updating the AxesImage

Here's a program that I am trying to run for Potts Model. It gives the plot once and then keeps repeating the line <Figure size 432x288 with 0 Axes>. What's the solution to get various plots after certain time steps showing the evolution of the system?
import math
import numpy as np
import matplotlib.pyplot as plt
def largest_primes_under(N):
n = N - 1
while n >= 2:
if all(n % d for d in range(2, int(n ** 0.5 + 1))):
return n
n -= 1
def Neighbors(Lattice,i,j,n=1):
''' Returns an flat array of all neighboring sites in the n-th coordination sphere including the center'''
N, M = Lattice.shape
rows = [(i-1) % N, i, (i+1) % N]
cols = [(j-1) % N, j, (j+1) % M]
return Lattice[rows][:, cols].flatten()
def calc_dE(Lattice, x, y, z):
N, M = Lattice.shape
old_energy = 0
new_energy = 0
for i in [0,1,-1]:
for j in [0,1,-1]:
if i == 0 and j == 0:
continue
if Lattice[x%N,y%M] == Lattice[(x+i)%N,(y+j)%M]:
old_energy += 1
elif z == Lattice[(x+i)%N,(y+j)%M]:
new_energy += 1
return old_energy-new_energy
N, M = 100,100
orientations = 3
MCS = int(10)
a = largest_primes_under(N*M)
L = np.random.randint(1,orientations+1,size=(N,M))
mat = plt.matshow(L,cmap = plt.get_cmap('plasma', orientations+1), vmin = -0.5, vmax = orientations+0.5, interpolation='kaiser')
plt.axis('off')
for t in range(1,MCS+1):
rand = np.random.random_integers(N*M)
for i in range(0,N**2):
index = (a*i + rand) % (N**2)
x = index % N
y = index // N
n = Neighbors(L,x,y)
if len(n)-1 == 0:
continue
else:
z = np.random.choice(n)
dE = calc_dE(L,x,y,z)
if (dE < 0):
L[x%N,y%N] = z
elif np.random.sample() < math.exp(-dE*2.5):
L[x%N,y%N] = z
mat.set_data(L)
plt.draw()
plt.pause(0.1)
mat.set_data(L) is not updating the data
In the for-loop, replace mat.set_data(L) with:
mat = plt.matshow(L, cmap = plt.get_cmap('plasma', orientations+1), vmin = -0.5, vmax = orientations+0.5, interpolation='kaiser')
The plots successfully showed up when I tested the code with the change.
Also np.random.random_integers(N*M) is deprecated in numpy v1.20.1. In the code below, np.random.randint(N*M) is used, but this change isn't related to the question in the OP.
for t in range(1, MCS+1):
rand = np.random.randint(N*M)
for i in range(0, N**2):
index = (a*i + rand) % (N**2)
x = index % N
y = index // N
n = Neighbors(L, x, y)
if len(n)-1 == 0:
continue
else:
z = np.random.choice(n)
dE = calc_dE(L, x, y, z)
if (dE < 0):
L[x%N, y%N] = z
elif np.random.sample() < math.exp(-dE*2.5):
L[x%N, y%N] = z
mat = plt.matshow(L, cmap = plt.get_cmap('plasma', orientations+1), vmin = -0.5, vmax = orientations+0.5, interpolation='kaiser')
# mat.set_data(L)
plt.draw()
plt.pause(0.1)
Alternative
In this case it might be more interesting to animate the progression
Implemented with Animate quadratic grid changes (matshow)
In the following code, save_count=MCS takes the place of the original outer loop for t in range(1, MCS+1), where t was just a throwaway variable.
import matplotlib.animation as animation
def generate_data():
rand = np.random.randint(N*M)
for i in range(0, N**2):
index = (a*i + rand) % (N**2)
x = index % N
y = index // N
n = Neighbors(L, x, y)
if len(n)-1 == 0:
continue
else:
z = np.random.choice(n)
dE = calc_dE(L, x, y, z)
if (dE < 0):
L[x%N, y%N] = z
elif np.random.sample() < math.exp(-dE*2.5):
L[x%N, y%N] = z
return L
def update(data):
mat.set_data(data)
return mat
def data_gen():
while True:
yield generate_data()
N, M = 100, 100
orientations = 3
MCS = 10
a = largest_primes_under(N*M)
L = np.random.randint(1, orientations+1, size=(N, M))
fig, ax = plt.subplots()
mat = ax.matshow(generate_data(), cmap=plt.get_cmap('plasma', orientations+1), vmin=-0.5, vmax=orientations+0.5, interpolation='kaiser')
plt.colorbar(mat)
ani = animation.FuncAnimation(fig, update, data_gen, interval=500, save_count=MCS)
plt.show()
ani.save('animation.gif')

Marching Square algorithm in Python

The following Python source code is the implementation of Marching Square algorithm:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import cv2
from PIL import Image, ImageDraw
class Square():
A = [0, 0]
B = [0, 0]
C = [0, 0]
D = [0, 0]
A_data = 0.0
B_data = 0.0
C_data = 0.0
D_data = 0.0
def GetCaseId(self, threshold):
caseId = 0
if (self.A_data >= threshold):
caseId |= 1
if (self.B_data >= threshold):
caseId |= 2
if (self.C_data >= threshold):
caseId |= 4
if (self.D_data >= threshold):
caseId |= 8
return caseId
def GetLines(self, Threshold):
lines = []
caseId = self.GetCaseId(Threshold)
if caseId in (0, 15):
return []
if caseId in (1, 14, 10):
pX = (self.A[0] + self.B[0]) / 2
pY = self.B[1]
qX = self.D[0]
qY = (self.A[1] + self.D[1]) / 2
line = (pX, pY, qX, qY)
lines.append(line)
if caseId in (2, 13, 5):
pX = (self.A[0] + self.B[0]) / 2
pY = self.A[1]
qX = self.C[0]
qY = (self.A[1] + self.D[1]) / 2
line = (pX, pY, qX, qY)
lines.append(line)
if caseId in (3, 12):
pX = self.A[0]
pY = (self.A[1] + self.D[1]) / 2
qX = self.C[0]
qY = (self.B[1] + self.C[1]) / 2
line = (pX, pY, qX, qY)
lines.append(line)
if caseId in (4, 11, 10):
pX = (self.C[0] + self.D[0]) / 2
pY = self.D[1]
qX = self.B[0]
qY = (self.B[1] + self.C[1]) / 2
line = (pX, pY, qX, qY)
lines.append(line)
elif caseId in (6, 9):
pX = (self.A[0] + self.B[0]) / 2
pY = self.A[1]
qX = (self.C[0] + self.D[0]) / 2
qY = self.C[1]
line = (pX, pY, qX, qY)
lines.append(line)
elif caseId in (7, 8, 5):
pX = (self.C[0] + self.D[0]) / 2
pY = self.C[1]
qX = self.A[0]
qY = (self.A[1] + self.D[1]) / 2
line = (pX, pY, qX, qY)
lines.append(line)
return lines
def marching_square(xVector, yVector, Data, threshold):
linesList = []
Height = len(Data) # rows
Width = len(Data[1]) # cols
if ((Width == len(xVector)) and (Height == len(yVector))):
squares = np.full((Height - 1, Width - 1), Square())
sqHeight = squares.shape[0] # rows count
sqWidth = squares.shape[1] # cols count
for j in range(sqHeight): # rows
for i in range(sqWidth): # cols
a = Data[j + 1, i]
b = Data[j + 1, i + 1]
c = Data[j, i + 1]
d = Data[j, i]
A = [xVector[i], yVector[j + 1]]
B = [xVector[i + 1], yVector[j + 1]]
C = [xVector[i + 1], yVector[j]]
D = [xVector[i], yVector[j]]
squares[j, i].A_data = a
squares[j, i].B_data = b
squares[j, i].C_data = c
squares[j, i].D_data = d
squares[j, i].A = A
squares[j, i].B = B
squares[j, i].C = C
squares[j, i].D = D
list = squares[j, i].GetLines(threshold)
linesList = linesList + list
else:
raise AssertionError
return [linesList]
def main():
x = [i for i in range(256)]
y = [i for i in range(256)]
example_l = [[0 for i in range(256)] for j in range(256)]
for i in range(len(example_l)):
for j in range(len(example_l[0])):
example_l[i][j] = math.sin(i / 10.0)*math.cos(j / 10.0)
example = np.array(example_l)
im = Image.new('RGB', (256, 256), (128, 128, 128))
collection = marching_square(x, y, example, 0.9)
draw = ImageDraw.Draw(im)
for ln in collection:
for toup in ln:
draw.line(toup, fill=(255, 255, 0), width=1)
plt.axis("off")
plt.imshow( im )
plt.show()
if __name__ == '__main__':
main()
Output:
My question is: Why does this source code generate circular patterns?
what is the mathematical explanation of this circular pattern?
You are applying the marching squares algorithm to a sample of the surface defined by:
F(x,y) = sin(x)*cos(y)
If you plot it, e.g. with google here
You get a surface that looks like eggs boxes, and your marching squares algorithm is finding the isolines ("lines following a single data level, or isovalue.") at 0.9, which are circles. You can imagine this as the intersection of that surface with a plane parallel to the XY plane and at Z = 0.9.

Object values not being reset in python function

Here is my code. In the calculateOptimalLambda() function, I am attempting to declare a copy of n and
store it as m, remove one point from m, and make some calculations and a graph. Then, the loop should
restart, make a fresh copy of m, remove the next point, and so on.
However, when in the next iteration
of the loop, a point has been removed. Eventually, I run out of points to remove, and I get an error.
How do I declare a fresh copy of m so I can remove the next point?
import numpy as np
from matplotlib import pyplot as plt
class Data:
def __init__(self, points, sigma, lamda):
self.points = points
self.sigma = sigma
self.sample = np.random.uniform(-1,1, (points, 2))
self.transformedData = np.ones((points, 5))
self.weight = np.zeros((5,1))
self.lamda = lamda
def changeLamda(self,x):
self.lamda = x
def removePoint(self, x):
self.points = self.points - 1
self.sample = np.delete(self.sample, x, 0)
self.transformedData = np.delete(self.transformedData, x, 0)
def transformedFunction(self, x):
transformedData = np.ones((1, 5))
transformedData[0,1] = x
transformedData[0,2] = 0.5 * (3*x**2 -1)
transformedData[0,3]= 0.5 * (5*x**3 - 3*x)
transformedData[0,4] = 0.125 * (35*x**4 -30*x**2 + 3)
return np.dot(transformedData, self.weight)
def setY(self):
for i in range(len(self.sample[0:,0])):
self.sample[i,1] = np.random.normal(0, self.sigma) + self.sample[i,0]**2
def transform(self):
for i in range(len(self.sample[0:,0])):
self.transformedData[i,1] = self.sample[i,0]
self.transformedData[i,2] = 0.5 * (3*self.sample[i,0]**2 -1)
self.transformedData[i,3]= 0.5 * (5*self.sample[i,0]**3 - 3*self.sample[i,0])
self.transformedData[i,4] = 0.125 * (35*self.sample[i,0]**4 -30*self.sample[i,0]**2 + 3)
def calculateWeight(self):
z = n.transformedData
zProd = np.linalg.inv(np.matmul(np.transpose(z), z) + np.identity(5)*self.lamda)
next1 = np.matmul(zProd,np.transpose(z))
a = self.sample[0:,1]
a = a.reshape((-1, 1))
print(a)
self.weight = np.matmul(next1,a)
def calculateError(self):
error= (np.matmul(self.transformedData, self.weight) - self.sample[1,0:])
return error/self.points
def calculateOptimalLambda(n, L):
a = 0
for i in range(len(L)):
n.changeLamda(L[i])
for x in range(n.getPoints()):
a+=1
plt.subplot(4,5,a)
m = n
m.removePoint(x)
m.calculateWeight()
weight = m.getWeight()
error = m.calculateError()
twoD_plot(m)
print(error)
def twoD_plot(n):
t = np.linspace(-1, 1, 400)
x = np.square(t)
plt.plot(t,x,'b')
error = 0
y = x
for i in range(len(t)):
y[i] = n.transformedFunction(t[i])
error += (y[i] - t[i]**2)**2
"""print(error/len(t))"""
plt.plot(t,y,'r')
plt.scatter(n.getSample()[0:,0],n.getSample()[0:,1], c = 'g', marker = 'o')
n = Data(5,0.1,0)
n.setY()
n.transform()
n.calculateWeight()
L = [1, 0.01, 0.00001, 0]
calculateOptimalLambda(n, L)
plt.show()

What is wrong with my Implementation of 4th Order runge kutta in python for nonholonomic constraints?

I am trying to implement 4th order Runge Kutta for nonholonomic motion for car-like robots.
I don't know what I am doing wrong,essentially I am passing +-Pi/4 to calculate hard left and right turns to get different trajectories.
But no matter if I pass +pi/4 or -pi/4 to it, I get the same answer.
I cannot figure out what I am doing wrong.
The constraint equations that I am using are:
thetadot = (s/L)*tan(phi)
xdot = s*cos(theta)
ydot = s*sin(theta)
Where s is the speed and L is the length of the car like robot.
#! /usr/bin/env python
import sys, random, math, pygame
from pygame.locals import *
from math import sqrt,cos,sin,atan2,tan
import numpy as np
import matplotlib.pyplot as plt
XDIM = 640
YDIM = 480
WINSIZE = [XDIM, YDIM]
PHI = 45
s = 0.5
white = 255, 240, 200
black = 20, 20, 40
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
cyan = 0,255,255
pygame.init()
screen = pygame.display.set_mode(WINSIZE)
X = XDIM/2
Y = YDIM/2
THETA = 45
def main():
nodes = []
nodes.append(Node(XDIM/2.0,YDIM/2.0,0.0))
plt.plot(runge_kutta(nodes[0], (3.14/4))) #Hard Left turn
plt.plot(runge_kutta(nodes[0], 0)) #Straight ahead
plt.plot(runge_kutta(nodes[0], -(3.14/4))) #Hard Right turn
plt.show()
class Node:
x = 0
y = 0
theta = 0
distance=0
parent=None
def __init__(self,xcoord, ycoord, theta):
self.x = xcoord
self.y = ycoord
self.theta = theta
def rk4(f, x, y, n):
x0 = y0 = 0
vx = [0]*(n + 1)
vy = [0]*(n + 1)
h = 0.8
vx[0] = x = x0
vy[0] = y = y0
for i in range(1, n + 1):
k1 = h*f(x, y)
k2 = h*f(x + 0.5*h, y + 0.5*k1)
k3 = h*f(x + 0.5*h, y + 0.5*k2)
k4 = h*f(x + h, y + k3)
vx[i] = x = x0 + i*h
vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4)/6
print "1"
print vy
return vy
def fun1(x,y):
x = (0.5/2)*tan(y)
print "2"
print x
return x
def fun2(x,y):
x = 0.5*cos(y)
print "3"
print x
return x
def fun3(x,y):
x = 0.5*sin(y)
print "4"
print x
return x
def runge_kutta(p, phi):
x1 = p.x
y1 = p.y
theta1 = p.theta
fi = phi
for i in range(0,5):
x2 = rk4(fun2, x1, theta1, 5)
y2 = rk4(fun3, y1, theta1, 5)
theta2 = rk4(fun1, theta1 ,fi, 5)
theta1 = theta2
print "5"
print zip(x2,y2)
return zip(x2,y2)
# if python says run, then we should run
if __name__ == '__main__':
main()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I can't really say much about the algorithm, but the way you set up our rk4 function, the x, and y arguments will never have any effect:
def rk4(f, x, y, n):
x0 = y0 = 0 # x0 and y0 will both be 0 after this
vx = [0]*(n + 1)
vy = [0]*(n + 1)
h = 0.8
vx[0] = x = x0 # now x will be 0
vy[0] = y = y0 # and y will be 0 too
...
The rest of the function will use x=0 and y=0 in any case.
Also, I don't know if that's intentional, but the other functions fun1, fun2 and fun3 don't ever use the parameter passed as x, they only use y. They change x locally, but that won't reflect outside the function.

Creating a movie in Jython/Python

I am trying to make a movie, whilst creating frames through a loop. It is saving, but only the first frame (which it plays as a movie - short movie!) I've tried various things and cannot figure out what I am doing wrong. Thanks
def synthesiseFrame(folder):
folder =r"D:\FOLDER"
m=0.5
for x in range(1,121):
pic=makeEmptyPicture(960,540)
for x in range (0,960):
for y in range (0,540):
r=#some code
g=#some code
b=#some code
color =makeColor (r,g,b)
px= getPixel (pic, x, y)
setColor(px, color)
numStr=str(x)
m=m+0.0125
if x<10:
writePictureTo(pic, folder+"\pic00"+numStr+".png")
if x >=10 and x<100:
writePictureTo(pic, folder+"\pic0"+numStr+".png")
if x>=100:
writePictureTo(pic,folder+"\pic"+numStr+".png")
return movie
movie=synthesiseFrame(folder)
folder =r"D:\FOLDER"
file=r"D:\FOLDER\pic00.png"
movie=makeMovieFromInitialFile(file)
writeQuicktime(movie,"D:\FOLDER\movie.mov", 30)
playMovie(movie)
My first sight at JES video functions and at your code tells me something like (fully working example):
import os
import random
def synthesizeFrameAndCreateMovie(folder):
# Create an empty movie to receive the frames
movie = makeMovie()
# Compute & save the frames
w = 40
h = 25
nb_frames = 60 # Will give 60 frames at 30 fps => movie duration : 2 sec.
for z in range(0, nb_frames):
pic=makeEmptyPicture(w, h)
for x in range (0, w):
for y in range (0, h):
#makeColor() takes red, green, and blue (in that order) between 0 and 255
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = makeColor(r,g,b)
px = getPixel(pic, x, y)
setColor(px, color)
# Create one frame and inject in the movie object
filename = os.path.join(folder, 'pic%03d.png' % z)
writePictureTo(pic, filename)
addFrameToMovie(filename, movie)
# return the movie
return movie
movie = synthesizeFrameAndCreateMovie("D:\\FOLDER")
print movie
#writeQuicktime(movie,"D:\\FOLDER\\movie.mov", 30)
playMovie(movie)
Output (frames):
.............................................
EDIT :
More fun : animating a line (code taken form here)...
import os
import random
# Draw point, with check if the point is in the image area
def drawPoint(pic, col, x, y):
if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)):
px = getPixel(pic, x, y)
setColor(px, col)
# Draw line segment, given two points
# From Bresenham's line algorithm
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
def drawLine(pic, col, x0, y0, x1, y1):
dx = abs(x1-x0)
dy = abs(y1-y0)
sx = sy = 0
#sx = 1 if x0 < x1 else -1
#sy = 1 if y0 < y1 else -1
if (x0 < x1):
sx = 1
else:
sx = -1
if (y0 < y1):
sy = 1
else:
sy = -1
err = dx - dy
while (True):
drawPoint(pic, col, x0, y0)
if (x0 == x1) and (y0 == y1):
break
e2 = 2 * err
if (e2 > -dy):
err = err - dy
x0 = x0 + sx
if (x0 == x1) and (y0 == y1):
drawPoint(pic, col, x0, y0)
break
if (e2 < dx):
err = err + dx
y0 = y0 + sy
# Draw infinite line from segment
def drawInfiniteLine(pic, col, x0, y0, x1, y1):
# y = m * x + b
m = (y0-y1) / (x0-x1)
if (abs(m) > 100.0):
m = 100.0
# y0 = m * x0 + b => b = y0 - m * x0
b = y0 - m * x0
x0 = 0
y0 = int(m*x0 + b)
# get a 2nd point far away from the 1st one
x1 = getWidth(pic)
y1 = int(m*x1 + b)
drawLine(pic, col, x0, y0, x1, y1)
# Draw infinite line from origin point and angle
# Angle 'theta' expressed in degres
def drawInfiniteLineA(pic, col, x, y, theta):
# y = m * x + b
dx = y * tan(theta * pi / 180.0) # (need radians)
dy = y
if (dx == 0):
dx += 0.000000001 # Avoid to divide by zero
m = dy / dx
# y = m * x + b => b = y - m * x
b = y - m * x
# get a 2nd point far away from the 1st one
x1 = 2 * getWidth(pic)
y1 = m*x1 + b
drawInfiniteLine(pic, col, x, y, x1, y1)
def synthesizeFrameAndCreateMovie(folder):
# Create an empty movie to receive the frames
movie = makeMovie()
# Compute & save the frames
w = 40
h = 25
nb_frames = 120 # Will give 120 frames at 30 fps => movie duration : 4 sec.
for z in range(0, nb_frames):
pic = makeEmptyPicture(w, h)
addRectFilled(pic, 0, 0, w-1, h-1)
#makeColor() takes red, green, and blue (in that order) between 0 and 255
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
col = makeColor(r,g,b)
theta = z * 360 / nb_frames
if (theta != 180.0) and (theta != 0.0):
drawInfiniteLineA(pic, col, w//2, h//2, theta)
# Create one frame and inject in the movie object
filename = os.path.join(folder, 'pic%03d.png' % z)
writePictureTo(pic, filename)
addFrameToMovie(filename, movie)
# return the movie
return movie
movie = synthesizeFrameAndCreateMovie("/home/FOLDER")
print movie
#writeQuicktime(movie,"/home/golgauth/Desktop/FOLDER/movie.mov", 30)
#writeAVI(movie,"/home/golgauth/Desktop/FOLDER/movie.avi")
playMovie(movie)
Output (frames):
.............................................
I changed your code.
Used '%03d'%x instead of if*3.
change 'pic00.png' to 'pic001.png' because the loop in synthesiseFrame start from 1.
'\' -> os.path.join(..); Put import os if you didn't.
def synthesiseFrame(folder):
m = 0.5
for frameNumber in range(1,121):
pic=makeEmptyPicture(960,540)
for x in range (0,960):
for y in range (0,540):
r = #some code
g = #some code
b = #some code
color =makeColor (r,g,b)
px= getPixel (pic, x, y)
setColor(px, color)
m += 0.0125
writePictureTo(pic, os.path.join(folder, 'pic%03d.png' % frameNumber)) # 3 if -> no if
return movie
movie = synthesiseFrame(folder)
folder = r"D:\FOLDER"
file = r"D:\FOLDER\pic001.png" # 00 -> 001
movie=makeMovieFromInitialFile(file)
writeQuicktime(movie,"D:\FOLDER\movie.mov", 30)
playMovie(movie)
EDIT
x (in outer loop) -> frameNumber

Categories

Resources