I'm attempting to speed up my genetic algorithm that modifies images. After searching, I've found that my
fitness function is the bottleneck, taking sometimes up to 11 seconds to complete.
fitness function accepts two images and returns a float representing the total difference between the images' corresponding pixels, measured as distance in Cartesian RGB space.
To test, I used some large images of the same size (1254 x 834). What can I do to speed this process up?
def fitness(original, new): fitness = 0 for x in range(0, width): for y in range(0, height): r1, g1, b1 = original.getpixel((x, y)) r2, g2, b2 = new.getpixel((x, y)) deltaRed = abs(r1 - r2) deltaGreen = abs(g1 - g2) deltaBlue = abs(b1 - b2) pixelFitness = pixelFitness = math.sqrt(deltaRed ** 2 + deltaGreen ** 2 + deltaBlue ** 2) fitness += pixelFitness return fitness