def evolve(source_path, target_path, triangle_count): try: surface = pyglet.image.load(source_path) except: log("could not load source file: %s" % source_path) sys.exit(1) width, height = surface.width, surface.height display = Display((width, height), os.path.basename(source_path)) environment = Environment(surface, display) try: parent = Image.load(target_path) except: parent = Image.generate((width, height), triangle_count) parent_fitness = environment.fitness(parent) generation = 0 log("generation = %s, fitness = %s" % (generation, parent_fitness)) while not display.check_quit(): generation += 1 child = parent.mutate() child_fitness = environment.fitness(child) log("generation = %s, child_fitness = %s" % (generation, child_fitness)) if child_fitness < parent_fitness: parent = child parent_fitness = child_fitness log("generation = %s, fitness = %s" % (generation, parent_fitness)) try: parent.save(target_path) except: log("could not save target file: %s" % target_path) sys.exit(1)
def view(path, zoom): try: image = Image.load(path) except: log("could not load file: %s" % path) sys.exit(1) width, height = image.resolution resolution = int(round(width * zoom)), int(round(height * zoom)) display = Display(resolution, os.path.basename(path)) while not display.check_quit(): display.draw_image(image)