Python OpenCV – getWindowImageRect() Function
Python OpenCV getWindowImageRect() Function returns the client screen coordinates, along with the width and height of the window containing the picture.
Syntax of cv2.getWindowImageRect()
Syntax: cv2.getWindowImageRect(window_name)
Parameter:
- window_name – Name of the window displaying image/video
Return: return a tuple of x, y, w, h(Set of four integers including the coordinates & size of rectangle)with window name.
Image for Demonstration:
Example 1: Get default window size using getWindowImageRect() Python
- In the mentioned code, the Python OpenCV library is imported.The path variable stores the filepath of the image from the local machine.
- The imread() method is used in default mode to load the given file from the path into the image variable.
- To use the getWindowImageRect() function, we need to create a window first using the namedWindow() method and the default flag value is used.
- The dimensions and coordinates returned by the getWindowImageRect() function of the created window is printed on the console. The returned values can also be assigned separately and printed.
- The created window is displayed to the user using the imshow() method. After the wait time (here 0 ms), the user can destroy all the windows by pressing any key from the keyboard.
Python3
import cv2
path = 'C:/Users/Amisha Kirti/Downloads/GFG.png'
image = cv2.imread(path)
cv2.namedWindow( "Display1" , cv2.WINDOW_AUTOSIZE)
print (cv2.getWindowImageRect( "Display1" ))
(x, y, windowWidth, windowHeight) = cv2.
getWindowImageRect( "Display1" )
print ( "Origin Coordinates(x,y): " , x, y)
print ( "Width: " , windowWidth)
print ( "Height: " , windowHeight)
cv2.imshow( 'Display1' , image)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Output:
Example-1 Console & Window Output
Example 2: Get fullscreen window size using getWindowImageRect() Python
This function helps us get the exact number of pixels of the window in which the image is displayed.
- One can create a window in full-screen mode and get its width and height
- To use the getWindowImageRect() function, we need to create a window first using the namedWindow() method. In this example the WINDOW_FULLSCREEN or WINDOW_NORMAL flag value is used.
Python3
import cv2
path = 'download3.png'
image = cv2.imread(path)
cv2.namedWindow( "Display2" , cv2.WINDOW_NORMAL)
print (cv2.getWindowImageRect( "Display2" ))
(x, y, windowWidth, windowHeight) = cv2.
getWindowImageRect( "Display2" )
print ( "Origin Coordinates(x,y): " , x, y)
print ( "Width: " , windowWidth)
print ( "Height: " , windowHeight)
cv2.imshow( 'Display2' , image)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Output:
Example-2 Window Output
Example-2 Console Output
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...