Skip to content

types

Type Aliases:

  • BotFieldLeftShift

    Type alias for the bottom field's horizontal shift in pixels.

  • BotFieldTopShift

    Type alias for the bottom field's vertical shift in pixels.

  • Center

    Type alias for the center point of the sigmoid curve, determining the midpoint of the transition.

  • FieldShift

    Type alias for shifts in interlaced content.

  • LeftShift

    Type alias for horizontal shift in pixels (left).

  • Slope

    Type alias for the slope of the sigmoid curve, controlling the steepness of the transition.

  • TopFieldLeftShift

    Type alias for the top field's horizontal shift in pixels.

  • TopFieldTopShift

    Type alias for the top field's vertical shift in pixels.

  • TopShift

    Type alias for vertical shift in pixels (top).

Classes:

BotFieldLeftShift

BotFieldLeftShift = float

Type alias for the bottom field's horizontal shift in pixels.

Used when processing interlaced video to describe the horizontal shift of the bottom field.

BotFieldTopShift

BotFieldTopShift = float

Type alias for the bottom field's vertical shift in pixels.

Used when processing interlaced video to describe the vertical shift of the bottom field.

Center

Center = float

Type alias for the center point of the sigmoid curve, determining the midpoint of the transition.

FieldShift

Type alias for shifts in interlaced content.

Represents separate shifts for top and bottom fields.

LeftShift

LeftShift = float

Type alias for horizontal shift in pixels (left).

Represents the amount of horizontal offset when scaling a video.

Slope

Slope = float

Type alias for the slope of the sigmoid curve, controlling the steepness of the transition.

TopFieldLeftShift

TopFieldLeftShift = float

Type alias for the top field's horizontal shift in pixels.

Used when processing interlaced video to describe the horizontal shift of the top field.

TopFieldTopShift

TopFieldTopShift = float

Type alias for the top field's vertical shift in pixels.

Used when processing interlaced video to describe the vertical shift of the top field.

TopShift

TopShift = float

Type alias for vertical shift in pixels (top).

Represents the amount of vertical offset when scaling a video.

BorderHandling

Bases: CustomIntEnum

Border padding strategy used when a clip requires alignment padding.

Methods:

Attributes:

  • MIRROR

    Assume the image was resized with mirror padding.

  • REPEAT

    Assume the image was resized with extend padding, where the outermost row was extended infinitely far.

  • ZERO

    Assume the image was resized with zero padding.

MIRROR class-attribute instance-attribute

MIRROR = 0

Assume the image was resized with mirror padding.

REPEAT class-attribute instance-attribute

REPEAT = 2

Assume the image was resized with extend padding, where the outermost row was extended infinitely far.

ZERO class-attribute instance-attribute

ZERO = 1

Assume the image was resized with zero padding.

from_param classmethod

from_param(value: Any, func_except: FuncExcept | None = None) -> Self

Return the enum value from a parameter.

Parameters:

  • value

    (Any) –

    Value to instantiate the enum class.

  • func_except

    (FuncExcept | None, default: None ) –

    Exception function.

Returns:

  • Self

    Enum value.

Raises:

Source code in jetpytools/enums/base.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@classmethod
def from_param(cls, value: Any, func_except: FuncExcept | None = None) -> Self:
    """
    Return the enum value from a parameter.

    Args:
        value: Value to instantiate the enum class.
        func_except: Exception function.

    Returns:
        Enum value.

    Raises:
        NotFoundEnumValue: Variable not found in the given enum.
    """
    func_except = func_except or cls.from_param

    try:
        return cls(value)
    except (ValueError, TypeError):
        pass

    if isinstance(func_except, tuple):
        func_name, var_name = func_except
    else:
        func_name, var_name = func_except, repr(cls)

    raise NotFoundEnumValueError(
        'The given value for "{var_name}" argument must be a valid {enum_name}, not "{value}"!\n'
        "Valid values are: [{readable_enum}].",
        func_name,
        var_name=var_name,
        enum_name=cls,
        value=value,
        readable_enum=(f"{name} ({value!r})" for name, value in cls.__members__.items()),
        reason=value,
    ) from None

pad_amount cached

pad_amount(size: int, min_amount: int = 2) -> int

Return required padding for one dimension.

MIRROR always returns zero. Other modes pad to an 8-pixel boundary.

Parameters:

  • size

    (int) –

    Dimension size.

  • min_amount

    (int, default: 2 ) –

    Minimum required padding.

Returns:

  • int

    Padding amount.

Source code in vskernels/types.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@cache
def pad_amount(self, size: int, min_amount: int = 2) -> int:
    """
    Return required padding for one dimension.

    MIRROR always returns zero. Other modes pad to an 8-pixel boundary.

    Args:
        size: Dimension size.
        min_amount: Minimum required padding.

    Returns:
        Padding amount.
    """
    if self is BorderHandling.MIRROR:
        return 0

    return (((size + min_amount) + 7) & -8) - size

prepare_clip

prepare_clip(
    clip: VideoNode,
    min_pad: int = 2,
    shift: tuple[TopShift, LeftShift] = (0, 0),
) -> tuple[VideoNode, tuple[TopShift, LeftShift]]

Apply required padding and adjust shift.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • min_pad

    (int, default: 2 ) –

    Minimum padding before alignment.

  • shift

    (tuple[TopShift, LeftShift], default: (0, 0) ) –

    Current (top, left) shift.

Returns:

Source code in vskernels/types.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def prepare_clip(
    self, clip: vs.VideoNode, min_pad: int = 2, shift: tuple[TopShift, LeftShift] = (0, 0)
) -> tuple[vs.VideoNode, tuple[TopShift, LeftShift]]:
    """
    Apply required padding and adjust shift.

    Args:
        clip: Input clip.
        min_pad: Minimum padding before alignment.
        shift: Current (top, left) shift.

    Returns:
        (padded clip, updated shift).
    """
    pad_w, pad_h = (self.pad_amount(size, min_pad) for size in (clip.width, clip.height))

    if pad_w == pad_h == 0:
        return clip, shift

    match self:
        case BorderHandling.ZERO:
            padded = padder.COLOR(clip, pad_w, pad_w, pad_h, pad_h)
        case BorderHandling.REPEAT:
            padded = padder.REPEAT(clip, pad_w, pad_w, pad_h, pad_h)
        case _:
            raise CustomNotImplementedError

    shift = tuple(s + ((p - c) // 2) for s, c, p in zip(shift, *((x.height, x.width) for x in (clip, padded))))

    return padded, shift

value

value() -> int
Source code in jetpytools/enums/base.py
86
87
@enum_property
def value(self) -> int: ...

SampleGridModel

Bases: CustomIntEnum

Sampling grid alignment model.

While match edges will align the edges of the outermost pixels in the target image, match centers will instead align the centers of the outermost pixels.

Here's a visual example for a 3x1 image upsampled to 7x1:

  • Match edges:

    +-------------+-------------+-------------+
    |      ·      |      ·      |      ·      |
    +-------------+-------------+-------------+
    ↓                                         ↓
    +-----+-----+-----+-----+-----+-----+-----+
    |  ·  |  ·  |  ·  |  ·  |  ·  |  ·  |  ·  |
    +-----+-----+-----+-----+-----+-----+-----+
    

  • Match centers:

    +-----------------+-----------------+-----------------+
    |        ·        |        ·        |        ·        |
    +-----------------+-----------------+-----------------+
             ↓                                   ↓
          +-----+-----+-----+-----+-----+-----+-----+
          |  ·  |  ·  |  ·  |  ·  |  ·  |  ·  |  ·  |
          +-----+-----+-----+-----+-----+-----+-----+
    

For a more detailed explanation, refer to this page: https://entropymine.com/imageworsener/matching/.

The formula for calculating values we can use during desampling is simple:

  • width: base_width * (target_width - 1) / (base_width - 1)
  • height: base_height * (target_height - 1) / (base_height - 1)

Methods:

  • __call__

    Apply sampling model to sizes and shift.

  • for_dst

    Apply grid model using destination sizes.

  • for_src

    Apply grid model using source sizes.

  • from_param

    Return the enum value from a parameter.

  • value

Attributes:

MATCH_CENTERS class-attribute instance-attribute

MATCH_CENTERS = 1

Align pixel centers.

MATCH_EDGES class-attribute instance-attribute

MATCH_EDGES = 0

Align edges.

__call__

Apply sampling model to sizes and shift.

Parameters:

  • width

    (int) –

    Destination width.

  • height

    (int) –

    Destination height.

  • src_width

    (float) –

    Current source width.

  • src_height

    (float) –

    Current source height.

  • shift

    (tuple[float, float]) –

    (x, y) sampling shift.

  • kwargs

    (dict[str, Any]) –

    Parameter dict to update.

Returns:

Source code in vskernels/types.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def __call__(
    self,
    width: int,
    height: int,
    src_width: float,
    src_height: float,
    shift: tuple[float, float],
    kwargs: dict[str, Any],
) -> tuple[dict[str, Any], tuple[float, float]]:
    """
    Apply sampling model to sizes and shift.

    Args:
        width: Destination width.
        height: Destination height.
        src_width: Current source width.
        src_height: Current source height.
        shift: (x, y) sampling shift.
        kwargs: Parameter dict to update.

    Returns:
        (updated kwargs, updated shift).
    """

    if self is SampleGridModel.MATCH_CENTERS:
        src_width = src_width * (width - 1) / (src_width - 1)
        src_height = src_height * (height - 1) / (src_height - 1)

        kwargs |= {"src_width": src_width, "src_height": src_height}
        shift_x, shift_y, *_ = tuple(
            (x / 2 + y for x, y in zip(((height - src_height), (width - src_width)), shift))
        )
        shift = shift_x, shift_y

    return kwargs, shift

for_dst

for_dst(
    clip: VideoNode,
    width: int,
    height: int,
    shift: tuple[float, float],
    **kwargs: Any
) -> tuple[dict[str, Any], tuple[float, float]]

Apply grid model using destination sizes.

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • width

    (int) –

    Destination width.

  • height

    (int) –

    Destination height.

  • shift

    (tuple[float, float]) –

    Current shift.

  • **kwargs

    (Any, default: {} ) –

    Optional src_width/src_height.

Returns:

Source code in vskernels/types.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def for_dst(
    self, clip: vs.VideoNode, width: int, height: int, shift: tuple[float, float], **kwargs: Any
) -> tuple[dict[str, Any], tuple[float, float]]:
    """
    Apply grid model using destination sizes.

    Args:
        clip: Source clip.
        width: Destination width.
        height: Destination height.
        shift: Current shift.
        **kwargs: Optional src_width/src_height.

    Returns:
        (updated kwargs, updated shift).
    """

    src_width = kwargs.get("src_width", width)
    src_height = kwargs.get("src_height", height)

    return self(src_width, src_height, width, height, shift, kwargs)

for_src

for_src(
    clip: VideoNode,
    width: int,
    height: int,
    shift: tuple[float, float],
    **kwargs: Any
) -> tuple[dict[str, Any], tuple[float, float]]

Apply grid model using source sizes.

Parameters:

  • clip

    (VideoNode) –

    Source clip (fallback for src dimensions).

  • width

    (int) –

    Source width.

  • height

    (int) –

    Source height.

  • shift

    (tuple[float, float]) –

    Current shift.

  • **kwargs

    (Any, default: {} ) –

    Optional overrides.

Returns:

Source code in vskernels/types.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def for_src(
    self, clip: vs.VideoNode, width: int, height: int, shift: tuple[float, float], **kwargs: Any
) -> tuple[dict[str, Any], tuple[float, float]]:
    """
    Apply grid model using source sizes.

    Args:
        clip: Source clip (fallback for src dimensions).
        width: Source width.
        height: Source height.
        shift: Current shift.
        **kwargs: Optional overrides.

    Returns:
        (updated kwargs, updated shift).
    """

    src_width = kwargs.get("src_width", clip.width)
    src_height = kwargs.get("src_height", clip.height)

    return self(width, height, src_width, src_height, shift, kwargs)

from_param classmethod

from_param(value: Any, func_except: FuncExcept | None = None) -> Self

Return the enum value from a parameter.

Parameters:

  • value

    (Any) –

    Value to instantiate the enum class.

  • func_except

    (FuncExcept | None, default: None ) –

    Exception function.

Returns:

  • Self

    Enum value.

Raises:

Source code in jetpytools/enums/base.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@classmethod
def from_param(cls, value: Any, func_except: FuncExcept | None = None) -> Self:
    """
    Return the enum value from a parameter.

    Args:
        value: Value to instantiate the enum class.
        func_except: Exception function.

    Returns:
        Enum value.

    Raises:
        NotFoundEnumValue: Variable not found in the given enum.
    """
    func_except = func_except or cls.from_param

    try:
        return cls(value)
    except (ValueError, TypeError):
        pass

    if isinstance(func_except, tuple):
        func_name, var_name = func_except
    else:
        func_name, var_name = func_except, repr(cls)

    raise NotFoundEnumValueError(
        'The given value for "{var_name}" argument must be a valid {enum_name}, not "{value}"!\n'
        "Valid values are: [{readable_enum}].",
        func_name,
        var_name=var_name,
        enum_name=cls,
        value=value,
        readable_enum=(f"{name} ({value!r})" for name, value in cls.__members__.items()),
        reason=value,
    ) from None

value

value() -> int
Source code in jetpytools/enums/base.py
86
87
@enum_property
def value(self) -> int: ...