How I Discovered PyAutoGUI and Why It’s Awesome

Hi everyone! This is my first blog, and I’m super excited to share something cool I recently learned: PyAutoGUI. If you’ve ever wished your computer could do repetitive tasks on its own, then this is the magic wand you’ve been looking for!
Now, let me tell you what PyAutoGUI is, how I got started with it, and why I think it’s amazing.
What is PyAutoGUI?
In simple terms, PyAutoGUI is a Python library that lets you control your mouse and keyboard using code. Imagine being able to automate things like clicking buttons, typing, or even taking screenshots—without moving a finger! It’s like having a robot work on your computer while you chill.
How I Started
I came across PyAutoGUI when I was searching for ways to automate some boring tasks on my computer. Taking screenshots automatically, things like filling out forms or clicking on the same button over and over again. I thought, "There must be a smarter way to do this!"
Here’s how I got started:
First, I installed PyAutoGUI using this simple command:
pip install pyautogui(If you’re new to Python, just type this in your terminal or command prompt!)
Then, I wrote my very first automation script. It was so satisfying to see my mouse move on its own!
What Can PyAutoGUI Do?
Here are a few basic things you can do with PyAutoGUI:
1. Move the Mouse
You can move the mouse to any position on your screen with a single line of code:
import pyautogui
pyautogui.moveTo(100, 200, duration=1)
This moves the mouse to the coordinates (100, 200) in 1 second.
2. Click Anywhere
Want to click a specific button? Just tell PyAutoGUI where to click:
pyautogui.click(100, 200)
3. Type Automatically
Typing long messages is tiring. Let PyAutoGUI do it for you:
pyautogui.write("Hello, this is my first blog", interval=0.1)
This types the text with a slight delay between letters, so it looks natural.
4. Take Screenshots
You can even take screenshots and save them:
creenshot = pyautogui.screenshot()
screenshot.save('my_screenshot.png')
A Fun Example
Here’s a simple script I wrote to open Google and search for something automatically:
import pyautogui
import time
# Open Chrome
pyautogui.press('win')
pyautogui.write('chrome')
pyautogui.press('enter')
time.sleep(2)
# Search on Google
pyautogui.write('Learn PyAutoGUI')
pyautogui.press('enter')
Watching it happen felt like magic!
The Projects I made Using PyAutoGUI
#1 Auto Brightness controller for computers:
This python script helps me to get rid of adjusting the brightness of my screen again and again for bright and dark screens, specially while watching lectures.
This script basically takes screenshots continuously and analyzes the color of the screen and for some range and it varies between bright and dark screen. And that’s all it reduces the brightness when a bright screen appears and vice versa.
import time
import threading
import pyautogui
import screen_brightness_control as sbc
# Function to adjust screen brightness based on screen content
def adjust_brightness(min_brightness, max_brightness):
while True:
# Capture a screenshot
screenshot = pyautogui.screenshot()
# Calculate average brightness of the screenshot
total_brightness = sum(screenshot.convert("L").getdata())
average_brightness = total_brightness / (screenshot.width * screenshot.height)
custom_brightness = map_value(average_brightness, 0, 255, min_brightness, max_brightness)
# Adjust brightness based on custom brightness range
sbc.set_brightness(int(custom_brightness))
time.sleep(1)
# Function to map a value from one range to another
def map_value(value, from_min, from_max, to_min, to_max):
return (value - from_min) * (to_max - to_min) / (from_max - from_min) + to_min
# Main function
def main():
max_brightness = -10
min_brightness = 25
# Start a thread to continuously adjust brightness
brightness_thread = threading.Thread(target=adjust_brightness, args=(min_brightness, max_brightness))
brightness_thread.daemon = True # Daemonize the thread so it exits when the main thread exits
brightness_thread.start()
while True:
time.sleep(1) # Add a sleep to prevent high CPU usage
if __name__ == "__main__":
main()
How It Works
Screenshot Analysis: The program captures a screenshot of your screen using PyAutoGUI.
Brightness Calculation: It calculates the average brightness of the screen by converting the image to grayscale and summing up the pixel values.
Brightness Mapping: The average brightness is mapped to a custom brightness range (minimum to maximum) using the
map_valuefunction.Adjusting Screen Brightness: The program adjusts the screen brightness in real time using the
screen_brightness_controllibrary based on the calculated value.Continuous Monitoring: The process runs in a separate thread, ensuring that brightness adjusts dynamically without blocking other tasks.
How to Run
Install Required Libraries: Ensure the required libraries are installed:
pip install pyautogui screen-brightness-controlSave the Code: Copy the code into a Python file, for example,
adaptive_brightness.py.Run the Script: Execute the script using Python:
python adaptive_brightness.pyWatch the Magic: The program will start adjusting your screen brightness automatically based on your screen content.
Note
The program runs in the background, so your screen brightness will adapt continuously as long as the script is running.
Make sure your system supports the
screen-brightness-controllibrary for adjusting brightness.
#2 AutoSearch for Earning credits in Edge
import os
import pyautogui
import time
import random
import string
# Function to generate a random 4-letter word
def generate_random_word(length=4):
return ''.join(random.choices(string.ascii_lowercase, k=length))
# Open Microsoft Edge
application_path = "C:\\Users\\Public\\Desktop\\Microsoft Edge.lnk"# This is my file structure, adjust it accordingly...
os.startfile(application_path)
# Wait for the browser to open
time.sleep(2) # Adjust this delay if necessary
iterations = 0
# Perform searches while Edge is running
print("Automation started. Close Edge to stop.")
while iterations <= 31:
iterations += 1
random_word = generate_random_word() # Generate a random word
pyautogui.typewrite(f"{str(iterations)} : {random_word}")
pyautogui.press('enter')
time.sleep(8) # Wait for search results to load
print("Edge closed. Automation stopped.")
How It Works
Opens Microsoft Edge: The script uses the
os.startfilefunction to open Microsoft Edge from a shortcut.Generates Random Words: A random 4-letter word is created using Python’s
randomandstringmodules.Types and Searches: PyAutoGUI automates typing these words into Edge and pressing Enter to perform the search.
Repeats for 31 Iterations: The script repeats the process for 31 searches, with an 8-second delay between each search to let the results load.
How to Run It
Install PyAutoGUI if you haven’t already:
pip install pyautoguiSave the code in a
.pyfile and make sure the path to Microsoft Edge is correct in theapplication_pathvariable.Run the script, and watch the magic happen!
A Quick Safety Tip
If something goes wrong, just move your mouse to the top-left corner of your screen. This will stop PyAutoGUI instantly. (It’s called the failsafe feature.)
Why I Love PyAutoGUI
For someone like me, who’s just starting with Python, PyAutoGUI felt very beginner-friendly. It’s powerful yet easy to understand. Plus, it made me feel like I was doing something really advanced, even though the code was so simple!
For More Details Visit the Official PyAutoGUI Documentation
Thanks For Reading!
And that’s it! Hope you found these project helpful and fun to try out! If you have any questions or ideas to improve it, feel free to share them.
Happy coding! 😊