Exemplo n.º 1
0
    def test_edge_to_image_size_vert_horz(self):
        aspect_ratio = 2.0
        edge_size = 2

        actual = utils.edge_to_image_size(edge_size, aspect_ratio, edge="vert")
        desired = (edge_size, round(edge_size * aspect_ratio))
        self.assertTupleEqual(actual, desired)

        actual = utils.edge_to_image_size(edge_size, aspect_ratio, edge="horz")
        desired = (round(edge_size * aspect_ratio), edge_size)
        self.assertTupleEqual(actual, desired)
Exemplo n.º 2
0
    def test_calculate_resized_image_size_long(self):
        edge_size = 2
        edge = "long"

        aspect_ratio = 2.0
        actual = utils.edge_to_image_size(edge_size, aspect_ratio, edge)
        desired = (round(edge_size / aspect_ratio), edge_size)
        self.assertTupleEqual(actual, desired)

        aspect_ratio = 0.5
        actual = utils.edge_to_image_size(edge_size, aspect_ratio, edge)
        desired = (edge_size, round(edge_size * aspect_ratio))
        self.assertTupleEqual(actual, desired)
Exemplo n.º 3
0
    def test_edge_to_image_size_short(self):
        edge_size = 2
        edge = "short"

        aspect_ratio = 2.0
        actual = utils.edge_to_image_size(edge_size, aspect_ratio, edge)
        desired = (edge_size, round(edge_size * aspect_ratio))
        self.assertTupleEqual(actual, desired)

        aspect_ratio = 0.5
        actual = utils.edge_to_image_size(edge_size, aspect_ratio, edge)
        desired = (round(edge_size / aspect_ratio), edge_size)
        self.assertTupleEqual(actual, desired)
Exemplo n.º 4
0
def resize(
    image: torch.Tensor,
    size: Union[int, Tuple[int, int]],
    edge: str = "short",
    aspect_ratio: Optional[float] = None,
    interpolation_mode: str = "bilinear",
) -> torch.Tensor:
    r"""Resize an image as specified. See :class:`~pystiche.image.transforms.Resize`
    for details.
    """
    if aspect_ratio is None:
        aspect_ratio = extract_aspect_ratio(image)

    if is_image_size(size):
        image_size = size
    elif is_edge_size(size):
        image_size = edge_to_image_size(cast(int, size), aspect_ratio, edge)
    else:
        raise RuntimeError

    return interpolate(
        image,
        size=image_size,
        scale_factor=None,
        mode=interpolation_mode,
    )
Exemplo n.º 5
0
    def test_read_image_resize_scalar(self):
        edge_size = 200

        image = self.load_image(backend="PIL")
        aspect_ratio = utils.calculate_aspect_ratio(
            (image.height, image.width))
        image_size = utils.edge_to_image_size(edge_size, aspect_ratio)

        actual = io.read_image(self.default_image_file(), size=edge_size)
        desired = image.resize(image_size[::-1])
        self.assertImagesAlmostEqual(actual, desired)
Exemplo n.º 6
0
def resize(
    image: torch.Tensor,
    size: Union[int, Tuple[int, int]],
    edge: str = "short",
    aspect_ratio: Optional[float] = None,
    interpolation_mode: str = "bilinear",
) -> torch.Tensor:
    if aspect_ratio is None:
        aspect_ratio = extract_aspect_ratio(image)

    if is_image_size(size):
        image_size = size
    elif is_edge_size(size):
        image_size = edge_to_image_size(cast(int, size), aspect_ratio, edge)
    else:
        raise RuntimeError

    return interpolate(
        image,
        size=image_size,
        scale_factor=None,
        mode=interpolation_mode,
    )