cv3.io.Window

class cv3.io.Window(window_name=None, pos=None, flag=1)[source]

Bases: object

A class to manage a single display window.

This class provides a convenient way to create and manage OpenCV windows with additional features like automatic naming and context manager support.

window_name

The name of the window.

Type:

str

__init__(window_name=None, pos=None, flag=1)[source]

Initialize a Window object.

Parameters:
  • window_name (str, optional) – Name of the window. If None, a name will be automatically generated. Defaults to None.

  • pos (tuple, optional) – Starting position of the window as (x, y). Defaults to None.

  • flag (int, optional) – Window flag. Defaults to cv2.WINDOW_AUTOSIZE.

Example

>>> import cv3
>>> import numpy as np
>>> # Create a window with automatic naming
>>> window = cv3.Window()
>>> # Create a window with a specific name and position
>>> window = cv3.Window('My Window', pos=(100, 100))
imshow(img)[source]

Display an image in this window.

Parameters:

img (numpy.ndarray) – Image to display.

Note

This function automatically handles RGB/BGR color space conversion based on the opt.RGB setting. When opt.RGB is True (default), the image is converted from RGB to BGR before displaying.

Example

>>> import cv3
>>> import numpy as np
>>> # Create a window
>>> window = cv3.Window('My Window')
>>> # Create an image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[:, :] = [255, 0, 0]  # Blue image (BGR)
>>> # Display the image
>>> window.imshow(img)
move(x, y)[source]

Move the window to a new position.

Parameters:
  • x (int) – New x-coordinate of the window.

  • y (int) – New y-coordinate of the window.

Example

>>> import cv3
>>> # Create a window
>>> window = cv3.Window('My Window')
>>> # Move the window to position (200, 200)
>>> window.move(200, 200)
close()[source]

Close this window and free associated resources.

Example

>>> import cv3
>>> # Create a window
>>> window = cv3.Window('My Window')
>>> # Close the window
>>> window.close()
static wait_key(t)[source]

Wait for a keyboard event.

This is a static method that calls the module-level wait_key function.

Parameters:

t (int) – Delay in milliseconds. If 0, it waits indefinitely for a key stroke.

Returns:

The code of the pressed key, or -1 if no key was pressed before the timeout.

Return type:

int

Example

>>> import cv3
>>> # Create a window
>>> window = cv3.Window('My Window')
>>> # Wait for a key press for 1 second
>>> key = window.wait_key(1000)
__enter__()[source]

Enter the runtime context for the window.

Returns:

This window instance.

Return type:

Window

Example

>>> import cv3
>>> # Use window as a context manager
>>> with cv3.Window('My Window') as window:
...     # Window is automatically closed when exiting the context
...     pass
__exit__(exc_type, exc_val, exc_tb)[source]

Exit the runtime context for the window.

This method ensures the window is closed when exiting the context manager.