Skip to content
Related Articles

Related Articles

Python OpenCV – startWindowThread() Function

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Jun, 2022

This article will discuss how to use the python OpenCV startWindowThread() function.

Do you want to display images and videos using a simplified interface through an OpenCV code? Then, you must check out the OpenCV startWindowsThread() function, which lets you use the high GUI windows, i.e., a simplified interface for displaying images and videos from OpenCV code. 

Syntax: cv2.startWindowThread()

Parameters: None

Return Value: It doesn’t returns anything. 

Stepwise Implementation:

Step 1: First of all, import the OpenCV library.:

Here, we are importing the cv2 library, the cv2 is the OpenCV package that helps us to call the imread(), startWindowThread(), namedWindow(), and imshow() functions respectively.

import cv2

Step 2: Now, read the image using imread() function:

In this step, we have used the imread() function that loads an image from the specified file. 

img = cv2.imread(path, flag)
  • path: Path of the image to be read.
  • flag: Way in which image should be read. The default value of the flag is cv2.IMREAD_COLOR.

Step 3: Then, call the startWindowThread() function for using the high GUI windows:

In this step, we have used a startWindowThread() function which displays the simplified interface for displaying images and videos from OpenCV code.

cv2.startWindowThread()

Step 4: Next, assign the name and size to your GUI app:

In this step, we have used a namedWindow() function which is used to create a window with a suitable name and size to display images and videos on the screen.

cv2.namedWindow(window_name,prop_value)
  • window_name: Name of the window.
  • prop_value: New value of the window property such as cv2.WND_PROP_FULLSCREEN, cv2.WND_PROP_AUTOSIZE, cv2.WND_PROP_ASPECT_RATIO, etc.

Step 5: Further, display the image on the GUI app:

In this step, we have used the imshow() function that is used to display an image in a window. The window automatically fits the image size.

cv2.imshow(window_name, image)
  • window_name: A string representing the name of the window in which the image is to be displayed. 
  • image: It is the image that is to be displayed.

Step 6: Finally, make python sleep for an unlimited time:

In this step, we have used a waitKey() function that allows users to display a window for a specified time or until any key is pressed. 

cv2.waitKey(0)

Example:

In this example, we have used the image ‘gfg_black.png’ (link). We have also used the startWindowThread() function as a simplified interface for displaying images and videos from OpenCV code.

Python3




# Python program for OpenCV
# startWindowThread() function
  
# Import the library OpenCV
import cv2
  
# Read the image
img = cv2.imread("gfg_black.png")
  
# Use high GUI windows
cv2.startWindowThread()
  
# Name the GUI app
cv2.namedWindow("preview", cv2.WINDOW_NORMAL)
  
# Display the image on GUI app
cv2.imshow("preview", img)
  
# Make Python sleep for unlimited time
cv2.waitKey(0)


Output:

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!