Using blending modesΒΆ

Fade in and out a JPEG over another
Code
Graph
from pynopegl_utils.misc import load_media

import pynopegl as ngl


@ngl.scene()
def fade(cfg: ngl.SceneCfg):
    image0 = load_media(cfg, "rooster")
    image1 = load_media(cfg, "panda")

    cfg.aspect_ratio = image0.width, image0.height
    cfg.duration = 4

    bg_tex = ngl.Texture2D(data_src=ngl.Media(image0.filename))
    bg = ngl.RenderTexture(bg_tex)

    fg_tex = ngl.Texture2D(data_src=ngl.Media(image1.filename))
    fg = ngl.RenderTexture(fg_tex)
    fg.set_blending("src_over")

    animkf = [
        ngl.AnimKeyFrameFloat(0, 0),
        ngl.AnimKeyFrameFloat(cfg.duration / 2, 1),
        ngl.AnimKeyFrameFloat(cfg.duration, 0),
    ]

    fg.add_filters(
        ngl.FilterOpacity(ngl.AnimatedFloat(animkf)),
    )

    return ngl.Group(children=[bg, fg])
graph
Overlay a transparent PNG over another image
Code
Graph
from pynopegl_utils.misc import load_media

import pynopegl as ngl


@ngl.scene()
def overlay(cfg: ngl.SceneCfg):
    image = load_media(cfg, "rooster")
    overlay = load_media(cfg, "fallen_leaf")

    cfg.aspect_ratio = image.width, image.height

    bg_tex = ngl.Texture2D(data_src=ngl.Media(image.filename))
    bg = ngl.RenderTexture(bg_tex)

    fg_tex = ngl.Texture2D(data_src=ngl.Media(overlay.filename))
    fg = ngl.RenderTexture(fg_tex)
    fg.set_blending("src_over")

    fg.add_filters(
        # A PNG is usually not premultiplied, but the blending operator expects
        # it to be.
        ngl.FilterPremult(),
        # Make the overlay half opaque
        ngl.FilterOpacity(0.5),
    )

    return ngl.Group(children=[bg, fg])
graph