Set Window Title in OpenCV with Python using setWindowTitle() Function

OpenCV (Open Source Computer Vision Library) is a powerful library used for computer vision tasks. It is widely used for image processing, video analysis, object detection, machine learning, etc . In Python, OpenCV provides bindings to the original C++ library, making it easier to use for various computer vision tasks.

Using setWindowTitle()

cv2.setWindowTitle()  is a function in OpenCV that allows you to set or change the title of a window when displaying an image or video.

When you display an image in a window using cv2.imshow(), the window will have a default name (the string you passed to imshow). If you want to change that title after the window has been created, you can use cv2.setWindowTitle() .

Syntax :

cv2.setWindowTitle(windowName, title)

Parameters:
  • windowName: The name of the window you want to change the title for. This is the same name that you used when calling cv2.imshow() to create the window.
  • title: The new title string you want to set for the window.

Program :

import cv2

# Load an image
image = cv2.imread('image.jpg') #spcify the image file path as parameter

# Create a window to display the image with a name "Original Window"
cv2.imshow('Original Window', image)

# Change the title of the window to "My Custom Title"
cv2.setWindowTitle('Original Window', 'My Custom Title')

# Wait until a key is pressed, then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

Explanation:
  1. cv2.imread(‘image.jpg’) : Loads the image file into the image variable.
  2. cv2.imshow(‘Original Window’, image): Displays the image in a window with the initial title “Original Window.”
  3. cv2.setWindowTitle(‘Original Window’, ‘My Custom Title’) : Changes the title of the window from “Original Window” to “My Custom Title.”
  4. cv2.waitKey(0): Waits for any key press before closing the window.
  5. cv2.destroyAllWindows() : Closes the window when the key is pressed.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top