Skip to content

core

Class info

🛈 DocStrings

The :func:inspect pipeline: load, detect duration, sample, check, compose.

SEAM_MIN_RATIO module-attribute

SEAM_MIN_RATIO = 0.002

A seam changing fewer than 0.2% of pixels always counts as seamless.

SEAM_STEP_FACTOR module-attribute

SEAM_STEP_FACTOR = 3

A seam is a jump if it changes this many times more pixels than one normal step.

inspect

inspect(
    source: str | Path,
    *,
    frames: int = 12,
    duration: float | None = None,
    size: tuple[int, int] | None = None,
    scale: float | None = None,
    at: float | None = None,
    out: str | Path | None = None,
    tile: int | None = None,
    columns: int | None = None,
    keep_frames: bool = False,
    heat_frames: int = 48,
    color_scheme: ColorScheme = "light",
    timeout: float = 30.0,
    every: float | None = None,
    times: Sequence[float] | Literal["marks"] | None = None,
    start: float | None = None,
    end: float | None = None,
    marks: str | Path | dict[str, Any] | Marks | None = None,
    static_stretch: float = 2.0,
    channel: str | None = None,
) -> InspectionResult

Render an animated SVG/HTML (or a video) at evenly spaced times and check it.

Parameters:

Name Type Description Default
source str | Path

Path to an .svg/.html file or a video (.mp4, .webm, .mov, .mkv, animated .gif/.webp), an http(s):// URL, - for stdin, or a markup string starting with <.

required
frames int

Number of contact-sheet tiles (2-100), spread over one cycle.

12
duration float | None

Cycle length in seconds; detected from SMIL/CSS/Web Animations if omitted.

None
size tuple[int, int] | None

Viewport (width, height) in CSS px (default: SVG size/viewBox, or 1280x720).

None
scale float | None

Device scale factor (default 1; tiny SVGs without size get more).

None
at float | None

Time in seconds of the full-resolution frame full.png.

None
out str | Path | None

Output directory (default: a new temporary directory).

None
tile int | None

Longest side of a contact-sheet tile in px (default: fit 1568 px width).

None
columns int | None

Contact-sheet columns (default: roughly 4:3 sheet).

None
keep_frames bool

Also write every sampled frame to frames/.

False
heat_frames int

Extra evenly spaced frames (pixels only) for a continuous heatmap trail; 0 uses only the contact-sheet frames.

48
color_scheme ColorScheme

light or dark (prefers-color-scheme).

'light'
timeout float

Seconds to wait for navigation and page scripts.

30.0
every float | None

Videos: one tile every this many seconds (instead of frames).

None
times Sequence[float] | Literal['marks'] | None

Videos: explicit tile times (seconds), or "marks" for each point's t and each region's/span's midpoint.

None
start float | None

Videos: start of the inspected range (default 0).

None
end float | None

Videos: end of the inspected range (default: the media duration).

None
marks str | Path | dict[str, Any] | Marks | None

Videos: marks to draw (path, JSON string, dict or :class:Marks).

None
static_stretch float

Videos: seconds without visible change reported as video-static-stretch.

2.0
channel str | None

Browser channel for Playwright (e.g. chrome for H.264 if the bundled Chromium lacks it).

None

Returns:

Type Description
InspectionResult

Paths of the written images plus metrics and findings.

Source code in src/framesheet/core.py
def inspect(
    source: str | Path,
    *,
    frames: int = 12,
    duration: float | None = None,
    size: tuple[int, int] | None = None,
    scale: float | None = None,
    at: float | None = None,
    out: str | Path | None = None,
    tile: int | None = None,
    columns: int | None = None,
    keep_frames: bool = False,
    heat_frames: int = 48,
    color_scheme: ColorScheme = "light",
    timeout: float = 30.0,
    every: float | None = None,
    times: Sequence[float] | Literal["marks"] | None = None,
    start: float | None = None,
    end: float | None = None,
    marks: str | Path | dict[str, Any] | Marks | None = None,
    static_stretch: float = 2.0,
    channel: str | None = None,
) -> InspectionResult:
    """Render an animated SVG/HTML (or a video) at evenly spaced times and check it.

    Args:
        source: Path to an ``.svg``/``.html`` file or a video (``.mp4``, ``.webm``,
            ``.mov``, ``.mkv``, animated ``.gif``/``.webp``), an ``http(s)://`` URL, ``-``
            for stdin, or a markup string starting with ``<``.
        frames: Number of contact-sheet tiles (2-100), spread over one cycle.
        duration: Cycle length in seconds; detected from SMIL/CSS/Web Animations if omitted.
        size: Viewport ``(width, height)`` in CSS px (default: SVG size/viewBox, or 1280x720).
        scale: Device scale factor (default 1; tiny SVGs without ``size`` get more).
        at: Time in seconds of the full-resolution frame ``full.png``.
        out: Output directory (default: a new temporary directory).
        tile: Longest side of a contact-sheet tile in px (default: fit 1568 px width).
        columns: Contact-sheet columns (default: roughly 4:3 sheet).
        keep_frames: Also write every sampled frame to ``frames/``.
        heat_frames: Extra evenly spaced frames (pixels only) for a continuous heatmap
            trail; 0 uses only the contact-sheet frames.
        color_scheme: ``light`` or ``dark`` (``prefers-color-scheme``).
        timeout: Seconds to wait for navigation and page scripts.
        every: Videos: one tile every this many seconds (instead of ``frames``).
        times: Videos: explicit tile times (seconds), or ``"marks"`` for each point's
            ``t`` and each region's/span's midpoint.
        start: Videos: start of the inspected range (default 0).
        end: Videos: end of the inspected range (default: the media duration).
        marks: Videos: marks to draw (path, JSON string, dict or :class:`Marks`).
        static_stretch: Videos: seconds without visible change reported as
            ``video-static-stretch``.
        channel: Browser channel for Playwright (e.g. ``chrome`` for H.264 if the bundled
            Chromium lacks it).

    Returns:
        Paths of the written images plus metrics and findings.
    """
    if not 2 <= frames <= 100:  # noqa: PLR2004
        msg = "frames must be between 2 and 100"
        raise ValueError(msg)
    if duration is not None and duration <= 0:
        msg = "duration must be positive"
        raise ValueError(msg)
    out_dir = Path(out) if out else Path(tempfile.mkdtemp(prefix="framesheet-"))
    out_dir.mkdir(parents=True, exist_ok=True)
    src = _resolve(source, out_dir)
    if src.kind == "video":
        from framesheet.marks import load_marks
        from framesheet.video import inspect_video

        if static_stretch <= 0:
            msg = "static_stretch must be positive"
            raise ValueError(msg)
        return inspect_video(
            src,
            out_dir=out_dir,
            frames=frames,
            duration=duration,
            size=size,
            scale=scale,
            at=at,
            tile=tile,
            columns=columns,
            keep_frames=keep_frames,
            heat_frames=heat_frames,
            timeout=timeout,
            every=every,
            times=times,
            start=start,
            end=end,
            marks=load_marks(marks) if marks is not None else None,
            static_stretch=static_stretch,
            channel=channel,
        )
    video_only = {"every": every, "times": times, "start": start, "end": end, "marks": marks}
    if given := [name for name, value in video_only.items() if value is not None]:
        msg = f"{', '.join(given)}: only for video inputs"
        raise ValueError(msg)
    at = 0.0 if at is None else at
    viewport = size or sources.default_size(src)
    dsf = scale or (1.0 if size else _auto_scale(viewport))

    with Session(
        src, size=viewport, scale=dsf, color_scheme=color_scheme, timeout=timeout, channel=channel
    ) as s:
        s.load()
        text = src.text if src.text is not None else s.response_text
        findings = _static_findings(src.kind, text)
        animations = timing.parse_timings(s.js("() => window.__framesheet.timings()"))
        info = timing.detect_duration(animations, duration)
        times = timing.sample_times(info.seconds, frames)
        eps = timing.epsilon(info.seconds)
        description = s.js("() => window.__framesheet.describe()")
        targets = s.js("() => window.__framesheet.animatedTargets()")
        canvas = as_rect(s.js("() => window.__framesheet.canvasRect()"))
        viewbox = s.js("() => window.__framesheet.viewBox()")
        is_svg_doc = bool(s.js("() => window.__framesheet.isSvgDoc"))
        snaps: dict[float, Snapshot] = {}
        heat: dict[float, Image.Image] = {}
        measured = {*times, round(eps, 4), at}
        extra = {round(info.seconds * i / heat_frames, 4) for i in range(heat_frames)}
        for t in sorted(measured | extra):
            if t in measured:
                snaps[t] = s.snapshot(t, overflow=src.kind == "html")
                heat[t] = snaps[t].image
            else:
                heat[t] = s.screenshot(t)
        console_errors = list(s.console_errors)

    sheet_snaps = [snaps[t] for t in times]
    images = [snap.image for snap in sheet_snaps]
    metrics = _frame_metrics(images, times, dsf)
    moving = any(m.changed_ratio > 0 for m in metrics)
    findings += _motion_findings(info, moving)
    loop = _loop_info(
        info, moving, snaps[times[0]].image, snaps[round(eps, 4)].image, images[-1], dsf
    )
    if loop.checked and loop.seamless is False:
        findings.append(
            Finding(
                severity="warning",
                code="non-seamless-loop",
                message=f"The loop jumps at the cycle end: {loop.detail}.",
                check="render",
                times=[times[-1], 0.0],
            )
        )
    geo = analysis.Geometry.build(
        description,
        sheet_snaps,
        canvas=canvas,
        viewbox=as_rect(viewbox) if viewbox else None,
        targets=targets,
        animations=animations,
        is_svg_doc=is_svg_doc,
    )
    findings += analysis.run_checks(geo)
    findings += [
        Finding(severity="warning", code="console-error", message=msg, check="render")
        for msg in console_errors[:MAX_CONSOLE_ERRORS]
    ]
    findings.sort(key=lambda f: _SEVERITY_ORDER[f.severity])

    heat_images = [heat[t] for t in sorted(heat) if t <= times[-1]]
    paths = _write_images(
        out_dir,
        src.label,
        info,
        times,
        sheet_snaps,
        heat_images,
        snaps[at].image,
        tile,
        columns,
        keep_frames,
    )
    if keep_frames:
        for m in metrics:
            m.path = out_dir / "frames" / f"frame_{m.index:03d}.png"
    result = InspectionResult(
        source=src.label,
        kind=src.kind,
        viewport=viewport,
        device_scale_factor=dsf,
        canvas=canvas,
        duration=info,
        times=times,
        full_at=at,
        images=paths,
        findings=findings,
        frames=metrics,
        elements=analysis.tracks(geo),
        loop=loop,
        console_errors=console_errors,
    )
    paths.report.write_text(result.model_dump_json(indent=2), encoding="utf-8")
    return result