Home > Software engineering >  How do I change the screen brightness in windows using python?
How do I change the screen brightness in windows using python?

Time:02-15

How do I change the screen brightness in windows using python, without using any extra python packages? I'd also like a way of getting what the screen brightness is currently at. I'm thinking that will need to be by using ctypes, but I can't find any information on how to do it.

CodePudding user response:

by the help of PowerShell commands we can easily increase or decrease the brightness of windows without any external packages

WmiSetBrightness(1,<brightness % goes here>)

import subprocess

subprocess.run(["powershell", "(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,40)"])

the subprocess is the internal library that comes with python.

just run the above code, hope this will work.

CodePudding user response:

The another way to do it without any library is:-

import subprocess


def run(cmd):
    completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
    return completed

if __name__ == '__main__':
    take_brightness = input("Please enter the brightness level: ")
    command = "(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,"   take_brightness   ")"
    hello_command = f"Write-Host {command}"
    hello_info = run(hello_command)

It will work fine and please upvote and accept answer if you like it. Thank You.

CodePudding user response:

Install screen brightness control using pip Windows: pip install screen-brightness-control

Mac/Linux: pip3 install screen-brightness-control I guess

Then use this code

import screen_brightness_control as sbc

sbc.set_brightness(25) # Number will be any number between 0 and hundred
# Sets screen brightness to 25%

I found info on the screen brightness module here

CodePudding user response:

I used the WMI library and it really worked fine. Here is the code, but this is for Windows. I think it is OS specific so if it doesn't work for you, you should look for a better solution.

import wmi
    
brightness = 40 # percentage [0-100] For changing thee screen 
c = wmi.WMI(namespace='wmi')
methods = c.WmiMonitorBrightnessMethods()[0]    
methods.WmiSetBrightness(brightness, 0)

Please upvote and accept the answer if you like it.

  • Related