Python OpenCV – namedWindow() Function
Last Updated :
03 Jan, 2023
Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen.
Created windows are referred by their names and can also be used as a placeholder. The function does nothing If a window exists with the same name.
Syntax: cv2.namedWindow(window_name, flag)
Parameters:
- window_name: Name of the window that will display image/video
- flag: Represents if window size is automatically set or adjustable.
Some of the flag values are:
- WINDOW_NORMAL – Allows to manually change window size
- WINDOW_AUTOSIZE(Default) – Automatically sets the window size
- WINDOW_FULLSCREEN – Changes the window size to fullscreen
Return Value: It doesn’t return anything
Image used for all the below examples:
Example 1: Working of namedWindow() method with automatically sets the window size
Python3
import cv2
path = 'C:/Users/art/OneDrive/Desktop/geeks.png'
image = cv2.imread(path)
cv2.namedWindow( "Display" , cv2.WINDOW_AUTOSIZE)
cv2.imshow( 'Display' , image)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Output:
Explanation:
- In this code, in order to use namedWindow function OpenCV python library is imported.
- Then by using cv2.imread, a file from a particular location(path) is loaded into ‘image’ variable in default mode.
- Now to create a window with ‘Display’ name and automatic size for image namedWindow is used.
- By using cv2.imshow, the custom window is displayed on the screen. After waiting for 0ms user can destroy all windows by pressing any key from the keyboard.
Example 2: Manually change the window size
Python3
import cv2
path = 'C:/Users/art/OneDrive/Desktop/geeks.png'
image = cv2.imread(path, 0 )
cv2.namedWindow( "Display_Image" , cv2.WINDOW_NORMAL)
cv2.imshow( 'Display_Image' , image)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Output:
Note: When a user randomly changes size, the window size is changed dimensions of the image remain unchanged.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...