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,
        figsize: tuple[float, float] = (-1, -1),
        popup_window_display: bool = False,
    ) -> ImagePIL.Image:
        """Show the image

        Args:
            figsize (tuple[float, float], optional): size of the figure.
                Defaults to (-1, -1), meaning the original size of the image.
            popup_window_display (bool, optional): whether to display the image in a
                popup window. Defaults to False.
        """
        if figsize[0] <= 0 and figsize[1] <= 0:
            figsize = (self.base.width, self.base.height)
        else:
            if figsize[1] <= 0:
                aspect_ratio = self.base.height / self.base.width
                figsize = (figsize[0], figsize[0] * aspect_ratio)

            elif figsize[0] <= 0:
                aspect_ratio = self.base.width / self.base.height
                figsize = (figsize[1] * aspect_ratio, figsize[1])

        figsize = (int(figsize[0]), int(figsize[1]))

        self.base.as_reversed_color_channel()
        im = self.base.as_pil().resize(size=figsize)

        if popup_window_display:
            im.show()

        return im

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

        Args:
            fp (str): fp stands for filepath which is the path to the file
        """
        self.base.as_reversed_color_channel().as_pil().save(fp)

save(fp)

Save the image in a local file

Parameters:

Name Type Description Default
fp str

fp stands for filepath which is the path to the file

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

    Args:
        fp (str): fp stands for filepath which is the path to the file
    """
    self.base.as_reversed_color_channel().as_pil().save(fp)

show(figsize=(-1, -1), popup_window_display=False)

Show the image

Parameters:

Name Type Description Default
figsize tuple[float, float]

size of the figure. Defaults to (-1, -1), meaning the original size of the image.

(-1, -1)
popup_window_display bool

whether to display the image in a popup window. Defaults to False.

False
Source code in otary/image/components/io/writer.py
def show(
    self,
    figsize: tuple[float, float] = (-1, -1),
    popup_window_display: bool = False,
) -> ImagePIL.Image:
    """Show the image

    Args:
        figsize (tuple[float, float], optional): size of the figure.
            Defaults to (-1, -1), meaning the original size of the image.
        popup_window_display (bool, optional): whether to display the image in a
            popup window. Defaults to False.
    """
    if figsize[0] <= 0 and figsize[1] <= 0:
        figsize = (self.base.width, self.base.height)
    else:
        if figsize[1] <= 0:
            aspect_ratio = self.base.height / self.base.width
            figsize = (figsize[0], figsize[0] * aspect_ratio)

        elif figsize[0] <= 0:
            aspect_ratio = self.base.width / self.base.height
            figsize = (figsize[1] * aspect_ratio, figsize[1])

    figsize = (int(figsize[0]), int(figsize[1]))

    self.base.as_reversed_color_channel()
    im = self.base.as_pil().resize(size=figsize)

    if popup_window_display:
        im.show()

    return im