Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hello there
#1
Hello there,
Does anybody know of a tool, library, or whatever on Python that allows you to create/access a user interface where the user could modify an image by drawing over it (something like Paint)?

Any help would be much appreciated!
Reply
#2
This was created by Google AI (tested and works):

import tkinter as tk

class PaintApp:
    def __init__(self, master):
        self.master = master
        master.title("Simple Paint")

        self.canvas_width = 600
        self.canvas_height = 400
        self.canvas = tk.Canvas(master, width=self.canvas_width, height=self.canvas_height, bg="white")
        self.canvas.pack()

        self.pen_color = "black"
        self.pen_size = 2
        self.drawing_tool = "pen"
        self.last_x, self.last_y = None, None

        self.canvas.bind("<B1-Motion>", self.draw)
        self.canvas.bind("<ButtonRelease-1>", self.reset_coords)

        self.create_controls()

    def create_controls(self):
        controls_frame = tk.Frame(self.master)
        controls_frame.pack()

        color_label = tk.Label(controls_frame, text="Color:")
        color_label.pack(side=tk.LEFT)

        colors = ["black", "red", "blue", "green", "yellow"]
        for color in colors:
            color_button = tk.Button(controls_frame, bg=color, width=2, command=lambda c=color: self.set_color(c))
            color_button.pack(side=tk.LEFT)

        size_label = tk.Label(controls_frame, text="Size:")
        size_label.pack(side=tk.LEFT)

        sizes = [2, 5, 8, 10]
        for size in sizes:
            size_button = tk.Button(controls_frame, text=str(size), command=lambda s=size: self.set_size(s))
            size_button.pack(side=tk.LEFT)
    
    def draw(self, event):
         x, y = event.x, event.y
         if self.last_x is not None and self.last_y is not None:
            if self.drawing_tool == "pen":
                self.canvas.create_line(self.last_x, self.last_y, x, y, fill=self.pen_color, width=self.pen_size, capstyle=tk.ROUND, smooth=tk.TRUE)
         self.last_x, self.last_y = x, y

    def reset_coords(self, event):
        self.last_x, self.last_y = None, None
        
    def set_color(self, color):
        self.pen_color = color

    def set_size(self, size):
        self.pen_size = size

root = tk.Tk()
app = PaintApp(root)
root.mainloop()
Reply
#3
You can use Tkinter with PIL (Pillow) to create a simple drawing tool in Python, or go for PyQt for a more advanced UI. If you're looking for something pre-built, OpenCV also allows basic drawing functionalities with mouse events. Check out Tkinter's Canvas for a lightweight option!
buran write Mar-09-2025, 09:16 AM:
Link removed
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020