226 lines
8.2 KiB
Python
226 lines
8.2 KiB
Python
import tkinter as tk
|
|
from tkinter import filedialog
|
|
import serial
|
|
import time
|
|
from PIL import Image, ImageEnhance
|
|
|
|
PORT = 'COM3'
|
|
BAUD = 115200
|
|
|
|
try:
|
|
ser = serial.Serial(PORT, BAUD, timeout=0.01)
|
|
time.sleep(1)
|
|
print("Připojeno k Pico na COM3.")
|
|
except Exception as e:
|
|
ser = None
|
|
print(f"Pico nebylo nalezeno: {e}")
|
|
|
|
WIDTH = 128
|
|
HEIGHT = 64
|
|
SCALE = 6
|
|
|
|
root = tk.Tk()
|
|
root.title("128x64 OLED Pokročilý editor s jasem")
|
|
root.configure(bg="#222")
|
|
|
|
canvas_data = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
|
|
current_tool = tk.StringVar(value="pencil")
|
|
brush_size = tk.IntVar(value=1)
|
|
global last_image_path
|
|
last_image_path = None
|
|
|
|
def send_pixel(x, y, val):
|
|
if ser and ser.is_open:
|
|
try:
|
|
msg = f"PX:{x},{y},{val}\n"
|
|
ser.write(msg.encode('utf-8'))
|
|
except Exception:
|
|
pass
|
|
|
|
def send_contrast(val):
|
|
if ser and ser.is_open:
|
|
try:
|
|
msg = f"CONTRAST:{val}\n"
|
|
ser.write(msg.encode('utf-8'))
|
|
except Exception:
|
|
pass
|
|
|
|
def send_clear():
|
|
if ser and ser.is_open:
|
|
try:
|
|
ser.write(b"CLEAR\n")
|
|
except:
|
|
pass
|
|
|
|
def update_ui_pixel(x, y, val):
|
|
color = "white" if val == 1 else "black"
|
|
ui_canvas.create_rectangle(
|
|
x * SCALE, y * SCALE, (x + 1) * SCALE, (y + 1) * SCALE,
|
|
fill=color, outline=color
|
|
)
|
|
|
|
def redraw_gui():
|
|
ui_canvas.delete("all")
|
|
for y in range(HEIGHT):
|
|
for x in range(WIDTH):
|
|
if canvas_data[y][x] == 1:
|
|
update_ui_pixel(x, y, 1)
|
|
|
|
def apply_brush(cx, cy, val):
|
|
size = brush_size.get()
|
|
radius = size // 2
|
|
for dy in range(-radius, radius + 1):
|
|
for dx in range(-radius, radius + 1):
|
|
nx, ny = cx + dx, cy + dy
|
|
if 0 <= nx < WIDTH and 0 <= ny < HEIGHT:
|
|
if canvas_data[ny][nx] != val:
|
|
canvas_data[ny][nx] = val
|
|
update_ui_pixel(nx, ny, val)
|
|
send_pixel(nx, ny, val)
|
|
|
|
def flood_fill(start_x, start_y, fill_val):
|
|
target_val = canvas_data[start_y][start_x]
|
|
if target_val == fill_val:
|
|
return
|
|
|
|
pixels_to_check = [(start_x, start_y)]
|
|
changed_pixels = []
|
|
|
|
while pixels_to_check:
|
|
x, y = pixels_to_check.pop(0)
|
|
if 0 <= x < WIDTH and 0 <= y < HEIGHT:
|
|
if canvas_data[y][x] == target_val:
|
|
canvas_data[y][x] = fill_val
|
|
changed_pixels.append((x, y))
|
|
pixels_to_check.append((x + 1, y))
|
|
pixels_to_check.append((x - 1, y))
|
|
pixels_to_check.append((x, y + 1))
|
|
pixels_to_check.append((x, y - 1))
|
|
|
|
redraw_gui()
|
|
for x, y in changed_pixels:
|
|
send_pixel(x, y, fill_val)
|
|
|
|
def on_canvas_click(event):
|
|
x = event.x // SCALE
|
|
y = event.y // SCALE
|
|
if not (0 <= x < WIDTH and 0 <= y < HEIGHT):
|
|
return
|
|
|
|
tool = current_tool.get()
|
|
if tool == "pencil":
|
|
apply_brush(x, y, 1)
|
|
elif tool == "eraser":
|
|
apply_brush(x, y, 0)
|
|
elif tool == "fill":
|
|
flood_fill(x, y, 1)
|
|
|
|
def on_canvas_drag(event):
|
|
x = event.x // SCALE
|
|
y = event.y // SCALE
|
|
if not (0 <= x < WIDTH and 0 <= y < HEIGHT):
|
|
return
|
|
|
|
tool = current_tool.get()
|
|
if tool == "pencil":
|
|
apply_brush(x, y, 1)
|
|
elif tool == "eraser":
|
|
apply_brush(x, y, 0)
|
|
|
|
def clear_canvas():
|
|
global canvas_data, last_image_path
|
|
last_image_path = None
|
|
canvas_data = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
|
|
ui_canvas.delete("all")
|
|
send_clear()
|
|
|
|
def process_and_send_image():
|
|
global canvas_data
|
|
if not last_image_path:
|
|
return
|
|
|
|
try:
|
|
img = Image.open(last_image_path).convert('L') # Převod na černobílou škálu (grayscale)
|
|
|
|
# Úprava jasu podle posuvníku (hodnota 0.1 až 3.0)
|
|
brightness_factor = img_brightness_slider.get()
|
|
enhancer = ImageEnhance.Brightness(img)
|
|
img = enhancer.enhance(brightness_factor)
|
|
|
|
# Změna velikosti a převod na 1-bit černobílou s prahováním
|
|
img = img.resize((WIDTH, HEIGHT), Image.Resampling.LANCZOS)
|
|
img = img.convert('1')
|
|
|
|
send_clear()
|
|
time.sleep(0.05)
|
|
|
|
canvas_data = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
|
|
pixels = img.load()
|
|
for y in range(HEIGHT):
|
|
for x in range(WIDTH):
|
|
val = 1 if pixels[x, y] > 0 else 0
|
|
canvas_data[y][x] = val
|
|
if val == 1:
|
|
send_pixel(x, y, 1)
|
|
|
|
redraw_gui()
|
|
except Exception as e:
|
|
print(f"Chyba při zpracování obrázku: {e}")
|
|
|
|
def upload_image():
|
|
global last_image_path
|
|
file_path = filedialog.askopenfilename(
|
|
title="Vybrat obrázek",
|
|
filetypes=[("Obrázky", "*.png *.jpg *.jpeg *.bmp *.gif")]
|
|
)
|
|
if not file_path:
|
|
return
|
|
last_image_path = file_path
|
|
process_and_send_image()
|
|
|
|
# --- GUI ROZVRŽENÍ ---
|
|
toolbar = tk.Frame(root, bg="#333", padx=5, pady=5)
|
|
toolbar.pack(fill="x", padx=15, pady=(15, 0))
|
|
|
|
tk.Radiobutton(toolbar, text="Tužka", variable=current_tool, value="pencil", bg="#333", fg="white", selectcolor="#444", indicatoron=0, font=("Arial", 10), padx=8, pady=4).pack(side="left", padx=2)
|
|
tk.Radiobutton(toolbar, text="Guma", variable=current_tool, value="eraser", bg="#333", fg="white", selectcolor="#444", indicatoron=0, font=("Arial", 10), padx=8, pady=4).pack(side="left", padx=2)
|
|
tk.Radiobutton(toolbar, text="Kyblík", variable=current_tool, value="fill", bg="#333", fg="white", selectcolor="#444", indicatoron=0, font=("Arial", 10), padx=8, pady=4).pack(side="left", padx=2)
|
|
|
|
tk.Label(toolbar, text=" | Velikost:", bg="#333", fg="white", font=("Arial", 10)).pack(side="left", padx=5)
|
|
tk.Radiobutton(toolbar, text="1x1", variable=brush_size, value=1, bg="#333", fg="white", selectcolor="#444", indicatoron=0, font=("Arial", 10), padx=5).pack(side="left", padx=1)
|
|
tk.Radiobutton(toolbar, text="3x3", variable=brush_size, value=3, bg="#333", fg="white", selectcolor="#444", indicatoron=0, font=("Arial", 10), padx=5).pack(side="left", padx=1)
|
|
tk.Radiobutton(toolbar, text="5x5", variable=brush_size, value=5, bg="#333", fg="white", selectcolor="#444", indicatoron=0, font=("Arial", 10), padx=5).pack(side="left", padx=1)
|
|
|
|
# Ovládací panel jasu
|
|
brightness_frame = tk.Frame(root, bg="#2a2a2a", padx=10, pady=5)
|
|
brightness_frame.pack(fill="x", padx=15, pady=(5, 0))
|
|
|
|
tk.Label(brightness_frame, text="Jas obrázku:", bg="#2a2a2a", fg="white", font=("Arial", 9)).pack(side="left")
|
|
img_brightness_slider = tk.Scale(brightness_frame, from_=0.2, to=3.0, resolution=0.1, orient="horizontal", length=120, bg="#2a2a2a", fg="white", highlightthickness=0, command=lambda v: process_and_send_image())
|
|
img_brightness_slider.set(1.0)
|
|
img_brightness_slider.pack(side="left", padx=5)
|
|
|
|
tk.Label(brightness_frame, text=" | Jas displeje:", bg="#2a2a2a", fg="white", font=("Arial", 9)).pack(side="left", padx=10)
|
|
display_contrast_slider = tk.Scale(brightness_frame, from_=1, to=255, orient="horizontal", length=120, bg="#2a2a2a", fg="white", highlightthickness=0, command=lambda v: send_contrast(int(v)))
|
|
display_contrast_slider.set(255) # Výchozí plný jas
|
|
display_contrast_slider.pack(side="left", padx=5)
|
|
|
|
ui_canvas = tk.Canvas(root, width=WIDTH * SCALE, height=HEIGHT * SCALE, bg="black", highlightthickness=0)
|
|
ui_canvas.pack(padx=15, pady=10)
|
|
|
|
ui_canvas.bind("<Button-1>", on_canvas_click)
|
|
ui_canvas.bind("<B1-Motion>", on_canvas_drag)
|
|
|
|
bottombar = tk.Frame(root, bg="#222")
|
|
bottombar.pack(fill="x", padx=15, pady=(0, 15))
|
|
|
|
btn_upload = tk.Button(bottombar, text="Nahrát obrázek...", command=upload_image, font=("Arial", 11), bg="#2980b9", fg="white", relief="flat", padx=10, pady=4)
|
|
btn_upload.pack(side="left")
|
|
|
|
btn_clear = tk.Button(bottombar, text="Vymazat vše", command=clear_canvas, font=("Arial", 11), bg="#c0392b", fg="white", relief="flat", padx=10, pady=4)
|
|
btn_clear.pack(side="right")
|
|
|
|
root.mainloop()
|
|
|
|
if ser and ser.is_open:
|
|
ser.close() |