Reputation: 15
Is it possible in python to create an area screenshot like this example ?
import pyautogui
def capture_current_scene(name_screenshot, i):
im1= pyautogui.screenshot()
im1.save(name_screenshot+'_'+str(i))
def capture_area_scene():
currentMouseX1, currentMouseY1 = pyautogui.position()
# click one ==> initialize a rectangle form
currentMouseX2, currentMouseY2 = pyautogui.position()
# click two ==> end the rectangle form
# you created an area screenshot
Upvotes: 1
Views: 2003
Reputation: 4561
Here is one way you can do this. First you display the screenshot of the full image (resized if necessary). A MouseCallback is created that stores the x/y position of the mouse when pressed, draws a rectangle on the image when dragging pressed, and upon mouse release creates a subimage.
Code:
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()
Upvotes: 2