Skip to content

Basic Paint Application

Abstract

Build a feature-rich digital painting application with a graphical interface that includes color palettes, brush size controls, eraser tools, canvas background customization, and image saving capabilities. This project demonstrates advanced GUI development, event handling, and image processing.

Prerequisites

  • Basic understanding of Python syntax
  • Knowledge of Tkinter for GUI development
  • Familiarity with event-driven programming
  • Understanding of coordinate systems and graphics
  • Basic knowledge of image processing concepts

Getting Started

  1. Install Required Dependencies

    pip install pillow
    # tkinter comes built-in with Python
    pip install pillow
    # tkinter comes built-in with Python
  2. Run the Paint Application

    python paint.py
    python paint.py
  3. Start Creating Art

    • Select colors from the color palette
    • Adjust brush size with the scale control
    • Use the eraser tool for corrections
    • Save your artwork when finished

Code Explanation

Application Architecture

paint.py
class Paint:
    def __init__(self, root):
        self.root = root
        self.root.title("Paint")
        self.root.geometry("800x520")
        self.pen_color = "black"
        self.eraser_color = "white"
paint.py
class Paint:
    def __init__(self, root):
        self.root = root
        self.root.title("Paint")
        self.root.geometry("800x520")
        self.pen_color = "black"
        self.eraser_color = "white"

Implements a class-based structure for better organization and state management of the painting application.

Event-Driven Drawing

paint.py
self.root.bind("<B1-Motion>", self.paint)
self.root.bind("<ButtonRelease-1>", self.reset)
 
def paint(self, event):
    x1, y1 = (event.x-2), (event.y-2)
    x2, y2 = (event.x+2), (event.y+2)
    self.canvas.create_oval(x1, y1, x2, y2, fill=self.pen_color, 
                           outline=self.pen_color, width=self.canvas_width.get())
paint.py
self.root.bind("<B1-Motion>", self.paint)
self.root.bind("<ButtonRelease-1>", self.reset)
 
def paint(self, event):
    x1, y1 = (event.x-2), (event.y-2)
    x2, y2 = (event.x+2), (event.y+2)
    self.canvas.create_oval(x1, y1, x2, y2, fill=self.pen_color, 
                           outline=self.pen_color, width=self.canvas_width.get())

Captures mouse movement and button events to create continuous drawing by placing small overlapping ovals.

Dynamic Color Palette

paint.py
colors = ["#ff0000", "#ff4000", "#ff8000", "#ffbf00", "#ffff00", ...]
i=j=0
for color in colors:
    Button(self.color_frame, bg=color, bd=2, relief=RIDGE, width=3, 
           command=lambda col=color:self.select_color(col)).grid(row=i, column=j)
paint.py
colors = ["#ff0000", "#ff4000", "#ff8000", "#ffbf00", "#ffff00", ...]
i=j=0
for color in colors:
    Button(self.color_frame, bg=color, bd=2, relief=RIDGE, width=3, 
           command=lambda col=color:self.select_color(col)).grid(row=i, column=j)

Creates a comprehensive color palette with buttons arranged in a grid layout for easy color selection.

Brush Size Control

paint.py
self.canvas_width = Scale(self.root, from_=1, to=100, orient=VERTICAL)
self.canvas_width.set(1)
paint.py
self.canvas_width = Scale(self.root, from_=1, to=100, orient=VERTICAL)
self.canvas_width.set(1)

Provides a vertical scale widget for real-time brush size adjustment from 1 to 100 pixels.

Canvas Background Customization

paint.py
def canvas_color(self):
    color = colorchooser.askcolor(title="Select Color")
    self.canvas.configure(background=color[1])
    self.eraser_color = color[1]
paint.py
def canvas_color(self):
    color = colorchooser.askcolor(title="Select Color")
    self.canvas.configure(background=color[1])
    self.eraser_color = color[1]

Allows users to change the canvas background color and automatically updates the eraser color to match.

Image Saving with Screen Capture

paint.py
def save_paint(self):
    filename = filedialog.asksaveasfilename(defaultextension=".jpg")
    x = self.root.winfo_rootx() + self.canvas.winfo_x()
    y = self.root.winfo_rooty() + self.canvas.winfo_y()
    x1 = x + self.canvas.winfo_width()
    y1 = y + self.canvas.winfo_height()
    PIL.ImageGrab.grab().crop((x,y,x1,y1)).save(filename)
paint.py
def save_paint(self):
    filename = filedialog.asksaveasfilename(defaultextension=".jpg")
    x = self.root.winfo_rootx() + self.canvas.winfo_x()
    y = self.root.winfo_rooty() + self.canvas.winfo_y()
    x1 = x + self.canvas.winfo_width()
    y1 = y + self.canvas.winfo_height()
    PIL.ImageGrab.grab().crop((x,y,x1,y1)).save(filename)

Implements screen capture technology to save the canvas content as an image file.

Features

  • Comprehensive Color Palette: 27 predefined colors in an organized grid
  • Variable Brush Size: Adjustable brush width from 1 to 100 pixels
  • Eraser Tool: Switch between drawing and erasing modes
  • Canvas Customization: Change background color with color picker
  • Clear Function: Reset the entire canvas instantly
  • Image Saving: Save artwork as JPEG files
  • Responsive Drawing: Smooth, continuous brush strokes
  • Professional Layout: Organized toolbar and spacious canvas area

Next Steps

Enhancements

  • Add different brush shapes (square, triangle, spray)
  • Implement undo/redo functionality
  • Create layers support for complex artwork
  • Add text tool for adding labels
  • Implement zoom in/out functionality
  • Add image loading capabilities
  • Create brush opacity controls
  • Add selection tools (rectangle, lasso)

Learning Extensions

  • Study advanced image processing with PIL/Pillow
  • Explore vector graphics programming
  • Learn about graphics algorithms and optimization
  • Practice with custom widget development
  • Understand color theory and digital art concepts
  • Explore tablet/stylus input integration

Educational Value

This project teaches:

  • Advanced GUI Development: Creating complex interfaces with multiple widget types
  • Event Handling: Managing mouse events for interactive drawing
  • Image Processing: Working with PIL for screen capture and image manipulation
  • Coordinate Systems: Understanding pixel-based positioning and drawing
  • State Management: Maintaining application state across user interactions
  • File Operations: Implementing save/load functionality with file dialogs
  • Color Theory: Working with color systems and user color selection
  • Object-Oriented Design: Structuring applications using classes and methods

Perfect for understanding graphics programming, user interface design, and image processing concepts in Python.

Was this page helpful?

Let us know how we did