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
PyQt5 - Create Paint Application
There are so many options provided by Python to develop GUI application and PyQt5 is one of them. PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library
5 min read
Python | Creating a Simple Drawing App in kivy
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Drawing App: In this w
4 min read
Build GUI Application Pencil Sketch from Photo in Python
In this article, we will cover how to convert images to watercolor sketches and pencil sketch Linux. Sketching and painting are used by artists of many disciplines to preserve ideas, recollections, and thoughts. Experiencing art from painting and sculpting to visiting an art museumâprovides a variet
5 min read
Matplotlib.animation.FuncAnimation class in Python
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.animation.FuncAnimation The matplotlib.animation.FuncAnimation class is used
4 min read
Matplotlib.artist.Artist.draw() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The Artist class contains Abstract base class for objects that render into a FigureCanvas. All visible elements in a figure are subclasses of Artist. matplotlib.artist.Artist.draw() method The draw() me
2 min read
Arcade inbuilt functions to draw point(s) in Python3
The Arcade library is a modern Python Module used widely for developing 2D video games with compelling graphics and sound. Arcade is an object-oriented library. It can be installed like any other Python Package. In this article, we will learn what are the arcade inbuilt functions to draw point. Arca
3 min read
Wand path_line() function in Python
path_line() is a function specially introduced for paths. path_line() draws a line from a destination point to the point we want the line we end to. It takes only end point as argument. Syntax : wand.drawing.path_line(to, relative) Parameters: ParameterInput TypeDescriptiontosequence or (numbers.Rea
1 min read
Wand oil_paint() function - Python
The oil_paint() function is an inbuilt function in the Python Wand ImageMagick library which is used to simulate an oil painting by replace each pixel with most frequent surrounding color. Syntax: oil_paint(radius, sigma) Parameters: This function accepts three parameters as mentioned above and defi
2 min read
Python PIL | ImageDraw.Draw.line()
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web u
2 min read
Matplotlib.axes.Axes.draw_artist() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read