19.06.2018

Draw your initials in Paint with PyAutoGUI!

Draw your initials in Paint with PyAutoGUI! - Do you like drawing your initials? Did you know you can do it without much labor using Python? Even better that you can do it in Paint? Read on to find out how.

First, install PyAutoGUI. The code begins with starting Paint (Admit it, Paint still rocks). We start Paint with the os module, by defining our paint_path to be "C:\WINDOWS\system32\mspaint.exe". We then have to wait a bit, in order to have time to start up Paint. Now we are ready to start drawing on the screen. We loop the number of times that we want to draw our initials.

I used my initials, COS. The draw_my_initials function accepts the variables text_size and space_size. Text_size determines the size of our text and space_size how much space we want between the letters. We use PyAutoGUI's methods mouseDown and Mouseup to simulate mouse clicks. The MoveRel method takes the relative offset from your mouse pointers current position and moves it the specified X and Y amount. Below is a picture of how it looks like after running once. Make sure that you have a brush or similar selected in Paint. You can see how it looks like on Youtube also.

#!python3

import pyautogui
import os
import time

def draw_my_initials(text_size, space_size):
    """
    Draws the initials COS onto the screen.
    """
    # Start drawing
    pyautogui.mouseDown()
    # Draw the letter C
    pyautogui.moveRel(text_size, 0)
    pyautogui.moveRel(0, -text_size)
    pyautogui.moveRel(-text_size, 0)
    # Make a space
    pyautogui.mouseUp()
    pyautogui.moveRel(space_size, 0)
    # Draw the letter O
    pyautogui.mouseDown()
    pyautogui.moveRel(0, text_size)
    pyautogui.moveRel(-text_size, 0)
    pyautogui.moveRel(0, -text_size)
    pyautogui.moveRel(text_size, 0)
    pyautogui.mouseUp()
    pyautogui.moveRel(-text_size, 0)
    # Make a space
    pyautogui.moveRel(space_size, 0)
    # Draw the letter S
    pyautogui.mouseDown()
    pyautogui.moveRel(-text_size, 0)
    pyautogui.moveRel(0, text_size/2)
    pyautogui.moveRel(text_size, 0)
    pyautogui.moveRel(0, text_size/2)
    pyautogui.moveRel(-text_size, 0)
    # All done, release mouse button
    pyautogui.mouseUp()
      
if __name__ == "__main__":
    text_size = -50
    space_size = 20
    # open paint
    paint_path = "C:\WINDOWS\system32\mspaint.exe"
    os.startfile(paint_path)
    # wait for paint to start
    time.sleep(2)
    # Start drawing
    for i in range(4): # Amount of initials
        draw_my_initials(text_size, space_size)
        pyautogui.moveRel(-text_size, text_size - space_size)

Happy coding, and please tell me if you did draw your initials as well!

Leave a Reply

Your email address will not be published.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram