pyclickimage.qt_image_viewer#

class pyclickimage.qt_image_viewer.QtImageViewer(parent: QWidget | None = None, half_shift: bool = True)[source]#

Bases: QGraphicsView

Interactive image viewer with zoom, annotations and selection.

The widget displays a QPixmap inside a QGraphicsScene and provides:

  • subpixel image-coordinate tracking;

  • configurable crosshair;

  • mouse-coordinate signals;

  • mouse click signals;

  • zoom using the mouse wheel;

  • panning by dragging with the left mouse button;

  • click-safe left and right mouse signals;

  • cross, rectangle and ellipse markers;

  • rectangular region selection by mouse dragging.

Image coordinates are expressed in the coordinate system of the displayed pixmap. When half_shift is enabled, coordinates are shifted by -0.5 so that the center of the first pixel corresponds to (0, 0).

Parameters:
  • parent (QWidget, optional) – Parent Qt widget.

  • half_shift (bool, default=True) – If True, pixel centers are represented using coordinates (0, 0) for the first pixel. If False, coordinates refer to pixel corners.

  • Signals

  • -------

  • mouse_entered (pyqtSignal(float, float)) –

    Emitted when the mouse cursor enters the image.

    param x:

    Horizontal image coordinate of the cursor.

    type x:

    float

    param y:

    Vertical image coordinate of the cursor.

    type y:

    float

  • mouse_moved (pyqtSignal(float, float)) –

    Emitted whenever the mouse cursor moves while it is inside the image.

    param x:

    Horizontal image coordinate of the cursor.

    type x:

    float

    param y:

    Vertical image coordinate of the cursor.

    type y:

    float

  • mouse_left (pyqtSignal()) – Emitted when the mouse cursor leaves the image.

  • left_pressed (pyqtSignal(float, float)) –

    Emitted when the left mouse button is pressed inside the image.

    param x:

    Horizontal image coordinate at the press position.

    type x:

    float

    param y:

    Vertical image coordinate at the press position.

    type y:

    float

  • left_released (pyqtSignal(float, float)) –

    Emitted when the left mouse button is released inside the image.

    param x:

    Horizontal image coordinate at the release position.

    type x:

    float

    param y:

    Vertical image coordinate at the release position.

    type y:

    float

  • right_pressed (pyqtSignal(float, float)) –

    Emitted when the right mouse button is pressed inside the image.

    param x:

    Horizontal image coordinate at the press position.

    type x:

    float

    param y:

    Vertical image coordinate at the press position.

    type y:

    float

  • right_released (pyqtSignal(float, float)) –

    Emitted when the right mouse button is released inside the image.

    param x:

    Horizontal image coordinate at the release position.

    type x:

    float

    param y:

    Vertical image coordinate at the release position.

    type y:

    float

  • left_click (pyqtSignal(float, float)) –

    Emitted when a left-button press and release are detected as a click rather than as a drag. A left-button drag pans the view and does not emit this signal.

    param x:

    Horizontal image coordinate of the click.

    type x:

    float

    param y:

    Vertical image coordinate of the click.

    type y:

    float

  • right_click (pyqtSignal(float, float)) –

    Emitted when a right-button press and release are detected as a click rather than as a drag.

    param x:

    Horizontal image coordinate of the click.

    type x:

    float

    param y:

    Vertical image coordinate of the click.

    type y:

    float

  • rectangle_selected (pyqtSignal(float, float, float, float)) –

    Emitted when a right-button drag is finished. A right-button click (movement of at most 2 px) does not emit this signal.

    param x:

    Horizontal image coordinate of the top-left corner of the selected rectangle.

    type x:

    float

    param y:

    Vertical image coordinate of the top-left corner of the selected rectangle.

    type y:

    float

    param width:

    Width of the selected rectangle in image coordinates.

    type width:

    float

    param height:

    Height of the selected rectangle in image coordinates.

    type height:

    float

Notes

All x and y coordinates are expressed in image coordinates, not widget or scene coordinates.

When half_shift=True, the center of the first pixel has coordinates (0, 0). When half_shift=False, coordinates correspond directly to the image coordinate system used by the underlying QGraphicsPixmapItem.

For selection signals, x and y always identify the top-left corner of the normalized rectangle, regardless of the direction in which the user dragged the mouse. width and height are therefore always non-negative.

Notes

Mouse bindings:

  • wheel: zoom around the cursor;

  • left button: click (left_click), or pan the view when dragged;

  • right button: click (right_click), or rectangular selection (rectangle_selected) when dragged.

A drag is distinguished from a click using a small movement tolerance (2 px). A drag therefore does not emit left_click or right_click. Panning only starts once the tolerance is exceeded, so a click never moves the image.

Mouse signals are only emitted while an image is displayed. clear_image() removes the image; has_image() tells whether one is displayed.

To refresh the pixels without losing the zoom or the markers (for example after a colormap change), call set_image(pixmap, reset_view=False).

_clamp_to_image(point: QPointF) → QPointF[source]#

Clamp a scene position to the image boundaries.

_create_crosshair() → None[source]#

Create the graphical crosshair items.

_finish_selection(current: QPointF) → None[source]#

Finish the current rectangular selection.

_image_to_scene(x: float, y: float) → QPointF[source]#

Convert image coordinates to scene coordinates.

_inside_image(point: QPointF) → bool[source]#

Return whether a scene position is inside the image.

_pan(event: QMouseEvent) → None[source]#

Pan the view during a left-button drag.

Notes

Panning starts once the cursor has moved more than the click tolerance from the press position. The first step then applies the whole displacement since the press, so the image stays under the cursor. The scroll bars are moved by the cursor displacement.

_rect_to_image(rect: QRectF) → Tuple[float, float, float, float][source]#

Convert a scene rectangle to image coordinates.

_scene_to_image(point: QPointF) → Tuple[float, float][source]#

Convert scene coordinates to image coordinates.

_selection_rect(current: QPointF) → QRectF[source]#

Return the normalized selection rectangle.

_stop_panning() → None[source]#

End a left-button pan and restore the cursor.

_update_selection(current: QPointF) → None[source]#

Update and emit the current selection rectangle.

clear_image() → None[source]#

Remove the displayed image, markers and selection.

After this call the viewer is empty: no coordinate signal is emitted until a new image is set with set_image().

clear_markers() → None[source]#

Remove all user markers.

clear_selection() → None[source]#

Remove the current selection overlay.

property crosshair_color: QColor#

Color used to draw the crosshair.

Returns:

Current crosshair color.

Return type:

QColor

draw_cross(x: float, y: float, color: QColor = <PyQt5.QtGui.QColor object>, size: float = 8.0, width: float = 0.0) → Tuple[QGraphicsLineItem, QGraphicsLineItem][source]#

Draw a cross marker.

Parameters:
  • x (float) – Image coordinates.

  • y (float) – Image coordinates.

  • color (QColor, default=blue) – Marker color.

  • size (float, default=8) – Half-size of the cross.

  • width (float, default=0) – Pen width.

Returns:

Horizontal and vertical marker lines.

Return type:

tuple of QGraphicsLineItem

draw_ellipse(x: float, y: float, width: float, height: float, color: QColor = <PyQt5.QtGui.QColor object>, pen_width: float = 0.0, brush: QBrush | None = None) → QGraphicsEllipseItem[source]#

Draw an elliptical marker.

draw_rectangle(x: float, y: float, width: float, height: float, color: QColor = <PyQt5.QtGui.QColor object>, pen_width: float = 0.0, brush: QBrush | None = None) → QGraphicsRectItem[source]#

Draw a rectangular marker.

get_crosshair_color() → QColor[source]#

Return the current crosshair color.

Returns:

Current crosshair color.

Return type:

QColor

get_x_line() → QGraphicsLineItem[source]#

Return the vertical crosshair line.

Returns:

Graphics item representing the line at the current x-coordinate.

Return type:

QGraphicsLineItem

get_y_line() → QGraphicsLineItem[source]#

Return the horizontal crosshair line.

Returns:

Graphics item representing the line at the current y-coordinate.

Return type:

QGraphicsLineItem

property half_shift: bool#

Whether image coordinates use half-pixel shifting.

Returns:

True when the first pixel center corresponds to (0, 0).

Return type:

bool

has_image() → bool[source]#

Return whether an image is currently displayed.

Returns:

True if set_image() was called and the image was not removed with clear_image().

Return type:

bool

leaveEvent(event: QEvent) → None[source]#

Hide the crosshair when the cursor leaves the widget.

Leaving the widget directly from the image (for example toward a neighboring panel) hides the crosshair and emits mouse_left.

left_click#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

left_pressed#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

left_released#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

load_image(path: str) → None[source]#

Load an image from a file.

Parameters:

path (str) – Path to the image file.

Raises:

ValueError – If the image cannot be loaded.

mouseMoveEvent(event: QMouseEvent) → None[source]#

Process mouse movement, panning and selection updates.

While the left button is pressed and dragged, the view follows the cursor (see _pan()).

mousePressEvent(event: QMouseEvent) → None[source]#

Process mouse-button presses.

A left press starts a potential click or pan, and a right press starts a potential click or rectangular selection.

mouseReleaseEvent(event: QMouseEvent) → None[source]#

Process mouse-button releases.

Notes

A release within 2 px of the press position is a click and emits left_click or right_click. A left-button release after a larger movement ends the pan. A right-button release after a larger movement finishes the selection and emits rectangle_selected.

mouse_entered#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

mouse_left#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

mouse_moved#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

rectangle_selected#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

reset_view() → None[source]#

Reset the view and fit the image.

resizeEvent(event: QResizeEvent) → None[source]#

Handle viewer resizing without destroying an active zoom.

right_click#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

right_pressed#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

right_released#

str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Type:

pyqtSignal(*types, name

set_crosshair_color(color: QColor) → None[source]#

Set the crosshair color.

Method equivalent of the crosshair_color setter, kept for symmetry with get_crosshair_color().

Parameters:

color (QColor) – New crosshair color.

set_image(pixmap: QPixmap, reset_view: bool = True) → None[source]#

Set the image displayed by the viewer.

Parameters:
  • pixmap (QPixmap) – Image to display.

  • reset_view (bool, default=True) –

    If True, the scene is rebuilt: existing markers and the current selection are removed and the view is fitted to the image.

    If False and the new pixmap has the same size as the displayed one, only the pixels are replaced. Markers, zoom and pan are preserved. This is the mode used to refresh the display after a colormap change. If the size differs, the method falls back to reset_view=True.

Raises:

ValueError – If pixmap is null.

wheelEvent(event: QWheelEvent) → None[source]#

Zoom around the mouse position.

The wheel only zooms: it never scrolls the view (panning is a left-button drag).