Skip to content

Yuan1z0825/nature-skills / skills/nature-image2ppt

nature-image2ppt

Flagged

Reconstruct slide images, screenshots, scanned PDFs, or image-only PPTX files as object-level editable PowerPoint. Use for 图片转可编辑PPT、截图还原PPT and diagram reconstruction; not authoring a new deck from research notes.

FileKindSize
.gitignoreother573 B
LICENSEother1.1 KB
README.mdmarkdown4.0 KB
README_EN.mdmarkdown4.3 KB
SKILL.mdskill md9.8 KB
agents/openai.yamlconfig272 B
cli/image2ppt/__init__.pyscript22 B
cli/image2ppt/cli.pyscript698 B
cli/image2ppt/runtime/__init__.pyscript69 B
cli/image2ppt/runtime/_input_normalization.pyscript15.9 KB
cli/image2ppt/runtime/_page_artifacts.pyscript4.5 KB
cli/image2ppt/runtime/build_pptx_from_manifest.pyscript54.0 KB
cli/image2ppt/runtime/configure_image_backend.pyscript5.8 KB
cli/image2ppt/runtime/deck_run_state.pyscript5.6 KB
cli/image2ppt/runtime/deck_text_hints.pyscript6.8 KB
cli/image2ppt/runtime/finalize_deck_run.pyscript3.1 KB
cli/image2ppt/runtime/formula_renderer.pyscript9.6 KB
cli/image2ppt/runtime/image_gen.pyscript37.1 KB
cli/image2ppt/runtime/main.pyscript39.7 KB
cli/image2ppt/runtime/make_page_contact_sheet.pyscript1.1 KB
cli/image2ppt/runtime/paddle_text_hints.pyscript10.7 KB
cli/image2ppt/runtime/page_job_status.pyscript2.1 KB
cli/image2ppt/runtime/page_text_metrics.pyscript11.6 KB
cli/image2ppt/runtime/prepare_deck_run.pyscript7.3 KB
cli/image2ppt/runtime/process_asset_sheet.pyscript6.0 KB
cli/image2ppt/runtime/record_imagegen_result.pyscript5.9 KB
cli/image2ppt/runtime/record_page_dispatch.pyscript2.8 KB
cli/image2ppt/runtime/record_page_result.pyscript4.6 KB
cli/image2ppt/runtime/remove_chroma_key.pyscript13.5 KB
cli/image2ppt/runtime/reset_page_job.pyscript2.6 KB
cli/image2ppt/runtime/runtime_env.pyscript18.8 KB
cli/image2ppt/runtime/split_alpha_components.pyscript9.2 KB
cli/image2ppt/runtime/text_hints.pyscript14.0 KB
cli/image2ppt/runtime/validate_pptx.pyscript38.7 KB
cli/pyproject.tomlmanifest570 B
config.example.yamlconfig1.0 KB
manifest.yamlconfig480 B
prompts/page-worker-base.mdmarkdown7.4 KB
prompts/page-worker.mdmarkdown5.1 KB
references/assets-provenance-contract.mdmarkdown2.9 KB
references/cli-helper.mdmarkdown17.8 KB
references/manifest-arrow-extension.mdmarkdown2.5 KB
references/manifest-schema.mdmarkdown19.9 KB
references/object-routing.mdmarkdown3.4 KB
references/ocr-text-hints-contract.mdmarkdown3.0 KB
references/page-decision-tree.mdmarkdown21.7 KB
references/qa-contract.mdmarkdown5.4 KB
references/region-decomposition.mdmarkdown6.5 KB
references/runtime-dependencies.mdmarkdown2.5 KB
references/workflow.mdmarkdown6.7 KB
requirements.txtmanifest176 B
schemas/page-manifest-v2.schema.jsonconfig2.9 KB
scripts/build_page_worker_prompt.pyscript4.1 KB
scripts/inspect_arrow_atomicity.pyscript7.3 KB
scripts/inspect_region_decomposition.pyscript18.1 KB
scripts/postprocess_manifest_arrows.pyscript18.4 KB
scripts/render_image2ppt_qa.pyscript11.1 KB
scripts/run_final_image2ppt_qa.pyscript9.0 KB
scripts/run_image2ppt_qa.pyscript9.5 KB
scripts/runtime_paths.pyscript4.0 KB
scripts/visual_review_evidence.pyscript5.4 KB
tests/support.pyscript6.4 KB
tests/test_console_portability.pyscript1021 B
tests/test_deck_slide_size.pyscript1.1 KB
tests/test_dispatch_concurrency.pyscript2.9 KB
tests/test_formula_renderer.pyscript4.3 KB
tests/test_independence.pyscript2.2 KB
tests/test_input_normalization_alpha.pyscript2.3 KB
tests/test_input_normalization_opc_targets.pyscript1.9 KB
tests/test_isolated_lifecycle.pyscript9.5 KB
tests/test_manifest_arrows.pyscript8.6 KB
tests/test_migration_parity.pyscript3.2 KB
tests/test_multi_agent_backend.pyscript80.8 KB
tests/test_ocr_http.pyscript16.7 KB
tests/test_paddle_result_url_safety.pyscript3.2 KB
tests/test_page_hints.pyscript10.4 KB
tests/test_path_confinement.pyscript7.5 KB
tests/test_pdf_backend.pyscript4.3 KB
tests/test_project_metadata.pyscript4.7 KB
tests/test_quality_contracts.pyscript9.1 KB
tests/test_region_decomposition.pyscript6.2 KB
tests/test_render_guard.pyscript2.4 KB
tests/test_runtime_config.pyscript2.6 KB
tests/test_runtime_paths.pyscript2.2 KB
tests/test_script_inventory.pyscript2.1 KB
tests/test_slide_layout.pyscript9.3 KB
tests/test_state_e2e.pyscript7.8 KB
tests/test_structure_reconstruction.pyscript12.5 KB
tests/test_thin_profile.pyscript4.0 KB
tests/test_visual_review_evidence.pyscript2.3 KB
tests/test_visual_review_runtime.pyscript2.9 KB

cli/image2ppt/runtime/_input_normalization.py

View on GitHub
import hashlib
import io
import json
import posixpath
import shutil
import subprocess  # nosec
import tempfile
import zipfile
from datetime import datetime
from pathlib import Path
from defusedxml import ElementTree as ET

from PIL import Image


IMG_EXTS = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tif", ".tiff"}
PPT_EXTS = {".ppt", ".pptx"}
REL_NS = "http://schemas.openxmlformats.org/package/2006/relationships"
NS = {
    "a": "http://schemas.openxmlformats.org/drawingml/2006/main",
    "p": "http://schemas.openxmlformats.org/presentationml/2006/main",
    "r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
    "rel": REL_NS,
}


def sha256_text(value):
    return hashlib.sha256(value.encode("utf-8")).hexdigest()


def collect_paragraph_text(root):
    paragraphs = []
    for paragraph in root.findall(".//a:p", NS):
        text = "".join(node.text or "" for node in paragraph.findall(".//a:t", NS))
        if text:
            paragraphs.append(text)
    return "\n".join(paragraphs)


def copy_input(src, input_dir):
    src = Path(src).resolve()
    dest = input_dir / src.name
    counter = 2
    while dest.exists() and dest.resolve() != src:
        dest = input_dir / f"{src.stem}-{counter}{src.suffix}"
        counter += 1
    if src != dest:
        shutil.copy2(src, dest)
    return dest


def flatten_to_rgb(image):
    """Convert to RGB, compositing any transparency onto white.

    A bare convert("RGB") drops the alpha channel and exposes whatever colour is
    stored under transparent pixels, which is usually black.
    """
    has_alpha = "A" in image.getbands() or image.mode == "P" and "transparency" in image.info
    if not has_alpha:
        return image.convert("RGB")
    rgba = image.convert("RGBA")
    background = Image.new("RGB", rgba.size, (255, 255, 255))
    background.paste(rgba, mask=rgba.getchannel("A"))
    return background


def save_image_page(src, page_dir):
    page_dir.mkdir(parents=True, exist_ok=True)
    out = page_dir / "source.png"
    with Image.open(src) as image:
        flatten_to_rgb(image).save(out)
    return out


def render_pdf_pages(pdf_path, pages_dir, dpi):
    import pypdfium2 as pdfium

    document = pdfium.PdfDocument(str(pdf_path))
    outputs = []
    scale = dpi / 72.0
    try:
        for index in range(len(document)):
            page = document[index]
            bitmap = None
            try:
                page_dir = pages_dir / f"page_{index + 1:03d}"
                page_dir.mkdir(parents=True, exist_ok=True)
                bitmap = page.render(
                    scale=scale,
                    fill_color=(255, 255, 255, 255),
                    draw_annots=True,
                )
                out = page_dir / "source.png"
                with bitmap.to_pil() as image:
                    rgb = image.convert("RGB")
                    try:
                        rgb.save(out)
                    finally:
                        rgb.close()
                outputs.append(out)
            finally:
                if bitmap is not None:
                    bitmap.close()
                page.close()
    finally:
        document.close()
    return outputs


def rel_source_part(rels_name):
    directory = posixpath.dirname(rels_name)
    if directory.endswith("/_rels"):
        directory = posixpath.dirname(directory)
    source = posixpath.basename(rels_name)[:-5]
    return posixpath.normpath(posixpath.join(directory, source))


def join_part(base_dir, target):
    """Resolve an OPC relationship target: "/ppt/x" is package-absolute, anything else is relative."""
    if target.startswith("/"):
        return posixpath.normpath(target.lstrip("/"))
    return posixpath.normpath(posixpath.join(base_dir, target))


def resolve_target(rels_name, target):
    source = rel_source_part(rels_name)
    return join_part(posixpath.dirname(source), target)


def collect_notes_from_pptx(pptx_path, notes_dir=None):
    notes = []
    if notes_dir:
        notes_dir = Path(notes_dir)
    with zipfile.ZipFile(pptx_path) as z:
        names = set(z.namelist())
        if "ppt/presentation.xml" not in names or "ppt/_rels/presentation.xml.rels" not in names:
            return notes
        pres = ET.fromstring(z.read("ppt/presentation.xml"))
        pres_rels = ET.fromstring(z.read("ppt/_rels/presentation.xml.rels"))
        rels_by_id = {rel.attrib.get("Id"): rel.attrib.get("Target") for rel in pres_rels.findall("rel:Relationship", NS)}
        slide_parts = []
        for sld_id in pres.findall(".//p:sldId", NS):
            rel_id = sld_id.attrib.get(f"{{{NS['r']}}}id")
            target = rels_by_id.get(rel_id)
            if target:
                slide_parts.append(join_part("ppt", target))
        for page_index, slide_part in enumerate(slide_parts, start=1):
            rels_name = f"{posixpath.dirname(slide_part)}/_rels/{posixpath.basename(slide_part)}.rels"
            note = {"page_index": page_index, "text": "", "text_sha256": sha256_text(""), "source_slide": slide_part}
            if rels_name in names:
                root = ET.fromstring(z.read(rels_name))
                for rel in root.findall("rel:Relationship", NS):
                    if rel.attrib.get("Type", "").endswith("/notesSlide"):
                        notes_part = resolve_target(rels_name, rel.attrib.get("Target", ""))
                        if notes_part in names:
                            notes_bytes = z.read(notes_part)
                            notes_root = ET.fromstring(notes_bytes)
                            text = collect_paragraph_text(notes_root)
                            update = {
                                "text": text,
                                "text_sha256": sha256_text(text),
                                "source_notes_part": notes_part,
                            }
                            if notes_dir:
                                out_dir = notes_dir / f"page_{page_index:03d}"
                                out_dir.mkdir(parents=True, exist_ok=True)
                                notes_xml = out_dir / "notesSlide.xml"
                                notes_xml.write_bytes(notes_bytes)
                                update["notes_xml"] = str(notes_xml)
                            note.update(update)
            if note["text"]:
                notes.append(note)
    return notes


def slide_parts_from_pptx(zip_file):
    names = set(zip_file.namelist())
    if "ppt/presentation.xml" not in names or "ppt/_rels/presentation.xml.rels" not in names:
        raise ValueError("PPTX is missing presentation relationships.")
    pres = ET.fromstring(zip_file.read("ppt/presentation.xml"))
    pres_rels = ET.fromstring(zip_file.read("ppt/_rels/presentation.xml.rels"))
    rels_by_id = {rel.attrib.get("Id"): rel.attrib.get("Target") for rel in pres_rels.findall("rel:Relationship", NS)}
    slide_parts = []
    for sld_id in pres.findall(".//p:sldId", NS):
        rel_id = sld_id.attrib.get(f"{{{NS['r']}}}id")
        target = rels_by_id.get(rel_id)
        if target:
            slide_parts.append(join_part("ppt", target))
    if not slide_parts:
        raise ValueError("PPTX has no slides.")
    return slide_parts


def slide_size_from_pptx(zip_file):
    pres = ET.fromstring(zip_file.read("ppt/presentation.xml"))
    size = pres.find(".//p:sldSz", NS)
    if size is None:
        raise ValueError("PPTX is missing slide size.")
    return int(size.attrib["cx"]), int(size.attrib["cy"])


def slide_relationships(zip_file, slide_part):
    rels_name = f"{posixpath.dirname(slide_part)}/_rels/{posixpath.basename(slide_part)}.rels"
    if rels_name not in zip_file.namelist():
        return {}
    root = ET.fromstring(zip_file.read(rels_name))
    return {rel.attrib.get("Id"): rel for rel in root.findall("rel:Relationship", NS)}


def full_slide_picture_target(zip_file, slide_part, slide_cx, slide_cy):
    slide_root = ET.fromstring(zip_file.read(slide_part))
    if collect_paragraph_text(slide_root):
        raise ValueError(f"{slide_part} contains native text and is not an image-based slide.")
    pictures = slide_root.findall(".//p:pic", NS)
    if len(pictures) != 1:
        raise ValueError(f"{slide_part} must contain exactly one full-slide picture; found {len(pictures)}.")
    picture = pictures[0]
    blip = picture.find(".//a:blip", NS)
    if blip is None:
        raise ValueError(f"{slide_part} picture has no embedded image.")
    rel_id = blip.attrib.get(f"{{{NS['r']}}}embed")
    relationships = slide_relationships(zip_file, slide_part)
    rel = relationships.get(rel_id)
    if rel is None or not rel.attrib.get("Type", "").endswith("/image"):
        raise ValueError(f"{slide_part} picture relationship is not an embedded image.")

    off = picture.find(".//a:xfrm/a:off", NS)
    ext = picture.find(".//a:xfrm/a:ext", NS)
    if off is None or ext is None:
        raise ValueError(f"{slide_part} picture has no placement transform.")
    x, y = int(off.attrib.get("x", 0)), int(off.attrib.get("y", 0))
    cx, cy = int(ext.attrib.get("cx", 0)), int(ext.attrib.get("cy", 0))
    tolerance = 2
    if abs(x) > tolerance or abs(y) > tolerance or abs(cx - slide_cx) > tolerance or abs(cy - slide_cy) > tolerance:
        raise ValueError(f"{slide_part} picture is not full-slide.")

    return resolve_target(f"{posixpath.dirname(slide_part)}/_rels/{posixpath.basename(slide_part)}.rels", rel.attrib["Target"])


def extract_image_based_pptx_pages(pptx_path, pages_dir):
    outputs = []
    with zipfile.ZipFile(pptx_path) as z:
        names = set(z.namelist())
        slide_cx, slide_cy = slide_size_from_pptx(z)
        for index, slide_part in enumerate(slide_parts_from_pptx(z), start=1):
            image_part = full_slide_picture_target(z, slide_part, slide_cx, slide_cy)
            if image_part not in names:
                raise ValueError(f"{slide_part} references missing image part: {image_part}")
            page_dir = pages_dir / f"page_{index:03d}"
            page_dir.mkdir(parents=True, exist_ok=True)
            out = page_dir / "source.png"
            with Image.open(io.BytesIO(z.read(image_part))) as image:
                flatten_to_rgb(image).save(out)
            outputs.append(out)
    return outputs


def find_soffice():
    return shutil.which("soffice") or shutil.which("libreoffice")


def convert_office_to_pdf(input_path, out_dir):
    soffice = find_soffice()
    if not soffice:
        raise RuntimeError("No local Office converter is available for this input.")
    out_dir.mkdir(parents=True, exist_ok=True)
    subprocess.run(
        [soffice, "--headless", "--convert-to", "pdf", "--outdir", str(out_dir), str(input_path)],
        check=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    pdfs = sorted(out_dir.glob("*.pdf"))
    if not pdfs:
        raise RuntimeError(f"Office conversion did not produce a PDF in {out_dir}")
    return pdfs[0]


def convert_ppt_to_pptx(input_path, out_dir):
    if input_path.suffix.lower() == ".pptx":
        return input_path
    soffice = find_soffice()
    if not soffice:
        raise RuntimeError("No local Office converter is available to normalize .ppt input.")
    out_dir.mkdir(parents=True, exist_ok=True)
    subprocess.run(
        [soffice, "--headless", "--convert-to", "pptx", "--outdir", str(out_dir), str(input_path)],
        check=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    pptxs = sorted(out_dir.glob("*.pptx"))
    if not pptxs:
        raise RuntimeError(f"Office conversion did not produce a PPTX in {out_dir}")
    return pptxs[0]


def page_record(job_dir, page_index, source, input_path, source_page):
    page_dir = source.parent
    rel_page_dir = page_dir.relative_to(job_dir).as_posix()
    return {
        "page_index": page_index,
        "source_page": source_page,
        "source_image": source.relative_to(job_dir).as_posix(),
        "page_dir": rel_page_dir,
        "manifest": f"{rel_page_dir}/manifest.json",
        "validation": f"{rel_page_dir}/validation.json",
        "input": Path(input_path).name,
        "agent_status": "pending",
    }


def default_job_dir(out_root, input_paths):
    stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    stem = Path(input_paths[0]).stem if input_paths else "job"
    return Path(out_root) / f"{stamp}-{stem}"


def default_output_name(input_paths):
    stem = Path(input_paths[0]).stem if input_paths else "deck"
    return f"{stem}_edited.pptx"


def normalize_inputs(inputs, out_root="output/image2ppt", job_dir=None, dpi=180):
    input_paths = [Path(path).resolve() for path in inputs]
    job_dir = Path(job_dir).resolve() if job_dir else default_job_dir(out_root, input_paths).resolve()
    input_dir = job_dir / "input"
    pages_dir = job_dir / "pages"
    input_dir.mkdir(parents=True, exist_ok=True)
    pages_dir.mkdir(parents=True, exist_ok=True)

    copied = [copy_input(path, input_dir) for path in input_paths]
    suffixes = {path.suffix.lower() for path in copied}
    pages = []
    notes = []
    input_type = "images"

    if len(copied) == 1 and copied[0].suffix.lower() == ".pdf":
        input_type = "pdf"
        sources = render_pdf_pages(copied[0], pages_dir, dpi)
        pages = [page_record(job_dir, i, source, copied[0], i) for i, source in enumerate(sources, start=1)]
    elif len(copied) == 1 and copied[0].suffix.lower() in PPT_EXTS:
        if copied[0].suffix.lower() == ".pptx":
            input_type = "pptx"
            notes = collect_notes_from_pptx(copied[0], input_dir / "notes")
            try:
                sources = extract_image_based_pptx_pages(copied[0], pages_dir)
            except ValueError as exc:
                raise SystemExit(
                    "Unsupported PPTX for the lightweight path: "
                    f"{exc} This skill accepts image-based PPTX files through lightweight extraction. "
                    "Convert native/complex PPTX slides to PDF or page images first."
                ) from exc
        else:
            input_type = "ppt"
            with tempfile.TemporaryDirectory() as tmp:
                tmp_dir = Path(tmp)
                source_pptx = convert_ppt_to_pptx(copied[0], tmp_dir)
                notes = collect_notes_from_pptx(source_pptx, input_dir / "notes")
                if source_pptx != copied[0]:
                    shutil.copy2(source_pptx, input_dir / source_pptx.name)
                rendered_pdf = convert_office_to_pdf(copied[0], tmp_dir)
                sources = render_pdf_pages(rendered_pdf, pages_dir, dpi)
        pages = [page_record(job_dir, i, source, copied[0], i) for i, source in enumerate(sources, start=1)]
    elif suffixes <= IMG_EXTS:
        input_type = "image" if len(copied) == 1 else "images"
        for i, src in enumerate(copied, start=1):
            source = save_image_page(src, pages_dir / f"page_{i:03d}")
            pages.append(page_record(job_dir, i, source, src, i))
    else:
        raise SystemExit(f"Unsupported input combination: {', '.join(str(path) for path in input_paths)}")

    for note in notes:
        notes_xml = note.get("notes_xml")
        if notes_xml:
            note["notes_xml"] = Path(notes_xml).relative_to(job_dir).as_posix()

    notes_manifest_path = job_dir / "notes_manifest.json"
    notes_manifest = {"source": copied[0].relative_to(job_dir).as_posix() if len(copied) == 1 else None, "notes": notes}
    notes_manifest_path.write_text(json.dumps(notes_manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")

    deck_manifest = {
        "input_type": input_type,
        "job_dir": str(job_dir),
        "page_count": len(pages),
        "inputs": [path.relative_to(job_dir).as_posix() for path in copied],
        "pages": pages,
        "notes_manifest": notes_manifest_path.relative_to(job_dir).as_posix(),
        "output": default_output_name(copied),
        "validation": "validation.json",
    }
    deck_manifest_path = job_dir / "deck_manifest.json"
    deck_manifest_path.write_text(json.dumps(deck_manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    return deck_manifest_path
canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill.Stars: 198kUpdated Claude CodeCodexVerifiedInstall