Drawing Automation with Python using PyQt, OpenCV, and PyAutoGUI

Posted by


In this tutorial, we will explore how to automate drawing using a combination of Python libraries such as PyQt, OpenCV, PyAutoGUI, and Paint Automation.

Before we dive into the details, let’s first understand what each library does:

  1. PyQt: PyQt is a set of Python bindings for the Qt application framework. Qt is a cross-platform application framework that is widely used for developing graphical user interfaces (GUIs). PyQt allows you to create rich and interactive GUIs using Python.

  2. OpenCV: OpenCV is an open-source computer vision and machine learning software library. It provides various functions and tools for image processing, such as image manipulation, object detection, and recognition.

  3. PyAutoGUI: PyAutoGUI is a Python module that can be used to control the mouse and keyboard actions on a computer. It allows you to automate repetitive tasks that involve clicking, typing, or moving the mouse cursor.

  4. Paint Automation: Paint Automation refers to automating the process of drawing or painting using software such as Microsoft Paint. By automating this process, we can create intricate and complex drawings with minimal effort.

Now that we have an understanding of the libraries involved, let’s go through the steps to automate drawing using Python:

Step 1: Install the required libraries
Before we start coding, we need to install the required libraries. You can do this by running the following commands in your terminal:

pip install PyQt5
pip install opencv-python
pip install pyautogui

Step 2: Create a GUI using PyQt
To create a GUI for our drawing application, we will use the PyQt library. You can create a simple window with a canvas where you can draw using the following code:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt

class DrawingApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Drawing App")
        self.setGeometry(100, 100, 800, 600)

        self.canvas = QWidget(self)
        self.canvas.setGeometry(0, 0, 800, 600)

        self.image = QImage(800, 600, QImage.Format_RGB32)
        self.image.fill(Qt.white)

        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawImage(0, 0, self.image)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = DrawingApp()
    sys.exit(app.exec_())

Step 3: Implement drawing functions
Next, we need to implement drawing functions that allow us to draw on the canvas. We can do this by capturing mouse events and updating the image accordingly:

class DrawingApp(QMainWindow):
    def __init__(self):
        ...
        self.last_pos = None
        self.pen_color = Qt.black
        self.pen_width = 3

    def mousePressEvent(self, event):
        self.last_pos = event.pos()

    def mouseMoveEvent(self, event):
        painter = QPainter(self.image)
        pen = QPen(self.pen_color, self.pen_width, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
        painter.setPen(pen)
        painter.drawLine(self.last_pos, event.pos())
        self.last_pos = event.pos()
        self.update()

    def mouseReleaseEvent(self, event):
        self.last_pos = None

Step 4: Add controls to change pen color and width
To give the user control over the pen color and width, we can add widgets to the GUI that allow them to change these attributes:

from PyQt5.QtWidgets import QColorDialog, QPushButton, QSlider

class DrawingApp(QMainWindow):
    def __init__(self):
        ...
        color_btn = QPushButton("Choose Color", self)
        color_btn.clicked.connect(self.choose_color)
        color_btn.setGeometry(10, 10, 100, 40)

        self.slider = QSlider(Qt.Horizontal, self)
        self.slider.setValue(3)
        self.slider.setMinimum(1)
        self.slider.setMaximum(10)
        self.slider.setGeometry(120, 10, 200, 40)
        self.slider.valueChanged.connect(self.change_width)

    def choose_color(self):
        color = QColorDialog.getColor()
        if color.isValid():
            self.pen_color = color

    def change_width(self):
        self.pen_width = self.slider.value()

Step 5: Automate drawing using PyAutoGUI
Now that we have created a GUI for drawing, we can automate the process using PyAutoGUI. PyAutoGUI allows us to control the mouse and keyboard actions on the computer, so we can simulate drawing on the canvas automatically:

import pyautogui
import time

def automate_drawing():
    pyautogui.click(100, 100)  # Click on the canvas to start drawing
    pyautogui.moveTo(200, 200)  # Move the mouse to draw a line
    pyautogui.dragTo(300, 300, duration=1)  # Drag the mouse to draw a line

    time.sleep(1)
    pyautogui.hotkey('ctrl', 's')  # Save the drawing

Step 6: Automate drawing using Paint Automation
If you want to automate drawing on a specific software like Microsoft Paint, you can use Paint Automation techniques. This involves taking screenshots of the canvas, analyzing the colors and shapes, and generating a script to replicate the drawing. Here is an example of automating drawing using Paint:

import cv2

def automate_paint_drawing():
    # Take a screenshot of the canvas
    screenshot = pyautogui.screenshot()
    screenshot.save("canvas.png")

    # Process the screenshot using OpenCV
    image = cv2.imread("canvas.png")
    # Apply image processing techniques to analyze the colors and shapes

    # Generate a script to replicate the drawing in Paint
    script = "draw_line(200, 200, 300, 300)n"
    script += "draw_rectangle(400, 400, 500, 500)n"

    # Execute the script in Paint using PyAutoGUI
    for line in script.split('n'):
        if "draw_line" in line:
            coordinates = line.split('(')[1].split(')')[0].split(',')
            x1, y1, x2, y2 = [int(coord) for coord in coordinates]
            pyautogui.moveTo(x1, y1)
            pyautogui.dragTo(x2, y2, duration=1)

        if "draw_rectangle" in line:
            coordinates = line.split('(')[1].split(')')[0].split(',')
            x1, y1, x2, y2 = [int(coord) for coord in coordinates]
            pyautogui.moveTo(x1, y1)
            pyautogui.dragTo(x2, y1)
            pyautogui.dragTo(x2, y2)
            pyautogui.dragTo(x1, y2)
            pyautogui.dragTo(x1, y1)

    time.sleep(1)
    pyautogui.hotkey('ctrl', 's')  # Save the drawing

That’s it! You have successfully automated drawing using Python with the help of PyQt, OpenCV, PyAutoGUI, and Paint Automation. Feel free to customize and expand upon this tutorial to create your own automated drawing application.

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@WillFourTwenty
2 hours ago

Useless program you’d have to download

1
0
Would love your thoughts, please comment.x
()
x