def generate(self) -> np.ndarray:
        """Generate the image grid.

        Returns:
            np.ndarray: Image consisting of a grid of added images and their
            titles.

        Note:
            Uses Agg backend to avoid issues with dimension mismatch when using
            backends like MacOSX.
        """
        num_cols = len(self.images)
        figure_size = (num_cols * 5, 5)

        # Use Agg backend. This method fails when using backend like MacOSX which might be automatically selected
        # The dimension of image returned by tostring_rgb() does not match the dimension of the canvas
        matplotlib.use("Agg")

        self.figure, self.axis = plt.subplots(1, num_cols, figsize=figure_size)
        self.figure.subplots_adjust(right=0.9)

        axes = self.axis if isinstance(self.axis, np.ndarray) else np.array([self.axis])
        for axis, image_dict in zip(axes, self.images, strict=True):
            axis.axes.xaxis.set_visible(b=False)
            axis.axes.yaxis.set_visible(b=False)
            axis.imshow(image_dict["image"], image_dict["color_map"], vmin=0, vmax=255)
            if image_dict["title"] is not None:
                axis.title.set_text(image_dict["title"])
        self.figure.canvas.draw()
        # convert canvas to numpy array to prepare for visualization with opencv
        img = np.array(self.figure.canvas.buffer_rgba(), dtype=np.uint8)[..., :3]
        plt.close(self.figure)
        return img
Generate the image grid.

        Returns:
            np.ndarray: Image consisting of a grid of added images and their
            titles.

        Note:
            Uses Agg backend to avoid issues with dimension mismatch when using
            backends like MacOSX.
        
src/anomalib/utils/visualization/image.py