How to Get the Screen Resolution of a Computer in Python using the pyautogui Module



Python


In this article, we show how to get the screen resolution of a computer in Python.

To do this, we use the pyautogui module.

If you're using a Windows computer, this module comes with the standard Python package. If you are using other operating systems, you may have to externally download this module.

Since I'm using a windows computer, I did not have to install anything. I simply had to import it into program.

pyautogui is a module that can do many different dynamic things, including sending virtual keypresses and mouse clicks to Windows.

On this page, we show how to use this module to get the screen resolution of a computer.

The resolution of a computer is how many pixels wide and tall the screen is.

The greater the resolution of a screen, the greater clarity or definition the screen is.

So in order to get the screen resolution of a computer, the statement, pyautogui.size(), is used.

This is shown in the code below.



So, first, you have to import the pyautogui module.

You then call, pyautogui.size(), to get the screen resolution of the computer.

I ran this on my computer and in return got the output of (1366,768).

This means that across the screen, there are 1366 pixels (the width).

And from the top of the screen to the bottom of the screen, there are 768 pixels (the height).

This would normally mean that the computer screen has a greater width than height, since there are more pixels across the width than the height.

If you want to also, you can have the width and height separated.

The following code, shown below, separates the width and height of the screen resolution, so that each can be called separated.



So, again, we import the pyautogui module.

We then call declare 2 variables, width and height, and set them equal to pyautogui.size()

Now if we call the variables, we get the respective width and height values.

So this shows how the screen resolution of a computer can be obtained in Python.

It may be useful if you're dealing with desktop applications, mostly, and could be used for making certain configurations.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...