Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import random
- import numpy as np
- def blend(first: np.array, second: np.array) -> np.array:
- res = first
- for i in range(3 * height * width):
- a = random.random()
- if a > 0.98:
- res[i] = random.randint(0, 255)
- elif a > 0.4:
- res[i] = second[i]
- elif a < 0.09:
- break
- return res
- population = 40
- iteration = 500
- best = 10
- ori = Image.open('C:\\Users\\Егор\\PycharmProjects\\AI\\kot.jpg')
- width, height = ori.size
- original = np.array([list(ori.getdata())])
- original.resize(width * height * 3)
- current_population = np.zeros((population, width * height * 3))
- for i in range(population):
- for j in range(len(current_population[i])):
- current_population[i][j] = random.randint(0, 255)
- for q in range(iteration):
- current_population = sorted(current_population, key=lambda x: sum(map(abs, x - original)))
- print(q)
- new_population = np.zeros((best, width * height * 3))
- for k in range(best):
- new_population[k] += current_population[k]
- count = 0
- current_population = np.zeros((population, width * height * 3))
- for i in range(len(new_population)):
- if count >= population:
- break
- for j in range(i + 1, best):
- current_population[count] = np.array([blend(new_population[i], new_population[j])])
- count += 1
- if count >= population:
- break
- if q % 50 == 0:
- res = Image.new(mode="RGB", size=(width, height))
- res2 = res.load()
- count = 0
- for i in range(width):
- for j in range(height):
- a = (int(current_population[0][count]), int(current_population[0][count + 1]),
- int(current_population[0][count + 2]))
- res2[i, j] = a
- count += 3
- res.save('temp.jpg')
- res = Image.new(mode="RGB", size=(width, height))
- res2 = res.load()
- count = 0
- for i in range(width):
- for j in range(height):
- a = (int(current_population[0][count]), int(current_population[0][count + 1]),
- int(current_population[0][count + 2]))
- res2[i, j] = a
- count += 3
- res.save('res.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement