Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Open CV
  • scikit-image
  • pycairo
  • Pyglet
  • Python
  • Numpy
  • Pandas
  • Python Database
  • Data Analysis
  • ML Math
  • Machine Learning
  • NLP
  • Deep Learning
  • Deep Learning Interview Questions
  • ML Projects
  • ML Interview Questions
  • 100 Days of Machine Learning
Open In App
Next Article:
Python OpenCV - waitKeyEx() Function
Next article icon

Python OpenCV - namedWindow() Function

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
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:

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:

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:

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


Next Article
Python OpenCV - waitKeyEx() Function
author
vibhutijain99
Improve
Article Tags :
  • Python
  • OpenCV
  • Python-OpenCV
Practice Tags :
  • python

Similar Reads

  • Python OpenCV - moveWindow() Function
    When we show the image using the imshow() function output window will open at the center or default position of a computer screen. Even if there are multiple image windows all windows will be displayed at the same position and we have to move windows manually. If we want to show image windows at a s
    2 min read
  • 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
    3 min read
  • Python OpenCV - getTrackbarPos() Function
    getTrackbarPos() is Function in Python OpenCV that returns the current position of the specified trackbar. It takes two arguments. The first is for the trackbar name and the second one is the window name which is the parent of the trackbar. Returns the trackbar position. Syntax : cv.getTrackbarPos(t
    2 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: Disp
    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 windo
    2 min read
  • Python - Move() function in wxPython
    In this particular article we will learn, how can we move our window to a particular point. This can be achieved using Move() function in wx.Window class of wxPython. Move() takes x and y points to move window to a particularx, y point. Syntax : wx.Move(self, x, y, flags=SIZE_USE_EXISTING) Parameter
    1 min read
  • Python OpenCV - selectroi() Function
    In this article, we are going to see an interesting application of the OpenCV library, which is selectROI(). With this method, we can select a range of interest in an image manually by selecting the area on the image. Syntax:  cv2.selectROI(Window_name, source image) Parameter: window_name:  name of
    3 min read
  • Python - GetMenu() function in wxPython
    In this particular article we are going to learn about GetMenu() function of wx.MenuBar class of wxPython. GetMenu() is function in wx.MenuBar class that return wx.Menu object present in Menubar. It needs only index of menu present on menubar. Syntax : wx.MenuBar.GetMenu(self, menuindex) Parameters
    1 min read
  • wxPython | GetToolEnabled() function in python
    In this article we are going to learn about GetToolEnabled() function present in class wx.ToolBar of wxPython. GetToolEnabled() function simply returns a pointer to the tool at particular position. It takes only single argument that is pos(position of tool starting from 0). Syntax : wx.ToolBar.GetTo
    2 min read
  • Python OpenCV | cv2.imshow() method
    OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imshow() method is used to display an image in a window. The window automatically fits the image size.  Syntax: cv2.imshow(window_name, image)Parameters: window_name: A string representing the name of the w
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences