[Tutorial] Drawing With Python (pyautogui)

Ghost

Seasoned Veteran
Blogger
Jun 25, 2009
3,198
185
213
Earth
wubur.com
FP$
2,483
Hey all, today I will be showing you how to draw a perfect rectangle in Paint (or any drawing app/program) with Python!
We will be using 'pyautogui' which allows Python (testing using Python 3.x+) to click, drag the mouse, and type text! You can use this module to automate a lot of things including account registrations, gaming bots, and in this case - drawing.
If you want to see how to draw a square instead of a rectangle, you can modify the code below or check out my how to draw a square in Python tutorial.

Here it is in action!


Python:
import pyautogui
distance = 50 # distance per line
def draw():
    pyautogui.click();
    pyautogui.dragRel(distance*2, 0, duration=0.5) # right
    pyautogui.dragRel(0, distance, duration=0.5) #down
    pyautogui.dragRel(-distance*2, 0, duration=0.5) #left
    pyautogui.dragRel(0, -distance, duration=0.5) #up
    continuescript = input("Press any key to continue or type 'exit' to close.")
    if(continuescript != 'exit'):
        draw()
    else:
        print("Thank you for trying this out!")

answer = input("Are you ready to draw? Press any key to continue")
draw()
Let's go through the code!
For one, you will need to make sure pyautogui is installed on your system, so you can use pip install pyautogui in your command line.
The first line is: import pyautogui to make sure our script can use it.

distance = 50 # distance per line

This allows us to declare how long each line of our rectangle should be!

def draw():

We define our function as draw() and then have our code inside of the func.

pyautogui.click();
pyautogui.dragRel(distance*2, 0, duration=0.5) # right
pyautogui.dragRel(0, distance, duration=0.5) [HASH=17389]#down[/HASH]
pyautogui.dragRel(-distance*2, 0, duration=0.5) [HASH=17390]#left[/HASH]
pyautogui.dragRel(0, -distance, duration=0.5) [HASH=17391]#up[/HASH]

In this segment we are saying "click wherever the user's mouse is placed currently". This starts off our line in Paint for example.
Then we use dragRel() four times to drag our lines perfectly straight.
It works like this... dragRel(distanceX, distanceY, duration)
So, in the first line we are saying we want pyautogui to click the spot where mouse is hovering over, and then drag the mouse "distance"*2 pixels (100) on the X axis - so we're going right.
We say 0 for distanceY because we do not want the line to go up or down - just to the right.
We tell the script to do this in 0.5 seconds so it's quite quick, but we can make this slower or faster as well.
In line 2, we have a distanceX of 0 which tells the script to not move left or right. DistanceY is distance variable (50 pixels), so it knows to go down 50px. Duration stays the same for all.
Then we go left by saying "go on the X axis -100 (-distance * 2) pixels, and obviously do not go up or down".
Finally we finish up by telling the mouse to move up by going -50 pixels on the Y axis to finish our rectangle.

The end result is a perfect rectangle.

continuescript = input("Press any key to continue or type 'exit' to close.")
if(continuescript != 'exit'):
draw()
else:
print("Thank you for trying this out!")

This last part of our function allows the user to draw another rectangle right after by pressing any key. If they type exit it will thank them for trying out the python script and stop running.
This is so you can create a bunch of perfect rectangle if you want.

answer = input("Are you ready to draw? Press any key to continue")
draw()

Even though our draw() function has the ability to continue, we did not initiate the actual function so at the bottom of my Python file I have a simple input question to ask the user if they are ready to start.
They can hover their mouse over the starting point of their rectangle & press enter in command line. The mouse will then click where their mouse is hovering (in Paint/Photoshop/whatever) and start drawing!