The Wayback Machine - http://web.archive.org/web/20240703075746/https://www.geeksforgeeks.org/python-opencv-namedwindow-function/
Open In App

Python OpenCV – namedWindow() Function

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Image

Example 1: Working of namedWindow() method with automatically sets the window size

Python3




# Python program to explain cv2.namedWindow() method
  
# Importing OpenCV
import cv2
  
# Path to image in local directory
path = 'C:/Users/art/OneDrive/Desktop/geeks.png'
  
# Using cv2.imread() to read an image in default mode
image = cv2.imread(path)
  
# Using namedWindow()
# A window with 'Display' name is created
# with WINDOW_AUTOSIZE, window size is set automatically
cv2.namedWindow("Display", cv2.WINDOW_AUTOSIZE)
  
# using cv2.imshow() to display the image
cv2.imshow('Display', image)
  
# Waiting 0ms for user to press any key
cv2.waitKey(0)
  
# Using cv2.destroyAllWindows() to destroy
# all created windows open on screen
cv2.destroyAllWindows()


Output:

Image

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




# Python Program to explain namedWindow() method
  
# Importing OpenCV
import cv2
  
# Path to image in local directory
path = 'C:/Users/art/OneDrive/Desktop/geeks.png'
  
# Using cv2.imread() to read an image in grayscale mode
image = cv2.imread(path, 0)
  
# Using namedWindow()
# A window with 'Display_Image' name is created
# with WINDOW_NORMAL allowing us to have random size
cv2.namedWindow("Display_Image", cv2.WINDOW_NORMAL)
  
# Using cv2.imshow() to display the image
cv2.imshow('Display_Image', image)
  
# Waiting 0ms for user to press any key
cv2.waitKey(0)
  
# Using cv2.destroyAllWindows() to destroy
# all created windows open on screen
cv2.destroyAllWindows()


Output:

Image

Note: When a user randomly changes size, the window size is changed dimensions of the image remain unchanged.



Similar Reads

Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 min read
Python OpenCV - setWindowTitle() Function
Python OpenCV setWindowTitle() method used for giving the title of the windows. It takes 2 parameters that are windows name and the title that needs to be given. Both the parameters are expected to be of string type. Syntax: cv2.setWindowTitle( winname, title ) Parameters: winname: windows nametitle: title we want to set for the window with the abo
1 min read
Python OpenCV - imencode() Function
Python OpenCV imencode() function converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier. Basic example of imencode() FunctionExample 1: We began by importing the necessary libraries, which are OpenCV and NumPy. After importing lib
2 min read
Python OpenCV - resizeWindow() Function
resizeWindow() method in Python OpenCV is used to resize window displaying images/videos to a specific size. The specified window size is for images excluding toolbars. This only works for created windows having flags other than CV_WINDOW_AUTOSIZE. Syntax: cv2.resizeWindow(window_name, width, height) Parameters: window_name: Name of the window that
1 min read
Python OpenCV - waitKey() Function
waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. Examples 1: Display image with a time limit Using waitKey() method
1 min read
Python OpenCV - waitKeyEx() Function
Python OpenCv waitKeyEx() method is similar to waitKey() method but it also returns the full key code. The key code which is returned is implementation-specific and depends on the used backend: QT/GTK/Win32/etc. Syntax: cv2.waitKey(delay) Parameters: delay: The time in milliseconds after which windows needs to destroyed. If given 0 it waits for inf
2 min read
Python OpenCV - getRotationMatrix2D() Function
cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image. Syntax: cv2.getRotationMatrix2D(center, angle, scale) Parameters: center: Center of rotationangle(θ): Angle of Rotation. Angle is positive for anti-clockwise and negative for clockwise.scale: scaling factor which scales the image
3 min read
Python OpenCV - destroyAllWindows() Function
Python Opencv destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. If you have multiple windows open at the same time and you want to close then you would use this function. It doesn't take any parameters and doesn't return anything. It is similar to destroyWindow() function but this functi
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg