Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. import PIL.Image
  2. import numpy as np
  3. from colorama import Fore
  4. from colorama import Style
  5. from math import pow, sqrt
  6.  
  7.  
  8. available_colors = [
  9. (255, 255, 255), # Blanc (mur)
  10. (0, 0, 0), # Noir (marqueur)
  11. (255, 127, 237), # Rose
  12. (255, 106, 0), # Orange
  13. (0, 255, 255), # Cyan
  14. (76, 255, 0), # Vert
  15. ]
  16.  
  17.  
  18. def autocrop(image):
  19. image_data = np.asarray(image)
  20. image_data_bw = image_data.max(axis=2)
  21. non_empty_columns = np.where(image_data_bw.min(axis=0)<255)[0]
  22. non_empty_rows = np.where(image_data_bw.min(axis=1)<255)[0]
  23. cropBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
  24. return image_data[cropBox[0]:cropBox[1] + (1 if (cropBox[0] + cropBox[1]) % 2 == 1 else 2),
  25. cropBox[2]:cropBox[3] + (1 if (cropBox[2] + cropBox[3]) % 2 == 1 else 2), :]
  26.  
  27.  
  28. def main():
  29.  
  30. image = PIL.Image.open("./Mega_Venusaur.png").convert("RGB")
  31. image.load()
  32. cropped_image = PIL.Image.fromarray(autocrop(image))
  33. pixel_matrix = cropped_image.load()
  34. im_width, im_height = cropped_image.size
  35. render = PIL.Image.new("RGB", (im_width, im_height), (255, 255, 255))
  36. layer_one_render = PIL.Image.new("RGB", (im_width, im_height), (255, 255, 255))
  37. final_render = PIL.Image.new("RGB", (im_width // 8 * 9 + (im_width % 8), im_height // 8 * 9 + (im_height % 8)), (255, 255, 255))
  38.  
  39. xstart = 0
  40. ystart = 0
  41.  
  42. tab = []
  43. layer_one = [(0, 0, 0)] * (im_width * im_height)
  44. cpt = [0, 0, 0, 0]
  45.  
  46. for y in range(im_height):
  47. for x in range(im_width):
  48. distances = [sqrt(pow(k[0] - pixel_matrix[x, y][0], 2) +
  49. pow(k[1] - pixel_matrix[x, y][1], 2) +
  50. pow(k[2] - pixel_matrix[x, y][2], 2)) for k in available_colors]
  51. tab.append(available_colors[distances.index(min(distances))])
  52.  
  53. render.putdata(tab)
  54. render = render.resize((im_width * 16, im_height * 16))
  55. render.show()
  56.  
  57. counts = [0] * len(tab)
  58.  
  59. for y in range(ystart, im_height, 2):
  60. for x in range(xstart, im_width, 2):
  61.  
  62. if x < im_width - 1 and y < im_height - 1: # Milieu
  63. cell = [tab[y * im_width + x],
  64. tab[y * im_width + x + 1],
  65. tab[y * im_width + x + im_width],
  66. tab[y * im_width + x + im_width + 1]]
  67. counts[y * im_width + x] = cell.count(tab[y * im_width + x]) if tab[y * im_width + x] != (255, 255, 255) else 0
  68. counts[y * im_width + x + 1] = cell.count(tab[y * im_width + x + 1]) if tab[y * im_width + x + 1] != (255, 255, 255) else 0
  69. counts[y * im_width + x + im_width] = cell.count(tab[y * im_width + x + im_width]) if tab[y * im_width + x + im_width] != (255, 255, 255) else 0
  70. counts[y * im_width + x + im_width + 1] = cell.count(tab[y * im_width + x + im_width + 1]) if tab[y * im_width + x + im_width + 1] != (255, 255, 255) else 0
  71.  
  72. layer_one[y * im_width + x] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
  73. layer_one[y * im_width + x + 1] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
  74. layer_one[y * im_width + x + im_width] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
  75. layer_one[y * im_width + x + im_width + 1] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
  76.  
  77. layer_one_render.putdata(layer_one)
  78. layer_one_render = layer_one_render.resize((im_width * 16, im_height * 16))
  79. layer_one_render.show()
  80.  
  81. print("Xstart=" + str(xstart)
  82. + " ; Ystart=" + str(ystart)
  83. + "\t\t" + str([counts.count(i+1) // (i+1) for i in range(4)])
  84. + "\t\t" + str(sum([counts.count(i+3) // (i+3) for i in range(2)])))
  85.  
  86. if __name__ == '__main__':
  87. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement