from PIL import Image #from tkinter import Tk, Button, Canvas, PhotoImage COLORS = { (255, 255, 255): 'white', (0, 0, 255): 'blue', (255, 0, 0): 'red', (0, 255, 0): 'green', (0, 0, 0): 'black' } img = Image.open('image.png') pixels = img.load() result_list = [] for x in range(img.size[0]): for y in range(img.size[1]): # получаем RGB значение текущего пикселя pixel = pixels[x, y] # находим ближайший цвет из словаря COLORS distance = float('inf') nearest_color = None for rgb, color in COLORS.items(): current_distance = ((pixel[0]-rgb[0])**2 + (pixel[1]-rgb[1])**2 + (pixel[2]-rgb[2])**2)**0.5 if current_distance < distance: distance = current_distance nearest_color = color # добавляем итоговое значение в список result_list.append([x, y, nearest_color]) # создаем копию списка для инверсии по оси X inverted_list = result_list.copy() for i in range(len(inverted_list)): x, y, color_name = inverted_list[i] # изменяем значение x-координаты для инвертирования по оси X inverted_y = img.size[1] - y - 1 inverted_list[i] = [x,inverted_y , color_name] # смещение!!!!!!!!!!!!!!!!!!!!! #------------------------------------------------- move = [0, 0] #------------------------------------------------- res_list = result_list.copy() for i in range(len(inverted_list)): x, y, color_name = inverted_list[i] move_x = x + move[0] move_y = y + move[1] res_list[i] = [move_x, move_y, color_name] f = open("out.txt",'w') f.write(str(res_list)) f.close() # создаем новое изображение и заполняем его пикселями из списка new_img = Image.new('RGB', (img.size[0], img.size[1]), color='white') new_pixels = new_img.load() for x, y, color in result_list: rgb = [key for key, value in COLORS.items() if value == color][0] new_pixels[x, y] = rgb # инвертируем изображение по оси X new_img = new_img.transpose(method=Image.FLIP_LEFT_RIGHT) # поворачиваем изображение на 90 градусов влево new_img = new_img.transpose(method=Image.ROTATE_90) new_img.show()