Commit 4f475a6d authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): fulfill docstrings

parent 01eb7011
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -801,6 +801,18 @@ def _parse_rescale(obj):


class PillowPadToSize:
    """
    A class for padding images to a specified size.

    This class provides functionality to pad images to a target size while maintaining
    the original image content. It supports various padding colors and interpolation methods.

    :param size: Target size as (width, height) tuple or single integer for square
    :param background_color: Color for padding area (RGB, RGBA, string color name, or integer)
    :param interpolation: PIL interpolation method for resizing
    :type interpolation: int
    """

    def __init__(self, size: Union[Tuple[int, int], int],
                 background_color: Union[str, int, Tuple[int, int, int], Tuple[int, int, int, int]] = 'white',
                 interpolation: int = Image.BILINEAR):
@@ -812,6 +824,16 @@ class PillowPadToSize:
        _parse_color_to_rgba(self.background_color)

    def __call__(self, pic):
        """
        Apply padding transformation to the input image.

        :param pic: Input PIL Image
        :type pic: PIL.Image.Image

        :return: Padded image
        :rtype: PIL.Image.Image
        :raises TypeError: If input is not a PIL Image
        """
        if not isinstance(pic, Image.Image):
            raise TypeError('pic should be PIL Image. Got {}'.format(type(pic)))

@@ -823,6 +845,12 @@ class PillowPadToSize:
        )

    def __repr__(self) -> str:
        """
        Return string representation of the class.

        :return: String representation
        :rtype: str
        """
        interpolate_str = _PILLOW_TO_STR[self.interpolation]
        detail = f"(size={self.size}, interpolation={interpolate_str}, background_color={self.background_color})"
        return f"{self.__class__.__name__}{detail}"
@@ -832,6 +860,19 @@ class PillowPadToSize:
def _create_pad_to_size(size: Union[Tuple[int, int], int],
                        background_color: Union[str, int, Tuple[int, int, int], Tuple[int, int, int, int]] = 'white',
                        interpolation: str = 'bilinear'):
    """
    Factory function to create PillowPadToSize instance.

    :param size: Target size for padding
    :type size: Union[Tuple[int, int], int]
    :param background_color: Color for padding area
    :type background_color: Union[str, int, Tuple[int, int, int], Tuple[int, int, int, int]]
    :param interpolation: Interpolation method name
    :type interpolation: str

    :return: PillowPadToSize instance
    :rtype: PillowPadToSize
    """
    return PillowPadToSize(
        size=size,
        background_color=background_color,
@@ -841,6 +882,16 @@ def _create_pad_to_size(size: Union[Tuple[int, int], int],

@register_pillow_parse('pad_to_size')
def _parse_pad_to_size(obj):
    """
    Parse PillowPadToSize object to dictionary configuration.

    :param obj: Object to parse
    :type obj: Any

    :return: Configuration dictionary
    :rtype: dict
    :raises NotParseTarget: If object is not PillowPadToSize instance
    """
    if not isinstance(obj, PillowPadToSize):
        raise NotParseTarget

+61 −0
Original line number Diff line number Diff line
@@ -78,6 +78,24 @@ def _get_interpolation_mode(value):


def _get_int_from_interpolation_mode(value):
    """
    Convert a torchvision.transforms.InterpolationMode enum value to its corresponding integer representation.

    This function performs the reverse operation of _get_interpolation_mode, converting
    InterpolationMode enum values back to their integer representations.

    :param value: The InterpolationMode enum value to convert
    :type value: InterpolationMode

    :return: The integer representation of the interpolation mode
    :rtype: int

    :raises TypeError: If the input value is not an InterpolationMode enum value

    :examples:
        >>> mode = InterpolationMode.BILINEAR
        >>> _get_int_from_interpolation_mode(mode)  # Returns 2
    """
    from torchvision.transforms import InterpolationMode
    if not isinstance(value, InterpolationMode):
        raise TypeError(
@@ -358,6 +376,14 @@ if _HAS_TORCHVISION:
        """
        Resize and center-pad PIL image to target size with background color.
        TorchVision-compatible transform that can be composed.

        :param size: Target size as (height, width) tuple or single int for square output
        :type size: Union[Tuple[int, int], int]
        :param background_color: Color to use for padding. Can be string name, RGB/RGBA tuple, or single int
        :param interpolation: Interpolation mode for resizing, defaults to BILINEAR
        :type interpolation: InterpolationMode

        :raises ValueError: If size or background_color format is invalid
        """

        def __init__(self, size: Union[Tuple[int, int], int],
@@ -371,6 +397,16 @@ if _HAS_TORCHVISION:
            _parse_color_to_rgba(self.background_color)

        def forward(self, pic):
            """
            Apply padding transform to input image.

            :param pic: Input PIL Image
            :type pic: PIL.Image.Image

            :return: Padded image with target size
            :rtype: PIL.Image.Image
            :raises TypeError: If input is not a PIL Image
            """
            if not isinstance(pic, Image.Image):
                raise TypeError('pic should be PIL Image. Got {}'.format(type(pic)))

@@ -393,6 +429,20 @@ else:
def _create_pad_to_size(size: Union[Tuple[int, int], int],
                        background_color: Union[str, int, Tuple[int, int, int], Tuple[int, int, int, int]] = 'white',
                        interpolation='bilinear'):
    """
    Factory function to create PadToSize transform instance.

    :param size: Target size as (height, width) tuple or single int
    :type size: Union[Tuple[int, int], int]
    :param background_color: Color for padding
    :type background_color: Union[str, int, Tuple[int, int, int], Tuple[int, int, int, int]]
    :param interpolation: Interpolation mode name
    :type interpolation: str

    :return: PadToSize transform instance
    :rtype: PadToSize
    :raises AssertionError: If torchvision is not available
    """
    assert PadToSize is not None
    return PadToSize(
        size=size,
@@ -403,6 +453,17 @@ def _create_pad_to_size(size: Union[Tuple[int, int], int],

@_register_parse('pad_to_size', safe=False)
def _parse_pad_to_size(obj):
    """
    Parse PadToSize transform object for serialization.

    :param obj: Transform object to parse
    :type obj: Any

    :return: Dictionary containing transform parameters
    :rtype: dict
    :raises NotParseTarget: If object is not a PadToSize instance
    :raises AssertionError: If torchvision is not available
    """
    assert PadToSize is not None
    if not isinstance(obj, PadToSize):
        raise NotParseTarget