Hi. I am completely new to Processing, and know nothing of Java, but I am fairly experienced with Python, so I have been writing some code with the Python version of Processing. I am writing a code that moves particles around according to the values of pixels in an image, and ran into errors whenever I tried to run it. I would get NoneType errors on the variable I created to hold an image. Here is my code.
image_name = "image.jpg"
particles_number = 100
picture = loadImage(image_name)
magnetic_constant = 255
import math
def setup():
background(255)
stroke(0)
picture = loadImage(image_name)
size(512 * 2 + 2, 512)
image(picture, 0, 0)
create_particles(particles_number)
draw_particles(picture.width)
particles_list = []
def closest_particle(particle_index):
current_distance = 100
for i in range(0, particles_number + 1):
if i != particle_index:
x_off = (particles_list[particle_index].location[0] - particles_list[i].location[0])
y_off = (particles_list[particle_index].location[1] - particles_list[i].location[1])
if x_off **2 + y_off ** 2 < current_distance:
current_closest = i
return current_closest, x_off, y_off
def move_particle(particle_index):
closest, x_off, y_off = closest_particle(particle_index)
direction = math.floor(y_off /( x_off +0.1) * 8)
closest = particles_list[closest]
particle = particles_list[particle_index]
if x_off + y_off == 0:
particle.location = [particle.location[0] + int(random(-2, 2)), particle.location[1] + int(random(-2, 2))]
else:
magnitude = int(closest.charge / (x_off + y_off))
if direction == 0:
particle.location[0] += magnitude
elif direction == 1:
offset = int(magnitude / 1.41)
particle.location[0] += offset
particle.location[1] += offset
elif direction == 2:
particle.location[1] += magnitude
elif direction == 3:
offset = int(magnitude / 1.41)
particle.location[0] -= offset
particle.location[1] += offset
elif direction == 4:
particle.location[0] -= magnitude
elif direction == 5:
offset = int(magnitude / 1.41)
particle.location[0] -= offset
particle.location[1] -= offset
elif direction == 6:
particle.location[1] -= magnitude
elif direction == 7:
offset = int(magnitude / 1.41)
particle.location[0] += offset
particle.location[1] -= offset
change_charge(particle)
def change_charge(particle):
x = particle.location[0]
y = particle.location[1]
picture.loadPixels()
pix = picture.pixels(index(x, y))
gray = 0.21 * red(pix) + 0.72 * green(pix) + 0.07 * blue(pix)
charge = gray / magnetic_constant
picture.updatePixels()
def index(x, y):
return x * picture.width + y
def move_particles():
for i in range(0, particles_number + 1):
move_particle(i)
def draw_particles(image_width):
for i in particles_list:
point(i.location[0] + image_width + 2., i.location[1])
class particle():
def __init__(self, location, charge, ID):
self.location = location
self.charge = charge
self.ID = ID
def create_particles(n):
for i in range(0, n + 1):
new_particle = particle([math.floor(i / 512), i % 512], 1, i)
particles_list.append(new_particle)
def draw():
move_particles()
draw_particles(picture.width)
What can I do fix this? Am I doing something stupid?