Lesnic

AI

Apr 15th, 2021
565
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PIL import Image
  2. import random
  3. import numpy as np
  4.  
  5.  
  6. def blend(first: np.array, second: np.array) -> np.array:
  7.     res = first
  8.     for i in range(3 * height * width):
  9.         a = random.random()
  10.         if a > 0.98:
  11.             res[i] = random.randint(0, 255)
  12.         elif a > 0.4:
  13.             res[i] = second[i]
  14.         elif a < 0.09:
  15.             break
  16.     return res
  17.  
  18.  
  19. population = 40
  20. iteration = 500
  21. best = 10
  22.  
  23. ori = Image.open('C:\\Users\\Егор\\PycharmProjects\\AI\\kot.jpg')
  24.  
  25. width, height = ori.size
  26. original = np.array([list(ori.getdata())])
  27. original.resize(width * height * 3)
  28. current_population = np.zeros((population, width * height * 3))
  29.  
  30. for i in range(population):
  31.     for j in range(len(current_population[i])):
  32.         current_population[i][j] = random.randint(0, 255)
  33.  
  34. for q in range(iteration):
  35.     current_population = sorted(current_population, key=lambda x: sum(map(abs, x - original)))
  36.  
  37.     print(q)
  38.     new_population = np.zeros((best, width * height * 3))
  39.     for k in range(best):
  40.         new_population[k] += current_population[k]
  41.  
  42.     count = 0
  43.     current_population = np.zeros((population, width * height * 3))
  44.     for i in range(len(new_population)):
  45.         if count >= population:
  46.             break
  47.         for j in range(i + 1, best):
  48.             current_population[count] = np.array([blend(new_population[i], new_population[j])])
  49.             count += 1
  50.             if count >= population:
  51.                 break
  52.     if q % 50 == 0:
  53.         res = Image.new(mode="RGB", size=(width, height))
  54.         res2 = res.load()
  55.         count = 0
  56.         for i in range(width):
  57.             for j in range(height):
  58.                 a = (int(current_population[0][count]), int(current_population[0][count + 1]),
  59.                      int(current_population[0][count + 2]))
  60.                 res2[i, j] = a
  61.                 count += 3
  62.         res.save('temp.jpg')
  63.  
  64. res = Image.new(mode="RGB", size=(width, height))
  65. res2 = res.load()
  66. count = 0
  67. for i in range(width):
  68.     for j in range(height):
  69.         a = (int(current_population[0][count]), int(current_population[0][count + 1]),
  70.              int(current_population[0][count + 2]))
  71.         res2[i, j] = a
  72.         count += 3
  73.  
  74. res.save('res.jpg')
  75.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×