Introduction
OpenCV (Open Source Computer Vision Library) is a versatile library used for computer vision tasks. It provides various functions to display images and video streams in windows. In addition to basic display functionalities, OpenCV allows you to adjust the size and position of these windows, providing better control over the graphical interface for your applications.
Syntax for Adjusting Window Size and Position
To adjust the window size, you can use the cv2.namedWindow() function with specific flags, followed by cv2.resizeWindow(). To adjust the window position, use the cv2.moveWindow() function. The syntaxes are as follows:

- window_name: The name of the window.
- flags: Window flags (e.g.,
cv2.WINDOW_NORMALfor resizable windows). - width, height: The desired width and height of the window.
- x, y: The desired x and y coordinates of the top-left corner of the window.
Example
Here is a simple example to demonstrate how to adjust the size and position of an OpenCV window:
import cv2
# Load an image
image = cv2.imread('example.jpg')
# Create a named window with the resizable flag
cv2.namedWindow('Resizable Window', cv2.WINDOW_NORMAL)
# Resize the window to 800x600 pixels
cv2.resizeWindow('Resizable Window', 800, 600)
# Move the window to position (100, 100)
cv2.moveWindow('Resizable Window', 100, 100)
# Display the image in the adjusted window
cv2.imshow('Resizable Window', image)
# Wait for a key press and close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example:
- We load an image using
cv2.imread(). - We create a named window with the
cv2.WINDOW_NORMALflag, allowing it to be resizable. - We resize the window to 800×600 pixels using
cv2.resizeWindow(). - We move the window to the position (100, 100) on the screen using
cv2.moveWindow(). - We display the image in the adjusted window using
cv2.imshow(). - We wait for a key press and close the window using
cv2.waitKey(0)andcv2.destroyAllWindows().
This script demonstrates how to control the size and position of an OpenCV window, providing a better user experience for your applications.