首页 > 解决方案 > 在 ImgGrab 中没有得到正确的宽度和高度

问题描述

因此,我试图通过两次单击(左上角和右下角)来获取图像,但是当我将宽度和高度赋予 ImgGrab 函数时,我会收到与原始值不同的值,我该如何解决?

我认为,当我将显示器的左上角设置为 (0, 0) 时,我得到了正确的值,但在任何其他地方我得到了错误的值。

def get_pos():
    input('Place the mouse on the top left corner of your game screen and 
        press enter ')
    topleft = mouse.position
    input('Do the same, but in the lower right corner')
    lowerright = mouse.position
    print('to start/stop running the program press "M", to exit the 
    program press "ESC"')
    ref = (lowerright[0] - topleft[0], lowerright[1] - topleft[1])
    pos_click = [(ref[0]*0.83 + topleft[0], ref[1]*0.37 + topleft[1]), 
            (ref[0]*0.42 + topleft[0], ref[1]*0.45 + topleft[1]), 
            (ref[0]*0.84 + topleft[0], ref[1]*0.79 + topleft[1]), 
            (ref[0]*0.50 + topleft[0], ref[1]*0.75 + topleft[1]),
            (topleft), (ref)]
    return pos_click

pos_click = get_pos()

img = ImageGrab.grab(bbox=(pos_click[4][0], pos_click[4][1], pos_click[5] 
[0], 
pos_click[5][1])) #x, y, w, h
img_np = np.array(img)
print('tamanho de img',img, 'tamanho ref', pos_click[5])
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
cv2.imshow("frame", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()

我希望 img 变量的值与pos_click[5]

标签: python-3.xcv2pynput

解决方案


推荐阅读