Python OpenCV – namedWindow() Function
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)
- 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
- 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
Note: When a user randomly changes size, the window size is changed dimensions of the image remain unchanged.