Skip to content

Instantly share code, notes, and snippets.

@4re
Last active January 1, 2023 19:32
Show Gist options
  • Save 4re/2545a281e3f17ba6ef82 to your computer and use it in GitHub Desktop.
Save 4re/2545a281e3f17ba6ef82 to your computer and use it in GitHub Desktop.

Revisions

  1. 4re revised this gist Nov 3, 2020. 1 changed file with 18 additions and 17 deletions.
    35 changes: 18 additions & 17 deletions psharpen.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import vapoursynth as vs
    from vapoursynth import core, GRAY

    __version__ = '1.1.0'
    __version__ = "1.1.1"


    def _clamp(minimum, value, maximum):
    @@ -11,8 +11,7 @@ def _m4(value, mult=4.0):
    return 16 if value < 16 else int(round(value / mult) * mult)


    def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    dest_x=None, dest_y=None):
    def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None, dest_y=None):
    """From http://forum.doom9.org/showpost.php?p=683344&postcount=28
    Sharpening function similar to LimitedSharpenFaster.
    @@ -26,7 +25,6 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    dest_x (int): Output resolution after sharpening.
    dest_y (int): Output resolution after sharpening.
    """
    core = vs.get_core()

    src = clip

    @@ -44,36 +42,39 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    ss_y = 1.0

    if ss_x != 1.0 or ss_y != 1.0:
    clip = core.resize.Lanczos(clip, width=_m4(src.width*ss_x), height=_m4(src.height*ss_y))
    clip = core.resize.Lanczos(clip, width=_m4(src.width * ss_x), height=_m4(src.height * ss_y))

    resz = clip

    if src.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=clip, planes=[0], colorfamily=vs.GRAY)
    clip = core.std.ShufflePlanes(clips=clip, planes=[0], colorfamily=GRAY)

    max_ = core.std.Maximum(clip)
    min_ = core.std.Minimum(clip)

    nmax = core.std.Expr([max_, min_], ['x y -'])
    nval = core.std.Expr([clip, min_], ['x y -'])
    nmax = core.std.Expr([max_, min_], ["x y -"])
    nval = core.std.Expr([clip, min_], ["x y -"])

    expr0 = threshold * (1.0 - strength) / (1.0 - (1.0 - threshold) * (1.0 - strength))
    epsilon = 0.000000000000001
    scl = (1 << clip.format.bits_per_sample) // 256
    x = f'x {scl} /' if scl != 1 else 'x'
    y = f'y {scl} /' if scl != 1 else 'y'
    x = f"x {scl} /" if scl != 1 else "x"
    y = f"y {scl} /" if scl != 1 else "y"

    expr = (f'{x} {y} {epsilon} + / 2 * 1 - abs {expr0} < {strength} 1 = {x} {y} 2 / = 0 {y} 2 / ? '
    f'{x} {y} {epsilon} + / 2 * 1 - abs 1 {strength} - / ? {x} {y} {epsilon} + / 2 * 1 - abs 1 {threshold} - '
    f'* {threshold} + ? {x} {y} 2 / > 1 -1 ? * 1 + {y} * 2 / {scl} *')
    expr = (
    f"{x} {y} {epsilon} + / 2 * 1 - abs {expr0} < {strength} 1 = {x} {y} 2 / = 0 {y} 2 / ? "
    f"{x} {y} {epsilon} + / 2 * 1 - abs 1 {strength} - / ? {x} {y} {epsilon} + / 2 * 1 - abs 1 {threshold} - "
    f"* {threshold} + ? {x} {y} 2 / > 1 -1 ? * 1 + {y} * 2 / {scl} *"
    )

    nval = core.std.Expr([nval, nmax], [expr])

    clip = core.std.Expr([nval, min_], ['x y +'])
    clip = core.std.Expr([nval, min_], ["x y +"])

    if src.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=[clip, resz], planes=[0, 1, 2],
    colorfamily=src.format.color_family)
    clip = core.std.ShufflePlanes(
    clips=[clip, resz], planes=[0, 1, 2], colorfamily=src.format.color_family
    )

    if ss_x != 1.0 or ss_y != 1.0 or dest_x != src.width or dest_y != src.height:
    clip = core.resize.Lanczos(clip, width=dest_x, height=dest_y)
  2. 4re revised this gist Apr 1, 2020. 1 changed file with 31 additions and 36 deletions.
    67 changes: 31 additions & 36 deletions psharpen.py
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,24 @@
    import vapoursynth as vs

    __version__ = '1.1.0'

    def _clamp(minimum, x, maximum):
    return int(max(minimum, min(round(x), maximum)))

    def _clamp(minimum, value, maximum):
    return int(max(minimum, min(round(value), maximum)))

    def _m4(x, m=4.0):
    return 16 if x < 16 else int(round(x / m) * m)

    def _m4(value, mult=4.0):
    return 16 if value < 16 else int(round(value / mult) * mult)


    def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    dest_x=None, dest_y=None):
    """From http://forum.doom9.org/showpost.php?p=683344&postcount=28
    Sharpeing function similar to LimitedSharpenFaster.
    Sharpening function similar to LimitedSharpenFaster.
    Args:
    clip (clip): Input clip.
    strength (int): Strength of the sharpening.
    threshold (int): Controls "how much" to be sharpened.
    ss_x (float): Supersampling factor (reduce aliasing on edges).
    @@ -25,62 +28,54 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    """
    core = vs.get_core()

    ox = clip.width
    oy = clip.height

    bd = clip.format.bits_per_sample
    max_ = 2 ** bd - 1
    scl = (max_ + 1) // 256
    x = 'x {} /'.format(scl) if bd != 8 else 'x'
    y = 'y {} /'.format(scl) if bd != 8 else 'y'
    src = clip

    if dest_x is None:
    dest_x = ox
    dest_x = src.width
    if dest_y is None:
    dest_y = oy
    dest_y = src.height

    strength = _clamp(0, strength, 100)
    threshold = _clamp(0, threshold, 100)
    strength = _clamp(0, strength, 100) / 100.0
    threshold = _clamp(0, threshold, 100) / 100.0

    if ss_x < 1.0:
    ss_x = 1.0
    if ss_y < 1.0:
    ss_y = 1.0

    if ss_x != 1.0 or ss_y != 1.0:
    clip = core.resize.Lanczos(clip, width=_m4(ox*ss_x), height=_m4(oy*ss_y))
    clip = core.resize.Lanczos(clip, width=_m4(src.width*ss_x), height=_m4(src.height*ss_y))

    orig = clip
    resz = clip

    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=clip, planes=[0],
    colorfamily=vs.GRAY)
    val = clip
    if src.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=clip, planes=[0], colorfamily=vs.GRAY)

    max_ = core.std.Maximum(clip)
    min_ = core.std.Minimum(clip)

    nmax = core.std.Expr([max_, min_], ['x y -'])
    nval = core.std.Expr([val, min_], ['x y -'])
    nval = core.std.Expr([clip, min_], ['x y -'])

    s = strength/100.0
    t = threshold/100.0
    x0 = t * (1.0 - s) / (1.0 - (1.0 - t) * (1.0 - s))
    expr0 = threshold * (1.0 - strength) / (1.0 - (1.0 - threshold) * (1.0 - strength))
    epsilon = 0.000000000000001
    scl = (1 << clip.format.bits_per_sample) // 256
    x = f'x {scl} /' if scl != 1 else 'x'
    y = f'y {scl} /' if scl != 1 else 'y'

    expr = ('{x} {y} / 2 * 1 - abs {x0} < {s} 1 = {x} {y} 2 / = 0 {y} 2 / ? '
    '{x} {y} / 2 * 1 - abs 1 {s} - / ? {x} {y} / 2 * 1 - abs 1 {t} - '
    '* {t} + ? {x} {y} 2 / > 1 -1 ? * 1 + {y} * 2 / {scl} *').format(
    x=x, y=y, x0=x0, t=t, s=s, scl=scl)
    expr = (f'{x} {y} {epsilon} + / 2 * 1 - abs {expr0} < {strength} 1 = {x} {y} 2 / = 0 {y} 2 / ? '
    f'{x} {y} {epsilon} + / 2 * 1 - abs 1 {strength} - / ? {x} {y} {epsilon} + / 2 * 1 - abs 1 {threshold} - '
    f'* {threshold} + ? {x} {y} 2 / > 1 -1 ? * 1 + {y} * 2 / {scl} *')

    nval = core.std.Expr([nval, nmax], [expr])

    val = core.std.Expr([nval, min_], ['x y +'])
    clip = core.std.Expr([nval, min_], ['x y +'])

    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=[val, orig], planes=[0, 1, 2],
    colorfamily=orig.format.color_family)
    if src.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=[clip, resz], planes=[0, 1, 2],
    colorfamily=src.format.color_family)

    if ss_x != 1.0 or ss_y != 1.0 or dest_x != ox or dest_y != oy:
    if ss_x != 1.0 or ss_y != 1.0 or dest_x != src.width or dest_y != src.height:
    clip = core.resize.Lanczos(clip, width=dest_x, height=dest_y)

    return clip
  3. 4re revised this gist Oct 13, 2016. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions psharpen.py
    Original file line number Diff line number Diff line change
    @@ -48,10 +48,7 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    ss_y = 1.0

    if ss_x != 1.0 or ss_y != 1.0:
    clip = core.fmtc.resample(clip, _m4(ox*ss_x), _m4(oy*ss_y),
    kernel="lanczos")
    if bd != clip.format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd, dmode=1)
    clip = core.resize.Lanczos(clip, width=_m4(ox*ss_x), height=_m4(oy*ss_y))

    orig = clip

    @@ -84,8 +81,6 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    colorfamily=orig.format.color_family)

    if ss_x != 1.0 or ss_y != 1.0 or dest_x != ox or dest_y != oy:
    clip = core.fmtc.resample(clip, dest_x, dest_y, kernel="lanczos")
    if bd != clip.format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd)
    clip = core.resize.Lanczos(clip, width=dest_x, height=dest_y)

    return clip
  4. 4re revised this gist Sep 23, 2015. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions psharpen.py
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ def _m4(x, m=4.0):
    return 16 if x < 16 else int(round(x / m) * m)


    def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,
    dest_y=None):
    def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0,
    dest_x=None, dest_y=None):
    """From http://forum.doom9.org/showpost.php?p=683344&postcount=28
    Sharpeing function similar to LimitedSharpenFaster.
    @@ -41,6 +41,7 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,

    strength = _clamp(0, strength, 100)
    threshold = _clamp(0, threshold, 100)

    if ss_x < 1.0:
    ss_x = 1.0
    if ss_y < 1.0:
    @@ -49,8 +50,8 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,
    if ss_x != 1.0 or ss_y != 1.0:
    clip = core.fmtc.resample(clip, _m4(ox*ss_x), _m4(oy*ss_y),
    kernel="lanczos")
    if bd != clip._format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd)
    if bd != clip.format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd, dmode=1)

    orig = clip

    @@ -80,11 +81,11 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,

    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=[val, orig], planes=[0, 1, 2],
    colorfamily=orig._format.color_family)
    colorfamily=orig.format.color_family)

    if ss_x != 1.0 or ss_y != 1.0 or dest_x != ox or dest_y != oy:
    clip = core.fmtc.resample(clip, dest_x, dest_y, kernel="lanczos")
    if bd != clip._format.bits_per_sample:
    if bd != clip.format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd)

    return clip
  5. 4re revised this gist Sep 23, 2015. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions psharpen.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,11 @@
    import vapoursynth as vs
    import havsfunc as haf


    def clamp(minimum, x, maximum):
    def _clamp(minimum, x, maximum):
    return int(max(minimum, min(round(x), maximum)))


    def m4(x, m=4.0):
    def _m4(x, m=4.0):
    return 16 if x < 16 else int(round(x / m) * m)


    @@ -21,8 +20,8 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,
    threshold (int): Controls "how much" to be sharpened.
    ss_x (float): Supersampling factor (reduce aliasing on edges).
    ss_y (float): Supersampling factor (reduce aliasing on edges).
    dest_x (int): Output resolution after sharpening (avoid a resizing step).
    dest_y (int): Output resolution after sharpening (avoid a resizing step).
    dest_x (int): Output resolution after sharpening.
    dest_y (int): Output resolution after sharpening.
    """
    core = vs.get_core()

    @@ -40,19 +39,24 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,
    if dest_y is None:
    dest_y = oy

    strength = clamp(0, strength, 100)
    threshold = clamp(0, threshold, 100)
    strength = _clamp(0, strength, 100)
    threshold = _clamp(0, threshold, 100)
    if ss_x < 1.0:
    ss_x = 1.0
    if ss_y < 1.0:
    ss_y = 1.0

    if ss_x != 1.0 or ss_y != 1.0:
    clip = haf.Resize(clip, m4(ox*ss_x), m4(oy*ss_y), kernel="Lanczos")
    clip = core.fmtc.resample(clip, _m4(ox*ss_x), _m4(oy*ss_y),
    kernel="lanczos")
    if bd != clip._format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd)

    orig = clip

    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=clip, planes=[0], colorfamily=vs.GRAY)
    clip = core.std.ShufflePlanes(clips=clip, planes=[0],
    colorfamily=vs.GRAY)
    val = clip

    max_ = core.std.Maximum(clip)
    @@ -76,9 +80,11 @@ def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,

    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=[val, orig], planes=[0, 1, 2],
    colorfamily=orig.format.color_family)
    colorfamily=orig._format.color_family)

    if ss_x != 1.0 or ss_y != 1.0 or dest_x != ox or dest_y != oy:
    clip = haf.Resize(clip, dest_x, dest_y, kernel="Lanczos")
    clip = core.fmtc.resample(clip, dest_x, dest_y, kernel="lanczos")
    if bd != clip._format.bits_per_sample:
    clip = core.fmtc.bitdepth(clip, bits=bd)

    return clip
  6. 4re created this gist Sep 23, 2015.
    84 changes: 84 additions & 0 deletions psharpen.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    import vapoursynth as vs
    import havsfunc as haf


    def clamp(minimum, x, maximum):
    return int(max(minimum, min(round(x), maximum)))


    def m4(x, m=4.0):
    return 16 if x < 16 else int(round(x / m) * m)


    def psharpen(clip, strength=25, threshold=75, ss_x=1.0, ss_y=1.0, dest_x=None,
    dest_y=None):
    """From http://forum.doom9.org/showpost.php?p=683344&postcount=28
    Sharpeing function similar to LimitedSharpenFaster.
    Args:
    strength (int): Strength of the sharpening.
    threshold (int): Controls "how much" to be sharpened.
    ss_x (float): Supersampling factor (reduce aliasing on edges).
    ss_y (float): Supersampling factor (reduce aliasing on edges).
    dest_x (int): Output resolution after sharpening (avoid a resizing step).
    dest_y (int): Output resolution after sharpening (avoid a resizing step).
    """
    core = vs.get_core()

    ox = clip.width
    oy = clip.height

    bd = clip.format.bits_per_sample
    max_ = 2 ** bd - 1
    scl = (max_ + 1) // 256
    x = 'x {} /'.format(scl) if bd != 8 else 'x'
    y = 'y {} /'.format(scl) if bd != 8 else 'y'

    if dest_x is None:
    dest_x = ox
    if dest_y is None:
    dest_y = oy

    strength = clamp(0, strength, 100)
    threshold = clamp(0, threshold, 100)
    if ss_x < 1.0:
    ss_x = 1.0
    if ss_y < 1.0:
    ss_y = 1.0

    if ss_x != 1.0 or ss_y != 1.0:
    clip = haf.Resize(clip, m4(ox*ss_x), m4(oy*ss_y), kernel="Lanczos")

    orig = clip
    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=clip, planes=[0], colorfamily=vs.GRAY)
    val = clip

    max_ = core.std.Maximum(clip)
    min_ = core.std.Minimum(clip)

    nmax = core.std.Expr([max_, min_], ['x y -'])
    nval = core.std.Expr([val, min_], ['x y -'])

    s = strength/100.0
    t = threshold/100.0
    x0 = t * (1.0 - s) / (1.0 - (1.0 - t) * (1.0 - s))

    expr = ('{x} {y} / 2 * 1 - abs {x0} < {s} 1 = {x} {y} 2 / = 0 {y} 2 / ? '
    '{x} {y} / 2 * 1 - abs 1 {s} - / ? {x} {y} / 2 * 1 - abs 1 {t} - '
    '* {t} + ? {x} {y} 2 / > 1 -1 ? * 1 + {y} * 2 / {scl} *').format(
    x=x, y=y, x0=x0, t=t, s=s, scl=scl)

    nval = core.std.Expr([nval, nmax], [expr])

    val = core.std.Expr([nval, min_], ['x y +'])

    if orig.format.num_planes != 1:
    clip = core.std.ShufflePlanes(clips=[val, orig], planes=[0, 1, 2],
    colorfamily=orig.format.color_family)

    if ss_x != 1.0 or ss_y != 1.0 or dest_x != ox or dest_y != oy:
    clip = haf.Resize(clip, dest_x, dest_y, kernel="Lanczos")

    return clip