Skip to content

Writer

The writer module provides ways to write image data.

WriterImage module

WriterImage

WriterImage class that provide methods to save and show the image

Source code in otary/image/components/io/writer.py
class WriterImage:
    """WriterImage class that provide methods to save and show the image"""

    def __init__(self, base: BaseImage) -> None:
        self.base = base

    def show(
        self,
        title: Optional[str] = None,
        figsize: tuple[float, float] = (8.0, 6.0),
        color_conversion: Optional[int] = cv2.COLOR_BGR2RGB,
        save_filepath: Optional[str] = None,
    ) -> None:
        """Show the image

        Args:
            title (Optional[str], optional): title of the image. Defaults to None.
            figsize (tuple[float, float], optional): size of the figure.
                Defaults to (8.0, 6.0).
            color_conversion (int, optional): color conversion parameter.
                Defaults to cv2.COLOR_BGR2RGB.
            save_filepath (Optional[str], optional): save the image if needed.
                Defaults to None.
        """
        # Converts from one colour space to the other. this is needed as RGB
        # is not the default colour space for OpenCV
        if color_conversion is not None:
            im = cv2.cvtColor(self.base.asarray, color_conversion)
        else:
            im = self.base.asarray

        plt.figure(figsize=figsize)

        # Show the image
        plt.imshow(im)

        # remove the axis / ticks for a clean looking image
        plt.xticks([])
        plt.yticks([])

        # if a title is provided, show it
        if title is not None:
            plt.title(title)

        if save_filepath is not None:
            plt.savefig(save_filepath)

        plt.show()

    def save(self, save_filepath: str) -> None:
        """Save the image in a local file

        Args:
            save_filepath (str): path to the file
        """
        self.show(save_filepath=save_filepath)

save(save_filepath)

Save the image in a local file

Parameters:

Name Type Description Default
save_filepath str

path to the file

required
Source code in otary/image/components/io/writer.py
def save(self, save_filepath: str) -> None:
    """Save the image in a local file

    Args:
        save_filepath (str): path to the file
    """
    self.show(save_filepath=save_filepath)

show(title=None, figsize=(8.0, 6.0), color_conversion=cv2.COLOR_BGR2RGB, save_filepath=None)

Show the image

Parameters:

Name Type Description Default
title Optional[str]

title of the image. Defaults to None.

None
figsize tuple[float, float]

size of the figure. Defaults to (8.0, 6.0).

(8.0, 6.0)
color_conversion int

color conversion parameter. Defaults to cv2.COLOR_BGR2RGB.

COLOR_BGR2RGB
save_filepath Optional[str]

save the image if needed. Defaults to None.

None
Source code in otary/image/components/io/writer.py
def show(
    self,
    title: Optional[str] = None,
    figsize: tuple[float, float] = (8.0, 6.0),
    color_conversion: Optional[int] = cv2.COLOR_BGR2RGB,
    save_filepath: Optional[str] = None,
) -> None:
    """Show the image

    Args:
        title (Optional[str], optional): title of the image. Defaults to None.
        figsize (tuple[float, float], optional): size of the figure.
            Defaults to (8.0, 6.0).
        color_conversion (int, optional): color conversion parameter.
            Defaults to cv2.COLOR_BGR2RGB.
        save_filepath (Optional[str], optional): save the image if needed.
            Defaults to None.
    """
    # Converts from one colour space to the other. this is needed as RGB
    # is not the default colour space for OpenCV
    if color_conversion is not None:
        im = cv2.cvtColor(self.base.asarray, color_conversion)
    else:
        im = self.base.asarray

    plt.figure(figsize=figsize)

    # Show the image
    plt.imshow(im)

    # remove the axis / ticks for a clean looking image
    plt.xticks([])
    plt.yticks([])

    # if a title is provided, show it
    if title is not None:
        plt.title(title)

    if save_filepath is not None:
        plt.savefig(save_filepath)

    plt.show()