pyclickimage.qt_image_viewer#
- class pyclickimage.qt_image_viewer.QtImageViewer(parent: QWidget | None = None, half_shift: bool = True)[source]#
Bases:
QGraphicsViewInteractive image viewer with zoom, annotations and selection.
The widget displays a
QPixmapinside aQGraphicsSceneand 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_shiftis enabled, coordinates are shifted by-0.5so 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. IfFalse, 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
xandycoordinates are expressed in image coordinates, not widget or scene coordinates.When
half_shift=True, the center of the first pixel has coordinates(0, 0). Whenhalf_shift=False, coordinates correspond directly to the image coordinate system used by the underlyingQGraphicsPixmapItem.For selection signals,
xandyalways identify the top-left corner of the normalized rectangle, regardless of the direction in which the user dragged the mouse.widthandheightare 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_clickorright_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).- _image_to_scene(x: float, y: float) QPointF[source]#
Convert image coordinates to scene coordinates.
- _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.
- 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().
- 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:
Truewhen the first pixel center corresponds to(0, 0).- Return type:
bool
- has_image() bool[source]#
Return whether an image is currently displayed.
- Returns:
Trueifset_image()was called and the image was not removed withclear_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_clickorright_click. A left-button release after a larger movement ends the pan. A right-button release after a larger movement finishes the selection and emitsrectangle_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
- 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_colorsetter, kept for symmetry withget_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
Falseand 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 toreset_view=True.
- Raises:
ValueError – If
pixmapis null.