Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import PIL.Image
- import numpy as np
- from colorama import Fore
- from colorama import Style
- from math import pow, sqrt
- available_colors = [
- (255, 255, 255), # Blanc (mur)
- (0, 0, 0), # Noir (marqueur)
- (255, 127, 237), # Rose
- (255, 106, 0), # Orange
- (0, 255, 255), # Cyan
- (76, 255, 0), # Vert
- ]
- def autocrop(image):
- image_data = np.asarray(image)
- image_data_bw = image_data.max(axis=2)
- non_empty_columns = np.where(image_data_bw.min(axis=0)<255)[0]
- non_empty_rows = np.where(image_data_bw.min(axis=1)<255)[0]
- cropBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
- return image_data[cropBox[0]:cropBox[1] + (1 if (cropBox[0] + cropBox[1]) % 2 == 1 else 2),
- cropBox[2]:cropBox[3] + (1 if (cropBox[2] + cropBox[3]) % 2 == 1 else 2), :]
- def main():
- image = PIL.Image.open("./Mega_Venusaur.png").convert("RGB")
- image.load()
- cropped_image = PIL.Image.fromarray(autocrop(image))
- pixel_matrix = cropped_image.load()
- im_width, im_height = cropped_image.size
- render = PIL.Image.new("RGB", (im_width, im_height), (255, 255, 255))
- layer_one_render = PIL.Image.new("RGB", (im_width, im_height), (255, 255, 255))
- final_render = PIL.Image.new("RGB", (im_width // 8 * 9 + (im_width % 8), im_height // 8 * 9 + (im_height % 8)), (255, 255, 255))
- xstart = 0
- ystart = 0
- tab = []
- layer_one = [(0, 0, 0)] * (im_width * im_height)
- cpt = [0, 0, 0, 0]
- for y in range(im_height):
- for x in range(im_width):
- distances = [sqrt(pow(k[0] - pixel_matrix[x, y][0], 2) +
- pow(k[1] - pixel_matrix[x, y][1], 2) +
- pow(k[2] - pixel_matrix[x, y][2], 2)) for k in available_colors]
- tab.append(available_colors[distances.index(min(distances))])
- render.putdata(tab)
- render = render.resize((im_width * 16, im_height * 16))
- render.show()
- counts = [0] * len(tab)
- for y in range(ystart, im_height, 2):
- for x in range(xstart, im_width, 2):
- if x < im_width - 1 and y < im_height - 1: # Milieu
- cell = [tab[y * im_width + x],
- tab[y * im_width + x + 1],
- tab[y * im_width + x + im_width],
- tab[y * im_width + x + im_width + 1]]
- counts[y * im_width + x] = cell.count(tab[y * im_width + x]) if tab[y * im_width + x] != (255, 255, 255) else 0
- 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
- 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
- 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
- layer_one[y * im_width + x] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
- layer_one[y * im_width + x + 1] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
- layer_one[y * im_width + x + im_width] = cell[cell.index(max(list(filter(lambda a: a != (0, 0, 0), cell)) + [(0, 0, 0)]))]
- 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)]))]
- layer_one_render.putdata(layer_one)
- layer_one_render = layer_one_render.resize((im_width * 16, im_height * 16))
- layer_one_render.show()
- print("Xstart=" + str(xstart)
- + " ; Ystart=" + str(ystart)
- + "\t\t" + str([counts.count(i+1) // (i+1) for i in range(4)])
- + "\t\t" + str(sum([counts.count(i+3) // (i+3) for i in range(2)])))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement