Python找到窗口句柄截图获取坐标

要查找图片名:target_image.png

import win32gui
import win32ui
import numpy as np
import cv2
from ctypes import windll

# 获取窗口柄
hwnd = win32gui.FindWindow("#32770", "Internet Download Manager 6.40")

# 获取窗口位置
left, top, right, bottom = win32gui.GetWindowRect(hwnd)

# 获取窗口大小
width = right - left
height = bottom - top

# 获取设备上下文
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

# 创建位图对象
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)

# 位图对象与设备上下文关联
saveDC.SelectObject(saveBitMap)

# 复制窗口内容到位图对象
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
img = np.frombuffer(bmpstr, dtype='uint8').reshape((bmpinfo['bmHeight'], bmpinfo['bmWidth'], 4))[:, :, :3]

# 加载目标图像
target_img = cv2.imread('target_image.png')

# 获取目标图像大小
target_height, target_width, _ = target_img.shape

# 根据颜色空间转换为灰度图像
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)

# 使用模板匹配查找目标图像
res = cv2.matchTemplate(img_gray, target_gray, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)

# 如果找到目标图像,则获取其居中坐标
if len(loc[0]) > 0:

    target_img_height = int(target_height / 2)
    target_img_width = int(target_width / 2)
    x = int(loc[1][0] + target_img_height)
    y = int(loc[0][0] + target_img_width)

    print(x,y)
import win32gui
import win32ui
from PIL import Image
from ctypes import windll

# 获取窗口柄
hwnd = win32gui.FindWindow("#32770", "Internet Download Manager 6.40")

# 获取窗口位置
left, top, right, bottom = win32gui.GetWindowRect(hwnd)

# 获取窗口大小
width = right - left
height = bottom - top

# 获取设备上下文
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

# 创建位图对象
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)

# 位图对象与设备上下文关联
saveDC.SelectObject(saveBitMap)

# 复制窗口内容到位图对象
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
img = Image.frombuffer( 'RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)

# 在图像中查找目标图像
target_img = Image.open('target_image.png')
target_width, target_height = target_img.size

# 获取图像像素列表
img_pixels = list(img.getdata())
target_pixels = list(target_img.getdata())

# 查找目标图像
for y in range(height - target_height):
    for x in range(width - target_width):
        count = 0
        # 比较像素列表中的像素
        for i in range(target_width * target_height):
            if img_pixels[(y+i//target_width)*width+x+i%target_width] == target_pixels[i]:
                count += 1
            else:
                break
        if count == target_width * target_height:
            # 如果找到目标图像,则返回其左上角坐标
            x, y = x + left, y + top
            print("Found target image at ({}, {})".format(x, y))


欢迎您的到来

积分:1=1 积分;
现在转战短视频平台,免费分享声卡调试技术,基础知识,机架插件提取封装:抖音 / 快手 同号:glbwl0808