[파이썬] pyautogui 지역 및 전역 좌표계

PyAutoGUI is a powerful Python library that allows you to automate GUI interactions, such as moving the mouse cursor, clicking on buttons, and typing text. One important concept to understand when using PyAutoGUI is the coordinate system it uses, which includes both local and global coordinates.

Local Coordinates

Local coordinates in PyAutoGUI are relative to the active window or screen where the automation is taking place. The origin (0,0) is located at the top-left corner of the active window or screen.

To obtain the local coordinates of an object such as a button or an image, you can use the pyautogui.locateOnScreen() function. This function returns the position of the object as a BoundingBox object, which contains the coordinates of the top-left corner, width, and height.

Here is an example that demonstrates how to find the local coordinates of an image on the screen:

import pyautogui

# Load the image to search for
image = pyautogui.imread('button.png')

# Find the coordinates of the image on the screen
bbox = pyautogui.locateOnScreen(image)

# Print the top-left coordinate of the image
print(f"Top-left coordinate: ({bbox.left}, {bbox.top})")

Global Coordinates

Global coordinates in PyAutoGUI represent the absolute position on the entire screen, regardless of the active window. The origin (0,0) is located at the top-left corner of the primary monitor.

To convert local coordinates to global coordinates, you can use the pyautogui.localToGlobal() function. This function takes the local x and y coordinates and returns the equivalent global coordinates.

Here is an example that demonstrates how to convert local coordinates to global coordinates:

import pyautogui

# Local coordinates of an object
local_x = 100
local_y = 50

# Convert local coordinates to global coordinates
global_x, global_y = pyautogui.localToGlobal(local_x, local_y)

# Print the global coordinates
print(f"Global coordinates: ({global_x}, {global_y})")

Conclusion

Understanding the local and global coordinate systems in PyAutoGUI is essential for accurate automation of GUI tasks. By using the provided functions, you can easily work with coordinates in both systems and automate interactions with precision. Whether you’re automating repetitive tasks or creating GUI testing scripts, PyAutoGUI’s coordinate system allows you to interact with GUI elements effectively.