You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pxl_oboard/im_convert.py

133 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from PIL import Image
#from tkinter import Tk, Button, Canvas, PhotoImage
COLORS = {
(255, 255, 255): 'white',
(255, 0, 0): 'light_red',
(192, 0, 0): 'red',
(128, 0, 0): 'dark_red',
(80, 25, 40): 'maroon',
(25, 5, 5): 'dark_maroon',
(115, 40, 60): 'light_maroon',
(165, 90, 25): 'brown',
(210, 150, 95): 'light_brown',
(50, 40, 10): 'dark_brown',
(250, 245, 200): 'sand',
(255, 128, 0): 'orange',
(255, 255, 0): 'yellow',
(255, 215, 0): 'light_gold',
(185, 135, 10): 'dark_gold',
(220, 165, 30): 'gold',
(0, 255, 0): 'light_lime',
(0, 128, 0): 'lime',
(128, 255, 0): 'salad',
(0, 255, 128): 'mint',
(0, 128, 0): 'green',
(0, 255, 255): 'cyan',
(0, 128, 255): 'light_blue',
(0, 0, 255): 'blue',
(0, 65, 90): 'dark_blue',
(128, 0, 128): 'dark_purple',
(128, 0, 255): 'purple',
(255, 0, 128): 'dark_pink',
(255, 0, 255): 'pink',
(192, 192, 192): 'light_gray',
(128, 128, 128): 'gray',
(64, 64, 64): 'dark_gray',
(0, 0, 0): 'black'
}
#CONVERT for main.py
#from collections import OrderedDict
#new_colors = dict((value, key) for key, value in COLORS.items())
#print(new_colors)
#exit(0)
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]
# создаем новое изображение и заполняем его пикселями из списка
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
new_img.show()
# смещение!!!!!!!!!!!!!!!!!!!!!
#-------------------------------------------------
move = [0, 0]
m0 = input("Enter move on X: ")
if m0 == "":
m0 = 0
m1 = input("Enter move on Y: ")
if m1 == "":
m1 = 0
move = [int(m0), int(m1)]
#-------------------------------------------------
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()
from bot import *
from remove_back import *
print("\nOptimize image?")
a = input("(Y/N) : ")
if a.lower() == 'y':
res_list = optimize(res_list)
print("\nCheck image.")
print("Upload image?")
a = input("(Y/N) : ")
if a.lower() == 'y':
draw(res_list)