Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
PyQt5 - Create Paint Application
Next article icon

Python Automation Drawing In Paint Application

Last Updated : 22 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 spiral

Example 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
                      
                       

square

Example 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



Next Article
PyQt5 - Create Paint Application

Z

zehraina
Improve
Article Tags :
  • Python
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences