PyAutoGUI
PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications. The API is designed to be simple. PyAutoGUI works on Windows, macOS, and Linux, and runs on Python 2 and 3.
Install
Basic Example
import pyautogui
screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.
screenWidth, screenHeight
# (2560, 1440)
currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.
currentMouseX, currentMouseY
# (1314, 345)
pyautogui.moveTo(100, 150) # Move the mouse to XY coordinates.
pyautogui.click() # Click the mouse.
pyautogui.click(100, 200) # Move the mouse to XY coordinates and click it.
pyautogui.click('button.png') # Find where button.png appears on the screen and click it.
pyautogui.move(400, 0) # Move the mouse 400 pixels to the right of its current position.
pyautogui.doubleClick() # Double click the mouse.
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad) # Use tweening/easing function to move mouse over 2 seconds.
pyautogui.write('Hello world!', interval=0.25) # type with quarter-second pause in between each key
pyautogui.press('esc') # Press the Esc key. All key names are in pyautogui.KEY_NAMES
with pyautogui.hold('shift'): # Press the Shift key down and hold it.
pyautogui.press(['left', 'left', 'left', 'left']) # Press the left arrow key 4 times.
# Shift key is released automatically.
pyautogui.hotkey('ctrl', 'c') # Press the Ctrl-C hotkey combination.
pyautogui.alert('This is the message to display.') # Make an alert box appear and pause the program until OK is clicked.
MS Paint Drawing Example
distance = 200
while distance > 0:
pyautogui.drag(distance, 0, duration=0.5) # move right
distance -= 5
pyautogui.drag(0, distance, duration=0.5) # move down
pyautogui.drag(-distance, 0, duration=0.5) # move left
distance -= 5
pyautogui.drag(0, -distance, duration=0.5) # move up
화면 녹화
WARNING |
ChatGPT 결과이다. 검증 필요. |
PyAutoGUI can take screenshots, save them to files, and locate images within the screen. This is useful if you have a small image of, say, a button that needs to be clicked and want to locate it on the screen. These features are provided by the PyScreeze module, which is installed with PyAutoGUI.
Screenshot functionality requires the Pillow module. OS X uses the screencapture
command, which comes with the operating system. Linux uses the scrot command, which can be installed by running sudo apt-get install scrot
.
import cv2
import numpy as np
import pyautogui
# 녹화할 비디오의 파일명과 형식을 설정합니다.
output_file = "screen_recording.avi"
# 화면의 크기를 가져옵니다.
screen_size = pyautogui.size()
# 비디오 코덱 설정
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# VideoWriter 객체를 생성합니다.
fps = 20.0 # 초당 프레임 수
out = cv2.VideoWriter(output_file, fourcc, fps, screen_size)
try:
print("녹화를 시작하려면 Ctrl+C를 누르세요.")
while True:
# 화면을 캡처합니다.
img = pyautogui.screenshot()
# 이미지를 NumPy 배열로 변환하고 색상 형식을 BGR로 변경합니다.
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# 비디오 파일에 프레임을 씁니다.
out.write(frame)
except KeyboardInterrupt:
# Ctrl+C를 누르면 녹화를 중지합니다.
print("녹화가 중지되었습니다.")
finally:
# 모든 리소스를 해제합니다.
out.release()
cv2.destroyAllWindows()
See also
- python
- Automation
- Robotic process automation (RPA)
- Auto Clicker
- Auto Mouse
- Desktop Automation
- PyAutoGUI