Python OpenCV - namedWindow() Function - GeeksforGeeks

25 Apr, 2022
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
Python3
# Python program to explain cv2.namedWindow() method # Importing OpenCVimport cv2 # Path to image in local directorypath = 'C:/Users/art/OneDrive/Desktop/geeks.png' # Using cv2.imread() to read an image in default modeimage = cv2.imread(path) # Using namedWindow()# A window with 'Display' name is created# with WINDOW_AUTOSIZE, window size is set automaticallycv2.namedWindow("Display", cv2.WINDOW_AUTOSIZE) # using cv2.imshow() to display the imagecv2.imshow('Display', image) # Waiting 0ms for user to press any keycv2.waitKey(0) # Using cv2.destroyAllWindows() to destroy# all created windows open on screencv2.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. 
Python3
# Python Program to explain namedWindow() method # Importing OpenCVimport cv2 # Path to image in local directorypath = 'C:/Users/art/OneDrive/Desktop/geeks.png' # Using cv2.imread() to read an image in grayscale modeimage = cv2.imread(path, 0) # Using namedWindow()# A window with 'Display_Image' name is created# with WINDOW_NORMAL allowing us to have random sizecv2.namedWindow("Display_Image", cv2.WINDOW_NORMAL) # Using cv2.imshow() to display the imagecv2.imshow('Display_Image', image) # Waiting 0ms for user to press any keycv2.waitKey(0) # Using cv2.destroyAllWindows() to destroy# all created windows open on screencv2.destroyAllWindows()
Output:
Note: When a user randomly changes size, the window size is changed dimensions of the image remain unchanged.
Picked
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python OOPs Concepts
How to Install PIP on Windows ?
Bar Plot in Matplotlib
Defaultdict in Python
Python Classes and Objects
Deque in Python
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python - Ways to remove duplicates from list
Class method vs Static method in Python