garmentiq.landmark.plot

Displaying an image with landmark coordinates overlaid.

 1"""Displaying an image with landmark coordinates overlaid."""
 2import matplotlib.pyplot as plt
 3import numpy as np
 4from PIL import Image
 5
 6
 7def plot(
 8    image_path: str,
 9    coordinate: np.ndarray = None,
10    figsize: tuple = (6, 6),
11    color: str = "red",
12):
13    """
14    Display an image from a file path using matplotlib, with optional overlay of coordinates.
15
16    This function loads an image from the given file path, displays it using matplotlib,
17    and optionally overlays coordinate points on the image.
18
19    Args:
20        image_path (str): Path to the image file. The image will be loaded as RGB.
21        coordinate (np.ndarray, optional): Optional array of coordinates to overlay on the image.
22                                          Expected shape: (1, N, 2), where N is the number of points.
23        figsize (tuple, optional): Size of the displayed figure in inches (width, height).
24        color (str, optional): Color of the overlay points. Default is 'red'.
25
26    Raises:
27        ValueError: If image cannot be loaded or coordinate format is invalid.
28
29    Returns:
30        None
31    """
32    try:
33        image_np = np.array(Image.open(image_path).convert("RGB"))
34    except Exception as e:
35        raise ValueError(f"Unable to load image from path: {image_path}. Error: {e}")
36
37    plt.figure(figsize=figsize)
38
39    if image_np.ndim == 2:
40        plt.imshow(image_np, cmap="gray")
41    else:
42        plt.imshow(image_np)
43
44    if coordinate is not None:
45        try:
46            plt.scatter(coordinate[0][:, 0], coordinate[0][:, 1], c=color, s=10)
47        except Exception as e:
48            raise ValueError(f"Invalid coordinate format: {coordinate}. Error: {e}")
49
50    plt.axis("off")
51    plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
52    plt.show()
def plot( image_path: str, coordinate: numpy.ndarray = None, figsize: tuple = (6, 6), color: str = 'red'):
 8def plot(
 9    image_path: str,
10    coordinate: np.ndarray = None,
11    figsize: tuple = (6, 6),
12    color: str = "red",
13):
14    """
15    Display an image from a file path using matplotlib, with optional overlay of coordinates.
16
17    This function loads an image from the given file path, displays it using matplotlib,
18    and optionally overlays coordinate points on the image.
19
20    Args:
21        image_path (str): Path to the image file. The image will be loaded as RGB.
22        coordinate (np.ndarray, optional): Optional array of coordinates to overlay on the image.
23                                          Expected shape: (1, N, 2), where N is the number of points.
24        figsize (tuple, optional): Size of the displayed figure in inches (width, height).
25        color (str, optional): Color of the overlay points. Default is 'red'.
26
27    Raises:
28        ValueError: If image cannot be loaded or coordinate format is invalid.
29
30    Returns:
31        None
32    """
33    try:
34        image_np = np.array(Image.open(image_path).convert("RGB"))
35    except Exception as e:
36        raise ValueError(f"Unable to load image from path: {image_path}. Error: {e}")
37
38    plt.figure(figsize=figsize)
39
40    if image_np.ndim == 2:
41        plt.imshow(image_np, cmap="gray")
42    else:
43        plt.imshow(image_np)
44
45    if coordinate is not None:
46        try:
47            plt.scatter(coordinate[0][:, 0], coordinate[0][:, 1], c=color, s=10)
48        except Exception as e:
49            raise ValueError(f"Invalid coordinate format: {coordinate}. Error: {e}")
50
51    plt.axis("off")
52    plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
53    plt.show()

Display an image from a file path using matplotlib, with optional overlay of coordinates.

This function loads an image from the given file path, displays it using matplotlib, and optionally overlays coordinate points on the image.

Arguments:
  • image_path (str): Path to the image file. The image will be loaded as RGB.
  • coordinate (np.ndarray, optional): Optional array of coordinates to overlay on the image. Expected shape: (1, N, 2), where N is the number of points.
  • figsize (tuple, optional): Size of the displayed figure in inches (width, height).
  • color (str, optional): Color of the overlay points. Default is 'red'.
Raises:
  • ValueError: If image cannot be loaded or coordinate format is invalid.
Returns:

None