Thursday, December 26, 2024
Google search engine
HomeLanguagesPython OpenCV – moveWindow() Function

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 specific position moveWindow() function of OpenCV will do it. 

Syntax: cv2.moveWindow(window_Name,x,y)

Parameters:

  • window_name: name of the window which you want to move at particular position
  • x: Value of x coordinate
  • y: Value of y coordinate

Return: None

Example 1: Image with a specific position

In this example, we will display only one window at a particular position.

Used image:

Code:

Python3




# Import OpenCV library
import cv2
  
# Read an Image
img = cv2.imread("Documents/Lazyroarlogo.png",
                 cv2.IMREAD_COLOR)
  
# Display image using imshow() function
cv2.imshow("I2", img)
  
# Move window to (10,50) position
# using moveWindow() function
cv2.moveWindow("I2", 10, 50)
  
# Wait for user to press any key
cv2.waitKey(0)
  
# Close all opened windows
cv2.destroyAllWindows()


Output:

Example 2: Multiple images in different positions

In this example, we will display Multiple windows at a particular position.

Used image:

Python3




# Import OpenCV library
import cv2
  
# Read four Images
img1 = cv2.imread("Documents/Lazyroarlogo.png", cv2.IMREAD_COLOR)
img2 = cv2.imread("Documents/Lazyroarlogo2.png", cv2.IMREAD_COLOR)
img3 = cv2.imread("Documents/Lazyroarlogo3.png", cv2.IMREAD_COLOR)
img4 = cv2.imread("Documents/Lazyroarlogo4.png", cv2.IMREAD_COLOR)
  
  
# Display images using imshow() function
cv2.imshow("I1", img1)
cv2.imshow("I2", img2)
cv2.imshow("I3", img3)
cv2.imshow("I4", img4)
  
# Move window to (10,50) position
# using moveWindow() function
cv2.moveWindow("I1", 10, 50)
cv2.moveWindow("I2", 650, 50)
cv2.moveWindow("I3", 10, 500)
cv2.moveWindow("I4", 650, 500)
  
# Wait for user to press any key
cv2.waitKey(0)
  
# Close all opened windows
cv2.destroyAllWindows()


Output:

RELATED ARTICLES

Most Popular

Recent Comments