Python创建区域屏幕截图

时间:2019-03-19 15:51:57

标签: python opencv user-interface automated-tests

是否可以在python中创建像此示例一样的区域屏幕截图?

example

for arg in "$@"
do
    case $arg in
      *.c) ;;
      *) arg="$arg.c";;
    esac

    cat <<'____HERE' > "$arg"
#include <stdio.h>
#include <stdlib.h>

int main(){




    return 0;
}
____HERE
done

1 个答案:

答案 0 :(得分:1)

这是执行此操作的一种方法。首先,您显示完整图像的屏幕截图(必要时调整大小)。创建一个MouseCallback,它在按下时存储鼠标的x / y位置,在按下时拖动在图像上绘制一个矩形,并在释放鼠标时创建一个子图像。

结果: enter image description here

代码:

import cv2
import numpy as np 

img = cv2.imread("screenshot.jpg", 1)
click1 = False
point1 = (0,0)

def click(event,x,y,flags, params):
        global click1, point1
        if event == cv2.EVENT_LBUTTONDOWN:
                # if mousedown, store the x,y position of the mous
                click1 = True
                point1 = (x,y)
        elif event == cv2.EVENT_MOUSEMOVE and click1:
                # when dragging pressed, draw rectangle in image
                img_copy = img.copy()
                cv2.rectangle(img_copy, point1, (x,y), (0,0,255),2)
                cv2.imshow("Image", img_copy)
        elif event == cv2.EVENT_LBUTTONUP:
                # on mouseUp, create subimage
                click1 = False
                sub_img = img[point1[1]:y,point1[0]:x]
                cv2.imshow("subimg", sub_img)

cv2.namedWindow("Image")
cv2.setMouseCallback("Image", click)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()