def figure_to_array(fig: Figure) -> np.ndarray:
    """Convert matplotlib figure to numpy array.

    Args:
        fig (Figure): Matplotlib figure to convert

    Returns:
        np.ndarray: RGB image array

    Examples:
        >>> import matplotlib.pyplot as plt
        >>> fig = plt.figure()
        >>> plt.plot([1, 2, 3])
        >>> img = figure_to_array(fig)
        >>> type(img)
        <class 'numpy.ndarray'>
    """
    fig.canvas.draw()
    # convert figure to np.ndarray for saving via visualizer
    img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    return img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
Convert matplotlib figure to numpy array.

    Args:
        fig (Figure): Matplotlib figure to convert

    Returns:
        np.ndarray: RGB image array

    Examples:
        >>> import matplotlib.pyplot as plt
        >>> fig = plt.figure()
        >>> plt.plot([1, 2, 3])
        >>> img = figure_to_array(fig)
        >>> type(img)
        <class 'numpy.ndarray'>
    
src/anomalib/data/utils/image.py