Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Draw a perfect square in Paint with Python

Ghost

Platinum Coder
Hey all, today I will be showing you how to draw a perfect square 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 rectangle instead of a square, you can modify the code below or check out my how to draw a rectangle in Python tutorial.

Here it is in action!
ONl04Wn.gif


Python:
import pyautogui
distance = 50 # distance per line
def draw():
    pyautogui.click();
    pyautogui.dragRel(distance, 0, duration=0.5) # right
    pyautogui.dragRel(0, distance, duration=0.5) #down
    pyautogui.dragRel(-distance, 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 square should be!

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

pyautogui.click(); pyautogui.dragRel(distance, 0, duration=0.5) # right pyautogui.dragRel(0, distance, duration=0.5) #down pyautogui.dragRel(-distance, 0, duration=0.5) #left pyautogui.dragRel(0, -distance, duration=0.5) #up
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" pixels (50) 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 -50 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.

The end result is a perfect square.

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 square 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 squares 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 square & press enter in command line. The mouse will then click where their mouse is hovering (in Paint/Photoshop/whatever) and start drawing!
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom