Skip to content

OpenSenseNova/SenseNova-Skills / skills/sn-image-base

sn-image-base

Verified

Base-layer skill for the SenseNova-Skills project, providing low-level APIs for image generation, recognition (VLM), and text optimization (LLM). This skill does not preprocess inputs; it only calls backend services and returns results. This skill is not user-facing and is intended for upper-layer skills only.

FileKindSize
.gitattributesother219 B
.gitignoreother4.3 KB
README.mdmarkdown10.7 KB
README_CN.mdmarkdown10.4 KB
SKILL.mdskill md13.0 KB
references/api_spec.mdmarkdown14.0 KB
requirements.txtmanifest50 B
scripts/.python-versionother4 B
scripts/extract_json.pyscript3.6 KB
scripts/pyproject.tomlmanifest339 B
scripts/ruff.tomlconfig730 B
scripts/sn_agent_runner.pyscript25.1 KB
scripts/sn_image_base/__init__.pyscript24 B
scripts/sn_image_base/configs.pyscript12.5 KB
scripts/sn_image_base/exceptions.pyscript1.2 KB
scripts/sn_image_base/generation/__init__.pyscript358 B
scripts/sn_image_base/generation/atlas_cloud.pyscript9.6 KB
scripts/sn_image_base/generation/core/__init__.pyscript412 B
scripts/sn_image_base/generation/core/client_base.pyscript2.5 KB
scripts/sn_image_base/generation/nano_banana.pyscript11.6 KB
scripts/sn_image_base/generation/openai_image.pyscript12.3 KB
scripts/sn_image_base/generation/sensenova.pyscript19.5 KB
scripts/sn_image_base/llm/__init__.pyscript215 B
scripts/sn_image_base/llm/anthropic_adapter.pyscript5.0 KB
scripts/sn_image_base/llm/chat_completions_adapter.pyscript9.7 KB
scripts/sn_image_base/llm/llm_adapter.pyscript1.8 KB
scripts/sn_image_base/utils/__init__.pyscript0 B
scripts/sn_image_base/utils/error_utils.pyscript7.3 KB
scripts/sn_image_base/utils/httpx_client.pyscript7.0 KB
scripts/sn_image_base/vlm/__init__.pyscript100 B
scripts/sn_image_base/vlm/utils.pyscript3.0 KB
scripts/sn_image_base/vlm/vlm_adapter.pyscript2.1 KB
scripts/tests/test_atlas_cloud.pyscript6.9 KB
tests/conftest.pyscript375 B
tests/test_configs_env_fallthrough.pyscript2.8 KB

scripts/sn_image_base/vlm/utils.py

View on GitHub
"""Image encoding / decoding utilities for VLM."""

from __future__ import annotations

import base64
import io
from pathlib import Path

from PIL import Image


def read_image_bytes(image: str | bytes) -> bytes:
    """Read raw image bytes from a path or return bytes unchanged.

    Args:
        image: File path to an image, or raw image bytes.

    Returns:
        bytes: Raw image bytes.

    Raises:
        FileNotFoundError: If image is a path and the file does not exist.
    """
    if isinstance(image, bytes):
        return image
    path = Path(image)
    if not path.is_file():
        raise FileNotFoundError(f"Image file not found: {image}")
    return path.read_bytes()


def detect_mime(data: bytes) -> str:
    """Infer MIME type from image magic bytes.

    Args:
        data: Raw image bytes (at least 8 bytes for PNG check).

    Returns:
        str: 'image/png', 'image/jpeg', or 'image/png' as fallback.
    """
    if data[:8] == b"\x89PNG\r\n\x1a\n":
        return "image/png"
    if data[:3] == b"\xff\xd8\xff":
        return "image/jpeg"
    return "image/png"


def detect_suffix(data: bytes) -> str:
    """Infer file suffix from image magic bytes.

    Args:
        data: Raw image bytes.

    Returns:
        str: '.png', '.jpg', or '.bin' as fallback.
    """
    if data[:8] == b"\x89PNG\r\n\x1a\n":
        return ".png"
    if data[:3] == b"\xff\xd8\xff":
        return ".jpg"
    return ".bin"


def image_to_mime_and_bytes(image: str | bytes) -> tuple[str, bytes]:
    """Get MIME type and raw bytes; convert to PNG if format is not PNG/JPEG.

    Args:
        image: File path or raw image bytes.

    Returns:
        tuple[str, bytes]: (mime_type, raw_bytes). Unknown formats become PNG.
    """
    raw = read_image_bytes(image)
    mime = detect_mime(raw)
    if mime in ("image/png", "image/jpeg"):
        return mime, raw
    img = Image.open(io.BytesIO(raw)).convert("RGBA")
    buf = io.BytesIO()
    img.save(buf, format="PNG")
    return "image/png", buf.getvalue()


def image_to_base64(image: str | bytes) -> tuple[str, str]:
    """Encode image to MIME type and base64 string.

    Args:
        image: File path or raw image bytes.

    Returns:
        tuple[str, str]: (mime_type, base64_encoded_string).
    """
    mime, raw = image_to_mime_and_bytes(image)
    return mime, base64.b64encode(raw).decode("utf-8")


def image_to_data_url(image: str | bytes) -> str:
    """Build a data URL (data:mime;base64,...) for the image.

    Args:
        image: File path or raw image bytes.

    Returns:
        str: Data URL string.
    """
    mime, b64 = image_to_base64(image)
    return f"data:{mime};base64,{b64}"


def mask_secret(secret: str) -> str:
    """Mask a secret for logging (e.g. show first 6 and last 4 chars).

    Args:
        secret: Raw secret string.

    Returns:
        str: Masked string (e.g. 'abcdef...ghij' or all '*' if length <= 8).
    """
    if len(secret) <= 8:
        return "*" * len(secret)
    return f"{secret[:6]}...{secret[-4:]}"
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall