How to Control Laptop Screen Brightness Using Python?


Python has become one of the most popular programming languages in the world, thanks to its simplicity, versatility, and wide range of applications. With its extensive libraries and frameworks, Python can be used for anything from web development to machine learning, and everything in between. One of the most popular libraries for data analysis and manipulation in Python is Pandas, which offers powerful tools for working with tabular data.

In this tutorial, we'll be using Python and the screen−brightness−control library to explore how to control laptop screen brightness. We'll show you how to use Python to adjust your screen brightness using code, and even automate the process based on the time of day. By the end of this article, you'll have a solid understanding of how to use Python to control screen brightness and some practical examples of how to use this functionality in your own projects. So, let's get started!

How to Control Laptop Screen Brightness Using Python?

For controlling laptop screen brightness using Python, we’re going to install the screen−brightness−control library.

To install the screen−brightness−control library, we can use pip, which is the package installer for Python. Open your terminal or command prompt and enter the following command:

pip install screen-brightness-control

The above command will download and install the latest version of the screen−brightness−control library, along with any dependencies it may require.

Now that we have installed the library, we can import it into our Python script using the following code:

from screen_brightness_control import get_brightness

This code imports the brightness module from the screen_brightness_control library, which provides a set of functions for controlling screen brightness. We'll be using these functions in the next section of the article to actually adjust the screen brightness.

Let's test our installation by checking the current screen brightness level using the brightness() function. Here's the code to do that:

# Import the `get_brightness` function from the `screen_brightness_control` module
from screen_brightness_control import get_brightness

# Get the current screen brightness using the `get_brightness` function
current_brightness = get_brightness()

# Print the current screen brightness to the console
print("Current screen brightness is:", current_brightness)

When you run the above code, you should see the current screen brightness level printed to the console.

Output

Current screen brightness is: 75

Now that we have installed and imported the screen−brightness−control library, let's learn how to use it to control screen brightness using Python.

Control Laptop Screen Brightness using the set_brightness function

The set_brightness() function allows us to adjust the brightness of the screen on our laptop or desktop. This function takes a single argument, which is the desired brightness level as a percentage. For example, if you want to set the screen brightness to 80%, you would call the set_brightness() function with a value of 80.

To increase the screen brightness, we can use the set_brightness() function, like this:

# Import the `get_brightness` & `set_brightness` functions from the `screen_brightness_control` module
from screen_brightness_control import set_brightness, get_brightness

# new brightness level
new_brightness = 80 

# replace this value with the desired brightness level
set_brightness(new_brightness)

# display the current brightness level
current_brightness = get_brightness()
print("Current screen brightness is:", current_brightness)

In the above code snippet, we set the new_brightness variable to the desired brightness level (in this case, 80%), and then call the set_brightness() function with this value as an argument. This will adjust the screen brightness to the desired level.

After that, we’re logging the current brightness level using the `get_brightness` function, which will log the new brightness of our laptop screen.

Output

Current screen brightness is: 80

As you can see in the output above, the laptop screen brightness has been changed to 80 and the same has been logged in the terminal.

Similarly, to decrease the screen brightness, we can use the set_brightness() function again, but with a lower brightness value. Here's an example of the code for the same:

# Import the `get_brightness` & `set_brightness` functions from the `screen_brightness_control` module
from screen_brightness_control import set_brightness, get_brightness

# new brightness level
new_brightness = 50 

# replace this value with the desired brightness level
set_brightness(new_brightness)
# display the current brightness level
current_brightness = get_brightness()
print("Current screen brightness is:", current_brightness)

In the above code, we set the new_brightness variable to a lower brightness level (in this case, 50%), and then call the set_brightness() function with this value as an argument. This will decrease the screen brightness to the desired level.

After that, we’re logging the current brightness level using the `get_brightness` function, which will log the new brightness of our laptop screen.

Output

Current screen brightness is: 50

As you can see in the output above, the laptop screen brightness has been changed to 50 and the same has been logged in the terminal.

So, this is how we can use the set_brightness() and get_brightness() functions in combination with Python, we can easily control the screen brightness of our laptop.

Conclusion

In this tutorial, we learned how to control laptop screen brightness using Python with the help of the screen−brightness−control library. We installed the library using pip, imported it into our Python script, and used the get_brightness() function to check the current screen brightness level. We also used the set_brightness() function to adjust the screen brightness to a desired level, both increasing and decreasing it. We provided examples for each of the methods, making it easy to follow along and try it out for yourself. By the end of this article, you should have a good understanding of how to control screen brightness using Python, and have practical knowledge of how to use it in your own projects.

Updated on: 21-Jul-2023

581 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements