pystiche.image

Utilities

pystiche.image.calculate_aspect_ratio(image_size)
Return type

float

pystiche.image.image_to_edge_size(image_size, edge='short')
Return type

int

pystiche.image.edge_to_image_size(edge_size, aspect_ratio, edge='short')
Return type

Tuple[int, int]

pystiche.image.extract_batch_size(x)
Return type

int

pystiche.image.extract_num_channels(x)
Return type

int

pystiche.image.extract_image_size(x)
Return type

Tuple[int, int]

pystiche.image.extract_edge_size(x, edge='short')
Return type

int

pystiche.image.extract_aspect_ratio(x)
Return type

float

I/O

pystiche.image.read_image(file, device='cpu', make_batched=True, size=None, interpolation_mode='bilinear')

Read an image from file with PIL.Image and return it as Tensor .

Parameters
  • file (str) – Path to image file to be read.

  • device (Union[device, str]) – Device that the image is transferred to. Defaults to CPU.

  • make_batched (bool) – If True, a fake batch dimension is added to the image.

  • size (Optional[Union[int]]) – Optional size the image is resized to.

  • interpolation_mode (str) – Interpolation mode that is used to perform the optional resizing. Valid modes are "nearest", "bilinear", and "bicubic". Defaults to "bilinear".

Return type

Tensor

pystiche.image.write_image(image, file, mode=None, **save_kwargs)

Write a Tensor image to a file with PIL.Image .

Parameters
Return type

None

pystiche.image.show_image(image, title=None, mode=None, size=None, interpolation_mode='bilinear')

Show an image and optionally read it from file first.

Note

show_image uses matplotlib.pyplot.imshow() as primary means to show images. If that is not available the native PIL.Image.Image.show() is used as a fallback.

Parameters
  • image (Union[Tensor, str]) – Image to be shown. If str this is treated as a path to an image and is read by read_image() .

  • title (Optional[str]) – Optional title of the image.

  • mode (Optional[str]) –

    Optional image mode. See the Pillow documentation for details.

  • size (Optional[Union[int]]) – Optional size the image is resized to.

  • interpolation_mode (str) – Interpolation mode that is used to perform the optional resizing. Valid modes are "nearest", "bilinear", and "bicubic". Defaults to "bilinear".

Return type

None

Guides

pystiche.image.verify_guides(guides, verify_coverage=True, verify_overlap=True)

Verify if guides cover the whole canvas and if they do not overlap with each other.

Parameters
  • guides (Dict[str, Tensor]) – Guides to be verified.

  • verify_coverage (bool) – If True verifies that the guides cover the whole canvas. Defaults to True.

  • verify_overlap (bool) – If True verifies that the guides do not overlap with each other. Defaults to True.

Raises

RuntimeError – If at least on check is not successful.

Return type

None

pystiche.image.read_guides(dir, device='cpu', make_batched=True, size=None, interpolation_mode='nearest')

Read all guides from a directory using read_image() and return them as dictionary. The filename without extensions is used as region key.

Parameters
  • dir (str) – Path to root directory of the guide files.

  • device (Union[device, str]) – Device that the guides are transferred to. Defaults to CPU.

  • make_batched (bool) – If True, a fake batch dimension is added to every guide.

  • size (Optional[Union[int]]) – Optional size the guides are resized to.

  • interpolation_mode (str) – Interpolation mode that is used to perform the optional resizing. Valid modes are "nearest", "bilinear", and "bicubic". Defaults to "nearest".

Return type

Dict[str, Tensor]

pystiche.image.write_guides(guides, dir, ext='.png', mode='L', **save_kwargs)

Write guides to directory using write_image(). The region key is used as filename.

Parameters
  • guides (Dict[str, Tensor]) – Guides to be written.

  • dir (str) – Path to root directory of the guide files.

  • ext (str) – Extension that is appended to the filename. Defaults to ".png".

  • mode (str) –

    Optional image mode. See the Pillow documentation for details. Defaults to "L".

  • **save_kwargs – Other parameters that are passed to PIL.Image.Image.save() .

Return type

None

pystiche.image.guides_to_segmentation(guides, color_map=None)

Combines multiple guides into one segmentation image.

Parameters
Return type

Tensor

pystiche.image.segmentation_to_guides(seg, region_map=None)

Splits a segmentation image in multiple guides.

Parameters
  • seg (Tensor) – Segmentation image to be split.

  • region_map (Optional[Dict[Tuple[int, int, int], str]]) – Optional mapping from RGB triplets to regions. If omitted, the RGB triplets are used as key in the output.

Return type

Dict[Union[Tuple[int, int, int], str], Tensor]