How to change the Brightness in Windows using a Python Script

How to change the Brightness in Windows using a Python Script

Overview

This article explains how to write a script to change the brightness on a Windows computer to any specified number within the range from 0 to 100.

Purpose

The purpose of this script is to change the brightness on a Windows machine to any specified number on the brightness scale. The brightness scale ranges from 0 to 100, where 0 is the lowest brightness and 100 is the maximum brightness. The brightness can be changed in three ways: in the settings, using the keyboard brightness keys or by using the Powershell or Command Prompt. To change the brightness in settings, it can be adjusted by dragging a button on a toggle bar which can be difficult to adjust to a certain number. The brightness keys always increase or decrease the brightness by increments of 10. The PowerShell command accurately changes the brightness, but instead of manually inputting the command each time, a script can be created and run that uses that same command while asking for any number on the brightness scale and immediately changing it.

Materials

  • Windows

  • Python

  • Powershell, Command Prompt or preferred IDE

Procedure: Creating the Script

Since this Python Script will be interacting with Windows by changing its brightness, the os module must be imported to run Windows commands in Python.

#!/usr/bin/env python3

import os

In the script, this Windows command down below will be incorporated to change the brightness is:

# Powershell Command
powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,...)

This command works both in Powershell and Command Prompt.

In the parenthesis, (1, ...) where "..." takes an integer value from the range of 0 to 100 to change the brightness. In the Python script, a function that contains a while loop to execute a try and except block can be incorporated to always ask for an integer value.

The variable "brightness_level" asks for an integer, and if a non-integer value is passed through, Python prints out that is a ValueError. By using a try statement, it will ask for an integer, and if receives a ValueError, then the "except ValueError" will then be executed and not the "break". Since the try and except block is within the "while True:", this loop will always be executed until an integer is passed through since only if receives an integer, it will then break through the while loop.

#!/usr/bin/env python3

import os

def insert_number():

    while True:
        try:
            brightness_level = int(input("Please enter a number from the range 0-100 to set the brightness:"))
            break
        except ValueError:
            print("Invalid input. ", end="")

After an integer value is passed, it is also required that the number passed must be between 0 to 100. An if-else statement can be incorporated to ask for a number within this range. If an invalid number is returned, the statement prints out that the value is not within the range and re-executes the function to ask for an integer. If a valid number is passed through, the else statement inserts the integer through the PowerShell command by using the Python format method and it is executed by using the Python os module. The os command is returned as the function's output to change the brightness.

#!/usr/bin/env python3
# Finalized Script

import os

def insert_number():

    while True:
        try:
            brightness_level = int(input("Please enter a number from the range 0-100 to set the brightness:"))
            break
        except ValueError:
            print("Invalid input. ", end="")    

    if brightness_level < 0 or brightness_level > 100:
            print("{} is not within 0 to 100. ".format(brightness_level), end="")
            insert_number()
    else:
        adjust_brightness_command = "powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,{})".format(brightness_level)

        statement = "The brightness is now {}".format(brightness_level)
        return (print(statement), os.system(adjust_brightness_command))


insert_number()

Demonstration

Below is a demonstration of how the script works:

Source Code

https://github.com/AndrewDass1/SCRIPTS/tree/main/Windows/Python/Change%20Brightness