Python Automation Drawing In Paint Application
Last Updated :
22 Jun, 2022
In this article, we are going to know how to automate paint applications using Python.
Introduction
To automate the Paint application in Python, we'll use a Python library PyAutoGUI which lets the user control keyboard and mouse movements and hence automate interaction with other applications on the system using Python. This library has various methods that will come in handy. It is used to stimulate mouse cursor moves and keyboard button presses. We can automate several applications using this module. It is mostly used for UI testing. This module has a lot of useful methods that make our tasks easier.
Prerequisites
- We must have basic knowledge of python.
- pyautogui module should be installed on the system.
Installation
To install the PyAutoGUI module. Type the following in the command prompt.
pip install pyautogui
We'll be using two methods of the PyAutoGUI module.
click()
This method is used to send a virtual mouse click to the computer and by default, it uses the left mouse button.
Syntax: pyautogui.click()
Parameters: It takes three parameters. x - coordinate, y - coordinate and duration which is optional.
Return type: Doesn't return anything, simply performs a click.
For example, pyautogui.click(10, 5, button='left') will click the left mouse button at the coordinates (10, 5).
dragRel()
This method is used to drag the mouse. By dragging, we mean holding the mouse button and dragging the cursor. It also takes 3 arguments; x - coordinate, y - coordinate and button (this is optional).
Syntax: pyautogui.dragRel()
Parameters: It takes three parameters. x - coordinate, y - coordinate and duration which is optional.
Return type: Returns nothing, just drags the mouse cursor to the specified location in the form of parameters.
For example, pyautogui.dragRel(20, 40, 5) will drag the mouse cursor to the specified location in the form of the parameter.
Example 1:
In this example, We are going to automate the drawing of square spiral in paint application without using mouse. To implement this we are going to use two methods of pyautogui module which are click() and dragRel() that are used inside the while loop to make the square spiral. click() is used to click inside the canvas of paint application and dragRel() is used to move the mouse cursor to make the spiral by incrementing and decrementing the values of parameters of dragRel() method.
Python3
# importing pyautogui
import pyautogui
pyautogui.click()
# initialising a variable distance
distance = 200
while (distance):
# moves the cursor to the right
pyautogui.dragRel(distance, 0, duration=0.2)
distance = distance - 5
# move the cursor down
pyautogui.dragRel(0, distance, duration=0.2)
# move the cursor to the left
pyautogui.dragRel(-distance, 0, duration=0.2)
distance = distance - 5
# move the cursor up
pyautogui.dragRel(0, -distance, duration=0.2)
Output:
Square spiralExample 2:
In this example, We are going to automate the drawing of squares in the paint application without using a mouse. we are going to use sleep() method of the time module for delaying the process of drawing squares. After that click() method is used to press click on the canvas of paint and then dragRel() method is used to draw a square.
Python3
# importing pyautogui module
import pyautogui
# importing time module
import time
# Make a delay of 2 sec
time.sleep(2)
pyautogui.click()
l = 200 # initialising variable l
a = 4 # initialising variable a
pyautogui.dragRel(200, 0, 0.1)
a -= 1
pyautogui.dragRel(0, 200, 0.1)
a -= 1
pyautogui.dragRel(-200, 0, 0.1)
a -= 1
pyautogui.dragRel(0, -200, 0.1)
a -= 1
squareExample 3:
In this example, We are going to automate the drawing of the hut in the paint application without using a mouse. Again we are using sleep() function to delay the process and then firstly, draw the rectangle after that draw the triangle and at last draw the rest of the part of the hut.
Python3
# importing required modules
import pyautogui
import time
# Take a break of 5 sec
time.sleep(5)
pyautogui.click() # using .click() method to click
l = 200
a = 4
x, y = pyautogui.position()
# making a square first
pyautogui.dragRel(200, 0, 0.2)
x1, y1 = pyautogui.position()
a -= 1
pyautogui.dragRel(0, 200, 0.2)
a -= 1
pyautogui.dragRel(-200, 0, 0.2)
a -= 1
pyautogui.dragRel(0, -200, 0.2)
a -= 1
# making a triangle over the square
pyautogui.click(x, y)
pyautogui.dragRel(100, -100, 0.2)
pyautogui.click(x1, y1)
pyautogui.dragRel(-100, -100, 0.2)
# making rest of the body of the hut
pyautogui.dragRel(350, 0, 0.2)
pyautogui.dragRel(0, 300, 0.2)
pyautogui.dragRel(-290, 0, 0.2)
pyautogui.click(x1, y1)
pyautogui.dragRel(250, 0, 0.2)
Output:
Hut
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read