Я делаю пакет Pixel Art с подушкой (PIL) и Tkinter.До сих пор я установил код, который рисует непрерывную линию на пути движения мыши.Тем не менее, линия не гладкая.
Я пытался нарисовать пять пикселей x-2, x-1, x, x + 1, x + 2, но все же линия не гладкая.
импорт tkinter as tk из PIL import Image
def tinker(img, pos, color):
pixels = img.load()
x = pos[0]
y = pos[1]
if (x > 3 and y > 3) and (x < 253 and y < 253):
pixels[x-2, y] = color
pixels[x - 1, y] = color
pixels[x , y] = color
pixels[x + 1, y] = color
pixels[x + 2, y] = color
elif x < 3 and y < 3:
x = 3
y = 3
elif x > 253 and y > 253:
x = 253
y = 253
img.save("image.png")
root = tk.Tk()
img = Image.new("RGB", (256, 256), color="white")
img.save("image.png")
openimg = tk.PhotoImage(file="image.png")
panel = tk.Label(root, image=openimg)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
x, y = e.x, e.y
print(f"({x}, {y})")
tinker(img, pos=(x, y), color=(0, 0, 0))
img2 = tk.PhotoImage(file="image.png")
panel.configure(image=img2)
panel.image = img2
root.bind("<B1-Motion>", callback)
root.mainloop()
Я ожидал гладких линий, а-ля Photoshop, GIMP и Paint.net.