Skip to content

Instantly share code, notes, and snippets.

@ashishrana160796
Created March 10, 2019 20:06
Show Gist options
  • Save ashishrana160796/9f91fe157c76bb19b2a98903ddf9c2a3 to your computer and use it in GitHub Desktop.
Save ashishrana160796/9f91fe157c76bb19b2a98903ddf9c2a3 to your computer and use it in GitHub Desktop.
From automatetheboringstuff.com the library pyautogui breaks when code is needed to be ported to different screen resolutions. Hence, this utility converts mouse clicks from one resolution to another resolution.
# Problem: Different Screens have different resolutions. When, it comes to GUI automation this resolution causes problem.
# Of having a different co-ordinate system for different systems. Hence, causing resolution problem issue.
# Solution: Just convert the resolution size with simple unitary formula method.
# This import is required to get the current screen size
import pyautogui
def res_convertor(wdth, hght):
'''
This function converts input co-ordinates wrt. reference resolution 1920x1080 into
co-ordinates of current resolution of the system.
'''
# Can be changed to another reference system also.
# Let's say input co-ordinates were encoded wrt. another reference resolution system.
rf_wdth = 1920
rf_hght = 1080
# Get resolution of current system.
crr_wdth, crr_hght = pyautogui.size()
# Check if the current system is having same resolution as reference system
if crr_wdth == rf_wdth and crr_hght == rf_hght:
return wdth, hght
# Simple, resolution converting logic
cnvrtd_wdth = (crr_wdth/rf_wdth)* wdth
cnvrtd_hght = (crr_hght/rf_hght)* hght
return int(cnvrtd_wdth), int(cnvrtd_hght)
if __name__ == '__main__':
# adding some test-values to check resolution conversion logic
print(res_convertor(1920,1080))
print(res_convertor(0,0))
print(res_convertor(1368,784))
print(res_convertor(225,113))
print(res_convertor(1080,1080))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment