Skip to content

Images

dynaface.image

Image loading, plus the base class that holds an image and draws on it.

ImageAnalysis

ImageAnalysis()

Holds an RGB image and draws on it: text, lines, arrows and shapes.

AnalyzeFace builds on this class. Measures draw their results on render_img, and save() writes it to a file.

is_image_loaded

is_image_loaded() -> bool

Check if an image is loaded.

Returns:

Name Type Description
bool bool

True if an image is loaded, False otherwise.

load_image

load_image(img: NDArray[Any]) -> bool

Load an image into the analysis class.

Parameters:

Name Type Description Default
img NDArray[Any]

Input image.

required

Returns: bool: True if the image was processed, False otherwise. Raises: TypeError: If img is None. ValueError: If img is too small.

init_image

init_image(img: NDArray[Any]) -> None

Initialize the image and related attributes.

Parameters:

Name Type Description Default
img NDArray[Any]

Input image.

required

write_text

write_text(
    pos: Tuple[int, int],
    txt: str,
    color: Optional[Tuple[int, int, int]] = None,
    size: float = 1,
    thick: int = 1,
) -> None

Write text on the image.

Parameters:

Name Type Description Default
pos Tuple[int, int]

Position to place the text.

required
txt str

Text to write.

required
color Optional[Tuple[int, int, int]]

Text color, defaults to white.

None
size float

Scaling factor for text size.

1
thick int

Thickness of the text.

1

calc_text_size

calc_text_size(
    txt: str, size: float = 1, thick: int = 1
) -> Tuple[Tuple[int, int], int]

Calculate text size and baseline.

Parameters:

Name Type Description Default
txt str

Text string.

required
size float

Text size scale.

1
thick int

Text thickness.

1

Returns:

Type Description
Tuple[Tuple[int, int], int]

Tuple[Tuple[int, int], int]: Text size and baseline.

write_text_sq

write_text_sq(
    pos: Tuple[int, int],
    txt: str,
    color: Optional[Tuple[int, int, int]] = None,
    mark: str = "2",
    up: int = 5,
) -> None

Write text with a square marker on the image.

Parameters:

Name Type Description Default
pos Tuple[int, int]

Position to place the text.

required
txt str

Text to write.

required
color Optional[Tuple[int, int, int]]

Text color.

None
mark str

Marker character.

'2'
up int

Upward offset for the marker.

5

hline

hline(
    y: int,
    x1: Optional[int] = None,
    x2: Optional[int] = None,
    color: Tuple[int, int, int] = (0, 255, 255),
    width: int = 5,
) -> None

Draw a horizontal line.

Parameters:

Name Type Description Default
y int

Y-coordinate.

required
x1 Optional[int]

Starting X-coordinate.

None
x2 Optional[int]

Ending X-coordinate.

None
color Tuple[int, int, int]

Line color.

(0, 255, 255)
width int

Line width.

5

vline

vline(
    x: int,
    y1: Optional[int] = None,
    y2: Optional[int] = None,
    color: Tuple[int, int, int] = (0, 255, 255),
    width: int = 5,
) -> None

Draws a vertical line in the image.

:param x: X-coordinate where the vertical line should be drawn. :param y1: Starting Y-coordinate (defaults to the top of the image if None). :param y2: Ending Y-coordinate (defaults to the bottom of the image if None). :param color: Color of the line in BGR format. :param width: Thickness of the line.

circle

circle(
    pt: Tuple[int, int],
    color: Tuple[int, int, int] = (0, 0, 255),
    radius: Optional[int] = None,
) -> None

Draw a circle on the image.

Parameters:

Name Type Description Default
pt Tuple[int, int]

Center point.

required
color Tuple[int, int, int]

Circle color.

(0, 0, 255)
radius Optional[int]

Circle radius.

None

render_reset

render_reset() -> None

Reset the render image to the original image.

extract_horiz

extract_horiz(
    y: int,
    x1: Optional[int] = None,
    x2: Optional[int] = None,
) -> NDArray[Any]

Extract a horizontal section of the image.

Parameters:

Name Type Description Default
y int

Row index.

required
x1 Optional[int]

Start column index.

None
x2 Optional[int]

End column index.

None

Returns:

Type Description
NDArray[Any]

NDArray[Any]: Extracted image section.

extract_horiz_hsv

extract_horiz_hsv(
    y: int,
    x1: Optional[int] = None,
    x2: Optional[int] = None,
) -> NDArray[Any]

Extract a horizontal section of the HSV image.

Parameters:

Name Type Description Default
y int

Row index.

required
x1 Optional[int]

Start column index.

None
x2 Optional[int]

End column index.

None

Returns:

Type Description
NDArray[Any]

NDArray[Any]: Extracted image section in HSV.

save

save(filename: str) -> None

Saves the rendered image to a file.

:param filename: The path where the image should be saved.

measure_polygon

measure_polygon(
    contours: List[Tuple[int, int]],
    pix2mm: float,
    alpha: float = 0.4,
    color: Tuple[int, int, int] = (0, 0, 255),
    render: bool = True,
) -> float

Measures the area of a polygon in the image.

:param contours: A list of points defining the polygon. :param pix2mm: Conversion factor from pixels to millimeters. :param alpha: Opacity for rendering overlay. :param color: Color for the polygon overlay. :param render: If True, renders the polygon overlay. :return: The computed area of the polygon.

line

line(
    pt1: Tuple[int, int],
    pt2: Tuple[int, int],
    color: Tuple[int, int, int] = (255, 0, 0),
    thickness: int = 3,
) -> None

Draws a line between two points.

:param pt1: Starting point of the line. :param pt2: Ending point of the line. :param color: Color of the line in BGR format. :param thickness: Thickness of the line.

arrow_head

arrow_head(
    pt1: Tuple[int, int],
    pt2: Tuple[int, int],
    par: int = 15,
) -> None

Draws an arrowhead at the end of a line.

:param pt1: The base point of the arrowhead. :param pt2: The tip of the arrow. :param par: Size of the arrowhead.

arrow

arrow(
    pt1: Tuple[int, int],
    pt2: Tuple[int, int],
    color: Tuple[int, int, int] = (255, 0, 0),
    thickness: int = 3,
    apt1: bool = True,
    apt2: bool = True,
) -> None

Draws a line with arrowheads at one or both ends.

:param pt1: Starting point of the line. :param pt2: Ending point of the line. :param color: Color of the line and arrowhead. :param thickness: Thickness of the line. :param apt1: If True, draws an arrowhead at pt1. :param apt2: If True, draws an arrowhead at pt2.

rectangle

rectangle(
    top_left: Tuple[int, int],
    bottom_right: Tuple[int, int],
    color: Tuple[int, int, int] = (255, 0, 0),
    thickness: int = 3,
    filled: bool = False,
    alpha: float = 1.0,
) -> None

Draws a rectangle on the image.

Parameters:

Name Type Description Default
top_left Tuple[int, int]

Top-left corner of the rectangle.

required
bottom_right Tuple[int, int]

Bottom-right corner of the rectangle.

required
color Tuple[int, int, int]

Color of the rectangle (default is red).

(255, 0, 0)
thickness int

Thickness of the rectangle border.

3
filled bool

Whether the rectangle is filled or not.

False
alpha float

Opacity level of filled rectangle (ignored if not filled).

1.0

sample_rectangle

sample_rectangle(
    top_left: Tuple[int, int],
    bottom_right: Tuple[int, int],
    as_hsv: bool = False,
) -> NDArray[Any]

Extract RGB or HSV pixel values from a rectangular region.

Parameters:

Name Type Description Default
top_left Tuple[int, int]

Top-left corner of the rectangle.

required
bottom_right Tuple[int, int]

Bottom-right corner of the rectangle.

required
as_hsv bool

If True, returns HSV values; otherwise, returns RGB values.

False

Returns:

Type Description
NDArray[Any]

NDArray[Any]: Array of pixel values with shape (n, 3), where n is the number of pixels. If the region is empty or invalid, returns an empty array of shape (0, 3).

load_image

load_image(filename: str) -> NDArray[Any]

Load an image from a file and convert it to RGB format.

Parameters:

Name Type Description Default
filename str

Path to the image file.

required

Returns:

Type Description
NDArray[Any]

NDArray[Any]: The loaded image in RGB format.

Raises:

Type Description
FileNotFoundError

If the file does not exist.