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

84 lines
3.1 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',
(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()
# задаем коэффициент увеличения
scale = 2
# создаем новое увеличенное изображение и заполняем его пикселями из списка
new_size = (img.size[0]*scale, img.size[1]*scale)
new_img = Image.new('RGB', new_size, color='white')
new_pixels = new_img.load()
for x in range(img.size[0]):
for y in range(img.size[1]):
# находим позиции пикселей на увеличенном изображении
new_x, new_y = x*scale, y*scale
# находим RGB значение текущего пикселя на увеличенном изображении
rgb = [key for key, value in COLORS.items() if value == result_list[y*img.size[0] + x][2]][0]
# заполняем пикселы на увеличенном изображении
new_pixels[new_x, new_y] = rgb
new_pixels[new_x+1, new_y] = rgb
new_pixels[new_x, new_y+1] = rgb
new_pixels[new_x+1, new_y+1] = 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()