본문 바로가기
목차
Python

[Ex] PyAutoGUI - 화면 캡처 및 이미지 인식

by ds31x 2025. 7. 15.
728x90
반응형

1-1. 실행 전 준비

  1. 화면에서 버튼 또는 아이콘을 캡처하여 target.png로 저장.
  2. 해당 버튼/아이콘이 현재 화면에 그대로 떠 있도록 유지.
  3. target.png는 Python 코드와 같은 폴더에 둘 것.

가급적 opencv를 설치하고 하길 권함.
Pillow 만으로는 거의 locateOnScreen이 잘 동작하지 않음.

1-2. 실습 코드

import pyautogui
import time
from pyautogui import ImageNotFoundException

time.sleep(3)

# 전체 화면 캡처
im = pyautogui.screenshot()
im.save('full_screen.png')
time.sleep(1)

# 색상 확인
color = pyautogui.pixel(100, 100)
print("색상:", color)

# 영역 저장
im = pyautogui.screenshot(region=(3723, 138, 92, 96))
print(f"이미지 모드: {im.mode}")
im.save('target.png')

# 이미지 탐색 및 클릭
try:
    location = pyautogui.locateOnScreen('target.png', confidence=0.8)
    center = pyautogui.center(location)
    pyautogui.moveTo(center, duration=0.5)
    current_pnt = pyautogui.position()
    print(f"{current_pnt = }")
except ImageNotFoundException:
    print("이미지를 찾지 못했습니다.")
  • 필요 시 pyautogui.mouseInfo()로 좌표와 RGB 확인 가능.
728x90

'Python' 카테고리의 다른 글

[Ex] PyAutoGUI - MessageBox  (0) 2025.07.15
[Ex] PyAutoGUI - hotkey 조합 입력하기  (0) 2025.07.15
[Ex] PyAutoGUI - 키보드 입력  (2) 2025.07.14
[Ex] PyAutoGUI - 마우스 이동 및 클릭  (0) 2025.07.14
[Ex] Input_Recoder and Replayer  (0) 2025.07.14