lvsfunc.scale

(De)scaling and conversion functions and wrappers.

lvsfunc.scale.comparative_descale(clip[, ...])

Descale to SharpBicubic and an additional kernel, compare them, then pick one or the other.

lvsfunc.scale.comparative_restore(clip[, ...])

Companion function for comparative_descale to reupscale the clip for descale detail masking.

lvsfunc.scale.descale(clip[, upscaler, ...])

Unified descaling function.

lvsfunc.scale.descale_detail_mask([clip, ...])

Generate a detail mask given a clip and a clip rescaled with the same kernel.

lvsfunc.scale.gamma2linear(clip, curve[, ...])

Convert gamma to linear.

lvsfunc.scale.linear2gamma(clip, curve[, ...])

Convert linear to gamma.

lvsfunc.scale.mixed_rescale(clip[, width, ...])

Rewrite of InsaneAA to make it easier to use and maintain.

lvsfunc.scale.reupscale([clip, width, ...])

Quick 'n easy wrapper to re-upscale a clip descaled with descale using znedi3.

lvsfunc.scale.ssim_downsample(clip[, width, ...])

SSIM_downsample rewrite taken from a Vardë gist.

lvsfunc.scale.comparative_descale(clip, width=None, height=720, kernel=None, thr=5e-08)

Descale to SharpBicubic and an additional kernel, compare them, then pick one or the other.

The output clip has props that can be used to frameeval specific kernels by the user.

Parameters
  • clip (vs.VideoNode) – Clip to process.

  • width (int | None) – Width to descale to (if None, auto-calculated).

  • height (int) – Descale height.

  • kernel (Kernel | str | None) – py:class:vskernels.Kernel object to compare py:class:vskernels.BicubicSharp to. This can also be the string name of the kernel (Default: py:class:vskernels.Spline36 if kernel=None).

  • thr (float) – Threshold for which kernel to pick.

Return type

vs.VideoNode

Returns

Descaled clip.

Raises

CompareSameKernelError – py:class:vskernels.BicubicSharp gets passed to kernel.

lvsfunc.scale.comparative_restore(clip, width=None, height=720, kernel=None)

Companion function for comparative_descale to reupscale the clip for descale detail masking.

Parameters
  • clip (vs.VideoNode) – Clip to process.

  • width (int | None) – Width to upscale to (if None, auto-calculated).

  • height (int) – Upscale height.

  • kernel (Kernel | str | None) – py:class:vskernels.Kernel object to compare py:class:vskernels.BicubicSharp to. This can also be the string name of the kernel (Default: py:class:vskernels.Spline36 if kernel=None).

Return type

vs.VideoNode

Returns

Reupscaled clip.

Raises

CompareSameKernelError – py:class:vskernels.BicubicSharp gets passed to kernel.

lvsfunc.scale.descale(clip, upscaler=<function reupscale>, width=None, height=720, kernel=vskernels.Bicubic, threshold=0.0, mask=<function descale_detail_mask>, src_left=0.0, src_top=0.0, show_mask=False)

Unified descaling function.

Includes support for handling fractional resolutions (experimental), multiple resolutions, detail masking, and conditional scaling.

If you want to descale to a fractional resolution, set src_left and src_top and round up the target height.

If the source has multiple native resolutions, specify height as a list.

If you want to conditionally descale, specify a non-zero threshold.

Dependencies:

Parameters
  • clip (vs.VideoNode) – Clip to descale.

  • upscaler (Callable[[vs.VideoNode, int, int], vs.VideoNode] | None) –

    Callable function with signature upscaler(clip, width, height) -> vs.VideoNode to be used for reupscaling. Must be capable of handling variable res clips for multiple heights and conditional scaling.

    If a single height is given and upscaler is None, a constant resolution GRAY clip will be returned instead.

    Note that if upscaler is None, no upscaling will be performed and neither detail masking nor proper fractional descaling can be performed. (Default: lvsfunc.scale.reupscale())

  • width (int | List[int] | None) – Width(s) to descale to (if None, auto-calculated).

  • height (int | List[int]) – Height(s) to descale to. List indicates multiple resolutions, the function will determine the best. (Default: 720)

  • kernel (Kernel | str) – py:class:vskernels.Kernel object used for the descaling. This can also be the string name of the kernel (Default: py:class:vskernels.Catrom).

  • threshold (float) – Error threshold for conditional descaling (Default: 0.0, always descale).

  • mask (CreditMask | vs.VideoNode | None) – Function or mask clip used to mask detail. If None, no masking. Function must accept a clip and a reupscaled clip and return a mask. (Default: lvsfunc.scale.descale_detail_mask())

  • src_left (float) – Horizontal shifting for fractional resolutions (Default: 0.0).

  • src_top (float) – Vertical shifting for fractional resolutions (Default: 0.0).

  • show_mask (bool) – Return detail mask.

Return type

vs.VideoNode

Returns

Descaled and re-upscaled clip with float bitdepth.

Raises
  • ValueError – Asymmetric number of heights and widths are specified.

  • RuntimeError – Variable clip gets returned.

lvsfunc.scale.descale_detail_mask(clip: vs.VideoNode = '__no__default__', rescaled_clip: vs.VideoNode = '__no__default__', threshold: float = 0.05) vs.VideoNode

Generate a detail mask given a clip and a clip rescaled with the same kernel.

Function is curried to allow parameter tuning when passing to lvsfunc.scale.descale()

Parameters
  • clip – Original clip.

  • rescaled_clip – Clip downscaled and reupscaled using the same kernel.

  • threshold – Binarization threshold for mask (Default: 0.05).

Returns

Mask of lost detail.

lvsfunc.scale.gamma2linear(clip, curve, gcor=1.0, sigmoid=False, thr=0.5, cont=6.5, epsilon=1e-06)

Convert gamma to linear.

Return type

VideoNode

lvsfunc.scale.linear2gamma(clip, curve, gcor=1.0, sigmoid=False, thr=0.5, cont=6.5)

Convert linear to gamma.

Return type

VideoNode

lvsfunc.scale.mixed_rescale(clip, width=None, height=720, kernel=vskernels.Bicubic, downscaler=<function ssim_downsample>, credit_mask=<function descale_detail_mask>, mask_thr=0.05, mix_strength=0.25, show_mask=False, nnedi3_args={}, eedi3_args={})

Rewrite of InsaneAA to make it easier to use and maintain.

Descales and downscales the given clip and merges them together with a set strength.

This can be useful for dealing with a source that you can’t accurately descale, but you still want to force it. Not recommended to use it on everything, however.

A string can be passed instead of a Kernel object if you want to use that. This gives you access to every kernel object in vskernels. For more information on what every kernel does, please refer to their documentation.

This is still a work in progress at the time of writing. Please use with care.

Parameters
  • clip (vs.VideoNode) – Clip to process.

  • width (None | int) – Upscale width. If None, determine from height (Default: None).

  • height (int) – Upscale height (Default: 720).

  • kernel (Kernel | str) – py:class:vskernels.Kernel object used for the descaling. This can also be the string name of the kernel (Default: py:class:vskernels.Bicubic(b=0, c=1/2)).

  • downscaler (CustomScaler | Kernel | str) – Kernel or custom scaler used to downscale the clip. This can also be the string name of the kernel (Default: py:func:lvsfunc.scale.ssim_downsample).

  • credit_mask (CreditMask | vs.VideoNode | None) – Function or mask clip used to mask detail. If None, no masking. Function must accept a clip and a reupscaled clip and return a mask. (Default: lvsfunc.scale.descale_detail_mask()).

  • mask_thr (float) – Binarization threshold for lvsfunc.scale.descale_detail_mask() (Default: 0.05).

  • mix_strength (float) – Merging strength between the descaled and downscaled clip. Stronger values will make the line-art look closer to the downscaled clip. This can get pretty dangerous very quickly if you use a sharp downscaler!

  • show_mask (bool | int) – Return the credit_mask. If set to 2, it will return the line-art mask instead.

  • nnedi3_args (Dict[str, Any]) – Additional args to pass to nnedi3.

  • eedi3_args (Dict[str, Any]) – Additional args to pass to eedi3.

Return type

vs.VideoNode

Returns

Rescaled clip with a downscaled clip merged with it and credits masked.

Raises

ValueErrormask_thr is not between 0.0–1.0.

lvsfunc.scale.reupscale(clip: vs.VideoNode = '__no__default__', width: int | None = None, height: int = 1080, kernel: Kernel | str = vskernels.Bicubic, **kwargs: Any) vs.VideoNode

Quick ‘n easy wrapper to re-upscale a clip descaled with descale using znedi3.

Function is curried to allow parameter tuning when passing to lvsfunc.scale.descale()

Original function written by Vardë, modified by LightArrowsEXE.

Dependencies:

Parameters
  • clip – Clip to process.

  • width – Upscale width. If None, determine from height assuming 16:9 aspect ratio (Default: None).

  • height – Upscale height (Default: 1080).

  • kernel – py:class:vskernels.Kernel object used for downscaling the super-sampled clip. This can also be the string name of the kernel (Default: py:class:vskernels.Bicubic(b=0, c=1/2)).

  • kwargs – Arguments passed to znedi3 (Default: nsize=4, nns=4, qual=2, pscrn=2).

Returns

Reupscaled clip.

lvsfunc.scale.ssim_downsample(clip, width=None, height=720, smooth=0.816496580927726, kernel=vskernels.Bicubic, gamma=False, curve=None, sigmoid=False, epsilon=1e-06)

SSIM_downsample rewrite taken from a Vardë gist.

Unlike muvsfunc’s implementation, this function also works in float and does not use nnedi3_resample. Most of the documentation is taken from muvsfunc.

SSIM downsampler is an image downscaling technique that aims to optimize for the perceptual quality of the downscaled results. Image downscaling is considered as an optimization problem where the difference between the input and output images is measured using famous Structural SIMilarity (SSIM) index. The solution is derived in closed-form, which leads to the simple, efficient implementation. The downscaled images retain perceptually important features and details, resulting in an accurate and spatio-temporally consistent representation of the high resolution input.

Original gist

Parameters
  • clip (vs.VideoNode) – Clip to process.

  • width (int | None) – Output width. If None, autocalculates using height.

  • height (int) – Output height (Default: 720).

  • smooth (float | VSFunction) – Image smoothening method. If you pass an int, it specifies the “radius” of the internally-used boxfilter, i.e. the window has a size of (2*smooth+1)x(2*smooth+1). If you pass a float, it specifies the “sigma” of core.tcanny.TCanny, i.e. the standard deviation of gaussian blur. If you pass a function, it acts as a general smoother. Default uses a gaussian blur.

  • kernel (Kernel | str) – py:class:vskernels.Kernel object used for certain scaling operations. This can also be the string name of the kernel (Default: py:class:vskernels.Bicubic(0, 0.5)).

  • curve (CURVES | None) – Gamma mapping. Will auto-determine based on the input props or resolution. Can be forced with for example curve=vs.TransferCharacteristics.TRANSFER_BT709.

  • gamma (bool) – Perform a gamma conversion prior to scaling and after scaling. This function MUST be set to True for sigmoid and epsilon to function.

  • sigmoid (bool) – When True, applies a sigmoidal curve after the power-like curve (or before when converting from linear to gamma-corrected). This helps reduce the dark halo artefacts found around sharp edges caused by resizing in linear luminance. This parameter only works if gamma=True.

  • epsilon (float) – Machine epsilon. This parameter only works if gamma=True.

Return type

vs.VideoNode

Returns

Downsampled clip.