Skip to content

Image

Note

PIL images are read and written using the PIL library under-the-hood. Tensor images are manipulated using the PyTorch library.

stegobox.io.image.read(filename)

Read a PIL image from a file.

Parameters:

Name Type Description Default
filename str

The name or path to the image file.

required

Returns:

Type Description
Image

A PIL image.

Source code in stegobox/io/image.py
def read(filename: str) -> Image.Image:
    """Read a PIL image from a file.

    Args:
        filename: The name or path to the image file.

    Returns:
        A PIL image.
    """
    return Image.open(filename)

stegobox.io.image.write(image, filename, format=None, **args)

Saves a PIL image to the designated file path.

Parameters:

Name Type Description Default
image Image

The PIL image to save.

required
filename str

The name or path of the image file.

required
format Optional[str]

The format of the image - determined by the filename extension if not specified. Defaults to None.

None
Source code in stegobox/io/image.py
def write(
    image: Image.Image, filename: str, format: Optional[str] = None, **args
) -> None:
    """Saves a PIL image to the designated file path.

    Args:
        image: The PIL image to save.
        filename: The name or path of the image file.
        format: The format of the image - determined by the filename extension if not
            specified. Defaults to None.
    """
    image.save(filename, format=format, **args)

stegobox.io.image.write_tensor(image, filename, format=None, **args)

Saves a torch.Tensor image to the designated file path.

Parameters:

Name Type Description Default
image Tensor

The torch.Tensor image to save.

required
filename str

The name or path of the image file.

required
format Optional[str]

The format of the image - determined by the filename extension if not specified. Defaults to None.

None
Source code in stegobox/io/image.py
def write_tensor(
    image: torch.Tensor, filename: str, format: Optional[str] = None, **args
) -> None:
    """Saves a torch.Tensor image to the designated file path.

    Args:
        image: The torch.Tensor image to save.
        filename: The name or path of the image file.
        format: The format of the image - determined by the filename extension if not
            specified. Defaults to None.
    """
    utils.save_image(image, filename, padding=0, format=format, **args)

Tip

You wouldn't normally read a tensor directly, in case you are wondering where is io.image.read_tensor.