How to Control Laptop Screen Brightness using Python?

17 Aug, 2021
To control the brightness of the screen we are using the screen-brightness-control library. The screen-brightness-control library has only one class and few functions. The most useful functions are mentioned below:
get_brightness()set_brightness()fade_brightness()list_monitors()
get_brightness()
set_brightness()
fade_brightness()
list_monitors()
Installation: We can install the package by running the following pip command:
pip install screen-brightness-control
Example 1: How to Get Screen Brightness
The get_brightness() method returns the current brightness level.
Syntax: get_brightness(display=None, method=None, verbose_error=False)
Parameters:
display -: the specific display you wish to adjust. This can be the name, model, serial or index of the display
method -: the OS specific method to use. On Windows this can be “wmi” (for laptop displays) or “vcp” (for desktop monitors). On Linux this can be “light”, “xrandr”, “ddcutil” or “xbacklight”.
verbose_error -: a boolean value to control how much detail any error messages should contain
Returns: Current brightness level
Python3
# importing the moduleimport screen_brightness_control as sbc # get current brightness  valuecurrent_brightness = sbc.get_brightness()print(current_brightness) # get the brightness of the primary displayprimary_brightness = sbc.get_brightness(display=0)print(primary_brightness)
  
Output: Suppose the brightness was this:
Then the output will be: 
50
50
The output can be a list or an integer depending on the number of detected monitors. 
Example 2: How to Set Screen Brightness
The set_brightness() method changes the brightness of the screen.
Syntax: set_brightness(value, display=None, method=None, force=False, verbose_error=False, no_return=False)
Parameters:
value: the level to set the brightness to. Can either be an integer or a string.
display:  the specific display you wish to adjust. This can be the name, model, serial or index of the display
method:  the OS specific method to use. On Windows this can be “wmi” (for laptop displays) or “vcp” (for desktop monitors). On Linux this can be “light”, “xrandr”, “ddcutil” or “xbacklight”.
force (Linux only): If set to False then the brightness is never set to less than 1 because on Linux this often turns the screen off. If set to True then it will bypass this check
verbose_error: A boolean value to control how much detail any error messages should contain
no_return: if False, this function will return what the brightness was set to. If True, this function returns nothing, which is slightly quicker
Python3
# importing the moduleimport screen_brightness_control as sbc # get current brightness valueprint(sbc.get_brightness()) #set brightness to 50%sbc.set_brightness(50) print(sbc.get_brightness()) #set the brightness of the primary display to 75%sbc.set_brightness(75, display=0) print(sbc.get_brightness())
    Output:
100
50
75
The output can be a list or an integer depending on the number of detected monitors. 
  Example 3: How to Fade the Brightness
The fade_brightness() method gently fades the brightness to a value.
Syntax: fade_brightness(finish, start=None, interval=0.01, increment=1, blocking=True)
Parameters:
finish: The brightness value to fade to
start: The value to start from. If not specified it defaults to the current brightness
interval: The time interval between each step in brightness
increment: The amount to change the brightness by each step
blocking: If set to False it fades the brightness in a new thread
Python3
# importing the moduleimport screen_brightness_control as sbc # get current brightness valueprint(sbc.get_brightness()) # fade brightness from the current brightness to 50%sbc.fade_brightness(50)print(sbc.get_brightness()) # fade the brightness from 25% to 75%sbc.fade_brightness(75, start = 25)print(sbc.get_brightness()) # fade the brightness from the current# value to 100% in steps of 10%sbc.fade_brightness(100, increment = 10)print(sbc.get_brightness())
 Output:
75
50
75
100
 Example 4: How to list available monitors
The list_monitors() method returns a list the names of all detected monitors
Syntax: list_monitors(method=None)
Parameters:
method: the OS specific method to use. On Windows this can be “wmi” (for laptop displays) or “vcp” (for desktop monitors). On Linux this can be “light”, “xrandr”, “ddcutil” or “xbacklight”.
Python3
# import the libraryimport screen_brightness_control as sbc # get the monitor namesmonitors = sbc.list_monitors()print(monitors) # now use this to adjust specific screens by namesbc.set_brightness(25, display=monitors[0])
Output:
["BenQ GL2450H", "Dell U2211H"]
The names and quantity of monitors will vary depending on what is plugged into your computer.
captaincrozzers
rajeev0719singh
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.