Skip to content

utils

Functions:

  • freeze_replace_squaremask
  • max_planes
  • normalize_mask

    Normalize any mask type to match the format and range of the input clip.

  • region_abs_mask

    Generates a mask that defines a rectangular region within the clip, replacing pixels inside or outside the region,

  • region_rel_mask

    Generates a mask that defines a rectangular region within the clip, replacing pixels inside or outside the region,

  • rekt_partial

    Creates a rectangular mask to apply fixes only within the masked area,

  • replace_squaremask

    Replace an area of the frame with another clip using a simple square mask.

  • squaremask

    Create a square used for simple masking.

RegionMask

RegionMask(func: Callable[P, R])

Class decorator that wraps region_rel_mask and region_abs_mask function and extends their functionality.

It is not meant to be used directly.

Methods:

  • __call__
  • expr

    Get the internal expr used for regioning.

Source code in vsmasktools/utils.py
62
63
def __init__(self, func: Callable[P, R]) -> None:
    self._func = func

__call__

__call__(*args: P.args, **kwargs: P.kwargs) -> R
Source code in vsmasktools/utils.py
65
66
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
    return self._func(*args, **kwargs)

expr

expr() -> str

Get the internal expr used for regioning.

Returns:

  • str

    Expression.

Source code in vsmasktools/utils.py
68
69
70
71
72
73
74
75
76
@cachedproperty
def expr(self) -> str:
    """
    Get the internal expr used for regioning.

    Returns:
        Expression.
    """
    return "X {left} < X {right} > or Y {top} < Y {bottom} > or or {replace_out} {replace_in} ?"

RektPartial

RektPartial(rekt_partial: Callable[P, R])

Class decorator that wraps the rekt_partial function and extends its functionality.

It is not meant to be used directly.

Methods:

  • __call__
  • abs

    Creates a rectangular mask to apply fixes only within the masked area,

  • rel
Source code in vsmasktools/utils.py
418
419
def __init__(self, rekt_partial: Callable[P, R]) -> None:
    self._func = rekt_partial

__call__

__call__(*args: P.args, **kwargs: P.kwargs) -> R
Source code in vsmasktools/utils.py
421
422
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
    return self._func(*args, **kwargs)

abs

abs(
    clip: VideoNode,
    func: Callable[Concatenate[VideoNode, ...], VideoNode],
    width: int,
    height: int,
    offset_x: int = 0,
    offset_y: int = 0,
    *args: Any,
    **kwargs: Any
) -> VideoNode

Creates a rectangular mask to apply fixes only within the masked area, significantly speeding up filters like anti-aliasing and scaling.

Parameters:

  • clip

    (VideoNode) –

    The source video clip to which the mask will be applied.

  • func

    (Callable[Concatenate[VideoNode, ...], VideoNode]) –

    The function to be applied within the masked area.

  • width

    (int) –

    The width of the rectangular mask.

  • height

    (int) –

    The height of the rectangular mask.

  • offset_x

    (int, default: 0 ) –

    The horizontal offset of the mask from the top-left corner, defaults to 0.

  • offset_y

    (int, default: 0 ) –

    The vertical offset of the mask from the top-left corner, defaults to 0.

Returns:

  • VideoNode

    A new clip with the applied mask.

Source code in vsmasktools/utils.py
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
def abs(
    self,
    clip: vs.VideoNode,
    func: Callable[Concatenate[vs.VideoNode, ...], vs.VideoNode],
    width: int,
    height: int,
    offset_x: int = 0,
    offset_y: int = 0,
    *args: Any,
    **kwargs: Any,
) -> vs.VideoNode:
    """
    Creates a rectangular mask to apply fixes only within the masked area,
    significantly speeding up filters like anti-aliasing and scaling.

    Args:
        clip: The source video clip to which the mask will be applied.
        func: The function to be applied within the masked area.
        width: The width of the rectangular mask.
        height: The height of the rectangular mask.
        offset_x: The horizontal offset of the mask from the top-left corner, defaults to 0.
        offset_y: The vertical offset of the mask from the top-left corner, defaults to 0.

    Returns:
        A new clip with the applied mask.
    """
    nargs = (clip, func, offset_x, clip.width - width - offset_x, offset_y, clip.height - height - offset_y)
    return self._func(*nargs, *args, **kwargs)  # type: ignore[return-value, arg-type]

rel

rel(*args: P.args, **kwargs: P.kwargs) -> R
Source code in vsmasktools/utils.py
424
425
def rel(self, *args: P.args, **kwargs: P.kwargs) -> R:
    return self._func(*args, **kwargs)

freeze_replace_squaremask

freeze_replace_squaremask(
    mask: VideoNode,
    insert: VideoNode,
    mask_params: tuple[int, int, int, int],
    frame: int,
    frame_range: tuple[int, int],
) -> VideoNode
Source code in vsmasktools/utils.py
298
299
300
301
302
303
304
305
306
307
308
309
def freeze_replace_squaremask(
    mask: vs.VideoNode,
    insert: vs.VideoNode,
    mask_params: tuple[int, int, int, int],
    frame: int,
    frame_range: tuple[int, int],
) -> vs.VideoNode:
    start, end = frame_range

    masked_insert = replace_squaremask(mask[frame], insert[frame], mask_params)

    return insert_clip(mask, masked_insert * (end - start + 1), start)

max_planes

max_planes(
    *_clips: VideoNode | Iterable[VideoNode], resizer: KernelLike = Bilinear
) -> VideoNode
Source code in vsmasktools/utils.py
44
45
46
47
48
49
50
51
def max_planes(*_clips: vs.VideoNode | Iterable[vs.VideoNode], resizer: KernelLike = Bilinear) -> vs.VideoNode:
    clips = flatten_vnodes(_clips)

    resizer = Kernel.ensure_obj(resizer, max_planes)

    width, height, fmt = clips[0].width, clips[0].height, clips[0].format.replace(subsampling_w=0, subsampling_h=0)

    return ExprOp.MAX.combine(split(resizer.scale(clip, width, height, format=fmt)) for clip in clips)

normalize_mask

normalize_mask(
    mask: VideoNode, clip: VideoNode, *, func: FuncExcept | None = None
) -> VideoNode
normalize_mask(
    mask: Callable[[VideoNode, VideoNode], VideoNode],
    clip: VideoNode,
    ref: VideoNode,
    *,
    func: FuncExcept | None = None
) -> VideoNode
normalize_mask(
    mask: EdgeDetectLike | RidgeDetectLike,
    clip: VideoNode,
    *,
    ridge: bool = ...,
    func: FuncExcept | None = None,
    **kwargs: Any
) -> VideoNode
normalize_mask(
    mask: GeneralMask,
    clip: VideoNode,
    ref: VideoNode,
    *,
    func: FuncExcept | None = None
) -> VideoNode
normalize_mask(
    mask: MaskLike,
    clip: VideoNode,
    ref: VideoNode | None = ...,
    *,
    ridge: bool = ...,
    func: FuncExcept | None = None,
    **kwargs: Any
) -> VideoNode
normalize_mask(
    mask: MaskLike,
    clip: VideoNode,
    ref: VideoNode | None = None,
    *,
    ridge: bool = False,
    func: FuncExcept | None = None,
    **kwargs: Any
) -> VideoNode

Normalize any mask type to match the format and range of the input clip.

Parameters:

  • mask

    (MaskLike) –

    The mask to normalize. Can be:

    • A VideoNode representing a precomputed mask.
    • A callable that takes (clip, ref) and returns a VideoNode.
    • An EdgeDetect or RidgeDetect instance or type.
    • A GeneralMask instance.
  • clip

    (VideoNode) –

    The clip to which the output mask will be normalized.

  • ref

    (VideoNode | None, default: None ) –

    A reference clip required by certain mask functions or classes.

  • ridge

    (bool, default: False ) –

    If True and mask is a RidgeDetect instance, generate a ridge mask instead of an edge mask. Defaults to False.

  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

  • **kwargs

    (Any, default: {} ) –

    Additional keyword arguments passed to the edge/ridge detection methods.

Raises:

  • CustomValueError

    If mask is a callable that requires a reference and ref is not provided.

Returns:

  • VideoNode

    A mask with the same format as clip.

Source code in vsmasktools/utils.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
def normalize_mask(
    mask: MaskLike,
    clip: vs.VideoNode,
    ref: vs.VideoNode | None = None,
    *,
    ridge: bool = False,
    func: FuncExcept | None = None,
    **kwargs: Any,
) -> vs.VideoNode:
    """
    Normalize any mask type to match the format and range of the input clip.

    Args:
        mask: The mask to normalize. Can be:

               - A `VideoNode` representing a precomputed mask.
               - A callable that takes `(clip, ref)` and returns a `VideoNode`.
               - An `EdgeDetect` or `RidgeDetect` instance or type.
               - A `GeneralMask` instance.
        clip: The clip to which the output mask will be normalized.
        ref: A reference clip required by certain mask functions or classes.
        ridge: If `True` and `mask` is a `RidgeDetect` instance, generate a ridge mask instead of an edge mask.
            Defaults to `False`.
        func: Function returned for custom error handling. This should only be set by VS package developers.
        **kwargs: Additional keyword arguments passed to the edge/ridge detection methods.

    Raises:
        CustomValueError: If `mask` is a callable that requires a reference and `ref` is not provided.

    Returns:
        A mask with the same format as `clip`.
    """
    func = func or normalize_mask

    if isinstance(mask, (str, type)):
        return normalize_mask(EdgeDetect.ensure_obj(mask, func), clip, ref, ridge=ridge, func=func, **kwargs)

    if isinstance(mask, EdgeDetect):
        if ridge and isinstance(mask, RidgeDetect):
            cmask = mask.ridgemask(clip, **kwargs)
        else:
            cmask = mask.edgemask(clip, **kwargs)
    elif isinstance(mask, GeneralMask):
        cmask = mask.get_mask(clip, ref)
    elif callable(mask):
        if ref is None:
            raise CustomValueError("This mask function requires a ref to be specified!", func)

        cmask = mask(clip, ref)
    else:
        cmask = mask

    return depth(cmask, clip, range_in=ColorRange.FULL, range_out=ColorRange.FULL)

region_abs_mask

region_abs_mask(
    clip: VideoNode,
    width: int,
    height: int,
    left: int = 0,
    top: int = 0,
    replace_in: SupportsString | None = None,
    replace_out: SupportsString | None = None,
    planes: Planes = None,
    func: FuncExcept | None = None,
) -> VideoNode

Generates a mask that defines a rectangular region within the clip, replacing pixels inside or outside the region, using absolute coordinates.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • width

    (int) –

    The width of the region.

  • height

    (int) –

    The height of the region.

  • left

    (int, default: 0 ) –

    Specifies how many pixels to crop from the left side. Defaults to 0.

  • top

    (int, default: 0 ) –

    Specifies how many pixels to crop from the top side. Defaults to 0.

  • replace_in

    (SupportsString | None, default: None ) –

    Pixel value or expression to fill inside the region. Defaults to using the original pixel values.

  • replace_out

    (SupportsString | None, default: None ) –

    Pixel value or expression to fill outside the region. Defaults to the lowest possible values per plane.

  • planes

    (Planes, default: None ) –

    Which planes to process. Defaults to all.

  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

Returns:

  • VideoNode

    A new clip with the specified rectangular region masked in or out.

Source code in vsmasktools/utils.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
@RegionMask
def region_abs_mask(
    clip: vs.VideoNode,
    width: int,
    height: int,
    left: int = 0,
    top: int = 0,
    replace_in: SupportsString | None = None,
    replace_out: SupportsString | None = None,
    planes: Planes = None,
    func: FuncExcept | None = None,
) -> vs.VideoNode:
    """
    Generates a mask that defines a rectangular region within the clip, replacing pixels inside or outside the region,
    using absolute coordinates.

    Args:
        clip: Input clip.
        width: The width of the region.
        height: The height of the region.
        left: Specifies how many pixels to crop from the left side. Defaults to 0.
        top: Specifies how many pixels to crop from the top side. Defaults to 0.
        replace_in: Pixel value or expression to fill inside the region.
            Defaults to using the original pixel values.
        replace_out: Pixel value or expression to fill outside the region.
            Defaults to the lowest possible values per plane.
        planes: Which planes to process. Defaults to all.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A new clip with the specified rectangular region masked in or out.
    """
    return region_rel_mask(
        clip,
        left,
        clip.width - width - left,
        top,
        clip.height - height - top,
        replace_in,
        replace_out,
        planes,
        func or region_abs_mask,
    )

region_rel_mask

region_rel_mask(
    clip: VideoNode,
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    replace_in: SupportsString | None = None,
    replace_out: SupportsString | None = None,
    planes: Planes = None,
    func: FuncExcept | None = None,
) -> VideoNode

Generates a mask that defines a rectangular region within the clip, replacing pixels inside or outside the region, using relative coordinates.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • left

    (int, default: 0 ) –

    Specifies how many pixels to crop from the left side. Defaults to 0.

  • right

    (int, default: 0 ) –

    Specifies how many pixels to crop from the right side. Defaults to 0.

  • top

    (int, default: 0 ) –

    Specifies how many pixels to crop from the top side. Defaults to 0.

  • bottom

    (int, default: 0 ) –

    Specifies how many pixels to crop from the bottom side. Defaults to 0.

  • replace_in

    (SupportsString | None, default: None ) –

    Pixel value or expression to fill inside the region. Defaults to using the original pixel values.

  • replace_out

    (SupportsString | None, default: None ) –

    Pixel value or expression to fill outside the region. Defaults to the lowest possible values per plane.

  • planes

    (Planes, default: None ) –

    Which planes to process. Defaults to all.

  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

Returns:

  • VideoNode

    A new clip with the specified rectangular region masked in or out.

Source code in vsmasktools/utils.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
@RegionMask
def region_rel_mask(
    clip: vs.VideoNode,
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    replace_in: SupportsString | None = None,
    replace_out: SupportsString | None = None,
    planes: Planes = None,
    func: FuncExcept | None = None,
) -> vs.VideoNode:
    """
    Generates a mask that defines a rectangular region within the clip, replacing pixels inside or outside the region,
    using relative coordinates.

    Args:
        clip: Input clip.
        left: Specifies how many pixels to crop from the left side. Defaults to 0.
        right: Specifies how many pixels to crop from the right side. Defaults to 0.
        top: Specifies how many pixels to crop from the top side. Defaults to 0.
        bottom: Specifies how many pixels to crop from the bottom side. Defaults to 0.
        replace_in: Pixel value or expression to fill inside the region.
            Defaults to using the original pixel values.
        replace_out: Pixel value or expression to fill outside the region.
            Defaults to the lowest possible values per plane.
        planes: Which planes to process. Defaults to all.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A new clip with the specified rectangular region masked in or out.
    """
    func = func or region_rel_mask

    if replace_in is None:
        replace_in = "x"
    if replace_out is None:
        replace_out = get_lowest_values(clip, ColorRange.FULL)

    lefts, rights, tops, bottoms = list[int](), list[int](), list[int](), list[int]()

    for i in range(clip.format.num_planes):
        w, h = get_plane_sizes(clip, i)
        scale_w = clip.width / w
        scale_h = clip.height / h

        lefts.append(int(left / scale_w))
        rights.append(int(w - (right / scale_w) - 1))
        tops.append(int(top / scale_h))
        bottoms.append(int(h - (bottom / scale_h) - 1))

    return norm_expr(
        clip,
        region_rel_mask.expr,
        planes,
        func=func,
        left=lefts,
        right=rights,
        top=tops,
        bottom=bottoms,
        replace_in=replace_in,
        replace_out=replace_out,
    )

rekt_partial

rekt_partial(
    clip: VideoNode,
    func: Callable[Concatenate[VideoNode, ...], VideoNode],
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    *args: Any,
    **kwargs: Any
) -> VideoNode

Creates a rectangular mask to apply fixes only within the masked area, significantly speeding up filters like anti-aliasing and scaling.

Parameters:

  • clip

    (VideoNode) –

    The source video clip to which the mask will be applied.

  • func

    (Callable[Concatenate[VideoNode, ...], VideoNode]) –

    The function to be applied within the masked area.

  • left

    (int, default: 0 ) –

    The left boundary of the mask, defaults to 0.

  • right

    (int, default: 0 ) –

    The right boundary of the mask, defaults to 0.

  • top

    (int, default: 0 ) –

    The top boundary of the mask, defaults to 0.

  • bottom

    (int, default: 0 ) –

    The bottom boundary of the mask, defaults to 0.

Returns:

  • VideoNode

    A new clip with the applied mask.

Source code in vsmasktools/utils.py
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
@RektPartial
def rekt_partial(
    clip: vs.VideoNode,
    func: Callable[Concatenate[vs.VideoNode, ...], vs.VideoNode],
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    *args: Any,
    **kwargs: Any,
) -> vs.VideoNode:
    """
    Creates a rectangular mask to apply fixes only within the masked area,
    significantly speeding up filters like anti-aliasing and scaling.

    Args:
        clip: The source video clip to which the mask will be applied.
        func: The function to be applied within the masked area.
        left: The left boundary of the mask, defaults to 0.
        right: The right boundary of the mask, defaults to 0.
        top: The top boundary of the mask, defaults to 0.
        bottom: The bottom boundary of the mask, defaults to 0.

    Returns:
        A new clip with the applied mask.
    """

    def _filtered_func(clip: vs.VideoNode, *args: Any, **kwargs: Any) -> vs.VideoNode:
        return func(clip, *args, **kwargs)

    if left == top == right == bottom == 0:
        return _filtered_func(clip, *args, **kwargs)

    cropped = clip.std.Crop(left, right, top, bottom)

    filtered = _filtered_func(cropped, *args, **kwargs)

    check_ref_clip(cropped, filtered, rekt_partial._func)

    filtered = core.std.AddBorders(filtered, left, right, top, bottom)

    ratio_w, ratio_h = 2**clip.format.subsampling_w, 2**clip.format.subsampling_h

    vals = list(
        filter(
            None,
            [
                ("X {left} >= " if left else None),
                ("X {right} < " if right else None),
                ("Y {top} >= " if top else None),
                ("Y {bottom} < " if bottom else None),
            ],
        )
    )

    return norm_expr(
        [clip, filtered],
        [*vals, ["and"] * (len(vals) - 1), "y x ?"],
        left=[left, left / ratio_w],
        right=[clip.width - right, (clip.width - right) / ratio_w],
        top=[top, top / ratio_h],
        bottom=[clip.height - bottom, (clip.height - bottom) / ratio_h],
        func=rekt_partial._func,
    )

replace_squaremask

replace_squaremask(
    clipa: VideoNode,
    clipb: VideoNode,
    mask_params: tuple[int, int, int, int],
    ranges: FrameRangeN | FrameRangesN | None = None,
    blur_sigma: int | float | None = None,
    invert: bool = False,
    func: FuncExcept | None = None,
    show_mask: bool = False,
) -> VideoNode

Replace an area of the frame with another clip using a simple square mask.

This is a convenience wrapper merging square masking and framerange replacing functionalities into one function, along with additional utilities such as blurring.

Parameters:

  • clipa

    (VideoNode) –

    Base clip to process.

  • clipb

    (VideoNode) –

    Clip to mask on top of clipa.

  • mask_params

    (tuple[int, int, int, int]) –

    Parameters passed to squaremask. Expects a tuple of (width, height, offset_x, offset_y).

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    Frameranges to replace with the masked clip. If None, replaces the entire clip. Default: None.

  • blur_sigma

    (int | float | None, default: None ) –

    Post-blurring of the mask to help hide hard edges. If you pass an int, a box_blur will be used. Passing a float will use a gauss_blur instead. Default: None.

  • invert

    (bool, default: False ) –

    Invert the mask. This means everything but the defined square will be masked. Default: False.

  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers. Default: squaremask.

  • show_mask

    (bool, default: False ) –

    Return the mask instead of the masked clip.

Returns:

  • VideoNode

    Clip with a squaremask applied, and optionally set to specific frameranges.

Source code in vsmasktools/utils.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def replace_squaremask(
    clipa: vs.VideoNode,
    clipb: vs.VideoNode,
    mask_params: tuple[int, int, int, int],
    ranges: FrameRangeN | FrameRangesN | None = None,
    blur_sigma: int | float | None = None,
    invert: bool = False,
    func: FuncExcept | None = None,
    show_mask: bool = False,
) -> vs.VideoNode:
    """
    Replace an area of the frame with another clip using a simple square mask.

    This is a convenience wrapper merging square masking and framerange replacing functionalities
    into one function, along with additional utilities such as blurring.

    Args:
        clipa: Base clip to process.
        clipb: Clip to mask on top of `clipa`.
        mask_params: Parameters passed to `squaremask`. Expects a tuple of (width, height, offset_x, offset_y).
        ranges: Frameranges to replace with the masked clip. If `None`, replaces the entire clip. Default: None.
        blur_sigma: Post-blurring of the mask to help hide hard edges.
            If you pass an int, a [box_blur][vsrgtools.box_blur] will be used.
            Passing a float will use a [gauss_blur][vsrgtools.gauss_blur] instead.
            Default: None.
        invert: Invert the mask. This means everything *but* the defined square will be masked. Default: False.
        func: Function returned for custom error handling. This should only be set by VS package developers. Default:
            [squaremask][vsmasktools.squaremask].
        show_mask: Return the mask instead of the masked clip.

    Returns:
        Clip with a squaremask applied, and optionally set to specific frameranges.
    """
    func = func or replace_squaremask

    mask = squaremask(clipb[0], *mask_params, invert, func=func)

    if isinstance(blur_sigma, int):
        mask = box_blur(mask, blur_sigma)
    elif isinstance(blur_sigma, float):
        mask = gauss_blur(mask, blur_sigma)

    mask = core.std.Loop(mask, clipa.num_frames)

    if show_mask:
        return mask

    merge = clipa.std.MaskedMerge(clipb, mask)

    ranges = normalize_ranges(clipa, ranges)

    if len(ranges) == 1 and ranges[0] == (0, clipa.num_frames - 1):
        return merge

    return replace_ranges(clipa, merge, ranges)

squaremask

squaremask(
    clip: VideoNode,
    width: int,
    height: int,
    offset_x: int,
    offset_y: int,
    invert: bool = False,
    force_gray: bool = True,
    planes: Planes = None,
    func: FuncExcept | None = None,
) -> VideoNode

Create a square used for simple masking.

This is a fast and simple mask that's useful for very rough and simple masking.

Parameters:

  • clip

    (VideoNode) –

    The clip to process.

  • width

    (int) –

    The width of the square. This must be less than clip.width - offset_x.

  • height

    (int) –

    The height of the square. This must be less than clip.height - offset_y.

  • offset_x

    (int) –

    The location of the square, offset from the left side of the frame.

  • offset_y

    (int) –

    The location of the square, offset from the top of the frame.

  • invert

    (bool, default: False ) –

    Invert the mask. This means everything but the defined square will be masked. Default: False.

  • force_gray

    (bool, default: True ) –

    Whether to force using GRAY format or clip format.

  • planes

    (Planes, default: None ) –

    Which planes to process.

  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

Returns:

  • VideoNode

    A mask in the shape of a square.

Source code in vsmasktools/utils.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
def squaremask(
    clip: vs.VideoNode,
    width: int,
    height: int,
    offset_x: int,
    offset_y: int,
    invert: bool = False,
    force_gray: bool = True,
    planes: Planes = None,
    func: FuncExcept | None = None,
) -> vs.VideoNode:
    """
    Create a square used for simple masking.

    This is a fast and simple mask that's useful for very rough and simple masking.

    Args:
        clip: The clip to process.
        width: The width of the square. This must be less than clip.width - offset_x.
        height: The height of the square. This must be less than clip.height - offset_y.
        offset_x: The location of the square, offset from the left side of the frame.
        offset_y: The location of the square, offset from the top of the frame.
        invert: Invert the mask. This means everything *but* the defined square will be masked. Default: False.
        force_gray: Whether to force using GRAY format or clip format.
        planes: Which planes to process.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A mask in the shape of a square.
    """
    func = func or squaremask

    mask_format = (
        clip.format.replace(color_family=vs.GRAY, subsampling_w=0, subsampling_h=0) if force_gray else clip.format
    )

    if offset_x + width > clip.width or offset_y + height > clip.height:
        raise CustomValueError("mask exceeds clip size!", func)

    base_clip = vs.core.std.BlankClip(
        clip, None, None, mask_format.id, 1, color=get_lowest_values(mask_format, ColorRange.FULL), keep=True
    )

    replaces = ("mask_max", "x") if not invert else ("x", "mask_max")
    mask = region_abs_mask(base_clip, width, height, offset_x, offset_y, *replaces, planes, func)

    if clip.num_frames == 1:
        return mask

    return core.std.Loop(mask, clip.num_frames)