파이썬 이미지캡쳐 좌표 설정 도와주세요.
조회수 1288회
import win32gui
import win32ui
from ctypes import windll
from PIL import Image
hwnd = win32gui.FindWindow(None, '새 텍스트 문서 (4).txt - 메모장')
# Change the line below depending on whether you want the whole window
# or just the client area.
#left, top, right, bot = win32gui.GetClientRect(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
# Change the line below depending on whether you want the whole window
# or just the client area.
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
print(result)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']) ,bmpstr, 'raw', 'BGRX', 0, 1)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
if result == 1:
#PrintWindow Succeeded
im.save("2.png")
이런 코드 에서 특정 범위만 캡쳐를 하고싶은데
w = right - left
h = bot - top
여기에서 변경하는 거 같아서 숫자를 더하고 빼보고 하는데 전부다 왼쪽 상단을 기준으로 다 캡쳐가 됩니다. (right + 500) -left 를 하면 옆으로 검은화면이 캡쳐됩니다. 이쪽 부분을 하는게 아닐까요?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
pillow image 를 다루는 게 더 편할 겁니다.
if result == 1: im = im.crop(5, 5, 55, 55) im.save("2.png")
이렇게 해 보세요.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.crop
댓글 입력