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

tests/test_ocr_http.py

View on GitHub
from __future__ import annotations

import json
import os
import sys
import tempfile
import unittest
import xml.etree.ElementTree as ET
import zipfile
from pathlib import Path
from unittest.mock import patch

import requests
from PIL import Image, ImageDraw


ROOT = Path(__file__).resolve().parents[1]
RUNTIME = ROOT / "cli" / "image2ppt" / "runtime"
sys.path.insert(0, str(RUNTIME))

import deck_text_hints  # noqa: E402
import build_pptx_from_manifest  # noqa: E402
import paddle_text_hints  # noqa: E402
import runtime_env  # noqa: E402


class FakeResponse:
    def __init__(self, *, status_code: int = 200, payload: dict | None = None, text: str = "") -> None:
        self.status_code = status_code
        self._payload = payload or {}
        self.text = text

    def json(self) -> dict:
        return self._payload

    def raise_for_status(self) -> None:
        if self.status_code >= 400:
            raise requests.HTTPError(f"status={self.status_code}")


def write_run(run: Path) -> Path:
    page = run / "pages" / "page_001"
    page.mkdir(parents=True)
    image = Image.new("RGB", (640, 360), "white")
    draw = ImageDraw.Draw(image)
    for index in range(8):
        y = 30 + index * 35
        for char in range(8):
            x = 40 + char * 16
            draw.rectangle([x, y, x + 9, y + 18], outline="black", width=2)
    image.save(page / "source.png")
    (run / "deck_manifest.json").write_text(
        json.dumps({"schema_version": 1, "job_dir": str(run), "input_type": "images", "pages": []}),
        encoding="utf-8",
    )
    (run / "page_jobs.json").write_text(
        json.dumps({"schema_version": 1, "pages": [{"page_id": "page_001", "page_dir": "pages/page_001", "status": "pending"}]}),
        encoding="utf-8",
    )
    return page


class PaddleHttpRegressionTests(unittest.TestCase):
    def test_project_config_precedes_legacy_user_config(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            project = root / "skill"
            user = root / "user"
            project.mkdir()
            user.mkdir()
            (project / "config.yaml").write_text('PADDLE_OCR_TOKEN: "project-token"\n', encoding="utf-8")
            (user / "config.yaml").write_text('PADDLE_OCR_TOKEN: "user-token"\n', encoding="utf-8")
            with patch.object(runtime_env, "SKILL_ROOT", project), patch.object(
                runtime_env, "DEFAULT_CONFIG_HOME", str(user)
            ), patch.dict(os.environ, {"IMAGE2PPT_CONFIG_HOME": ""}, clear=False):
                self.assertEqual(project / "config.yaml", runtime_env.config_path())
                self.assertEqual("project", runtime_env.config_scope())

    def test_token_environment_has_priority_over_local_config(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            home = Path(tmp)
            (home / "config.yaml").write_text('PADDLE_OCR_TOKEN: "config-token"\n', encoding="utf-8")
            with patch.dict(
                os.environ,
                {"IMAGE2PPT_CONFIG_HOME": str(home), "PADDLE_OCR_TOKEN": "environment-token"},
                clear=False,
            ):
                self.assertEqual(deck_text_hints.paddle_token(), "environment-token")

    def test_submit_parameters_and_normal_jsonl_result(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            source = Path(tmp) / "page.png"
            Image.new("RGB", (20, 20), "white").save(source)
            submitted = FakeResponse(payload={"data": {"jobId": "job-123"}})
            status = FakeResponse(
                payload={"data": {"state": "done", "resultUrl": {"jsonUrl": "https://example.test/result.jsonl"}}}
            )
            payload = {
                "result": {
                    "layoutParsingResults": [
                        {"prunedResult": {"width": 20, "height": 20, "parsing_res_list": []}}
                    ]
                }
            }
            result_file = FakeResponse(text=json.dumps(payload) + "\n")
            with patch.object(paddle_text_hints.requests, "post", return_value=submitted) as post, patch.object(
                paddle_text_hints.requests, "get", side_effect=[status, result_file]
            ) as get:
                pages = paddle_text_hints.submit_and_fetch(source, "secret-token", "PaddleOCR-VL-1.6", 30)
            self.assertEqual(len(pages), 1)
            self.assertEqual(post.call_args.args[0], paddle_text_hints.JOB_URL)
            self.assertEqual(post.call_args.kwargs["headers"]["Authorization"], "bearer secret-token")
            self.assertEqual(post.call_args.kwargs["data"]["model"], "PaddleOCR-VL-1.6")
            optional = json.loads(post.call_args.kwargs["data"]["optionalPayload"])
            self.assertEqual(optional["useDocOrientationClassify"], False)
            self.assertEqual(get.call_count, 2)

    def test_empty_result_is_rejected(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            source = Path(tmp) / "page.png"
            Image.new("RGB", (20, 20), "white").save(source)
            responses = [
                FakeResponse(payload={"data": {"state": "done", "resultUrl": {"jsonUrl": "https://example.test/empty"}}}),
                FakeResponse(text=""),
            ]
            with patch.object(
                paddle_text_hints.requests,
                "post",
                return_value=FakeResponse(payload={"data": {"jobId": "empty-job"}}),
            ), patch.object(paddle_text_hints.requests, "get", side_effect=responses):
                with self.assertRaisesRegex(RuntimeError, "returned no pages"):
                    paddle_text_hints.submit_and_fetch(source, "token", paddle_text_hints.DEFAULT_MODEL, 30)

    def test_endpoint_error_is_rejected(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            source = Path(tmp) / "page.png"
            Image.new("RGB", (20, 20), "white").save(source)
            with patch.object(
                paddle_text_hints.requests,
                "post",
                return_value=FakeResponse(status_code=503, text="unavailable"),
            ):
                with self.assertRaisesRegex(RuntimeError, r"submit failed \(503\)"):
                    paddle_text_hints.submit_and_fetch(source, "token", paddle_text_hints.DEFAULT_MODEL, 30)

    def test_failed_job_state_is_rejected(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            source = Path(tmp) / "page.png"
            Image.new("RGB", (20, 20), "white").save(source)
            with patch.object(
                paddle_text_hints.requests,
                "post",
                return_value=FakeResponse(payload={"data": {"jobId": "failed-job"}}),
            ), patch.object(
                paddle_text_hints.requests,
                "get",
                return_value=FakeResponse(payload={"data": {"state": "failed", "errorMsg": "bad input"}}),
            ):
                with self.assertRaisesRegex(RuntimeError, "PaddleOCR job failed: bad input"):
                    paddle_text_hints.submit_and_fetch(source, "token", paddle_text_hints.DEFAULT_MODEL, 30)

    def test_polling_timeout_is_rejected(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            source = Path(tmp) / "page.png"
            Image.new("RGB", (20, 20), "white").save(source)
            with patch.object(
                paddle_text_hints.requests,
                "post",
                return_value=FakeResponse(payload={"data": {"jobId": "slow-job"}}),
            ), patch.object(
                paddle_text_hints.requests,
                "get",
                return_value=FakeResponse(payload={"data": {"state": "running"}}),
            ), patch.object(
                paddle_text_hints.time,
                "time",
                side_effect=[100.0, 101.0],
            ):
                with self.assertRaisesRegex(RuntimeError, r"timed out after 0s \(state=running\)"):
                    paddle_text_hints.submit_and_fetch(source, "token", paddle_text_hints.DEFAULT_MODEL, 0)

    def test_network_exception_is_not_reinterpreted(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            source = Path(tmp) / "page.png"
            Image.new("RGB", (20, 20), "white").save(source)
            with patch.object(
                paddle_text_hints.requests,
                "post",
                side_effect=requests.ConnectionError("offline"),
            ):
                with self.assertRaises(requests.ConnectionError):
                    paddle_text_hints.submit_and_fetch(source, "token", paddle_text_hints.DEFAULT_MODEL, 30)

    def test_cloud_failure_falls_back_and_writes_text_hints(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            run = Path(tmp) / "run"
            page = write_run(run)
            argv = ["deck_text_hints.py", str(run), "--no-overlay"]
            with patch.object(deck_text_hints, "paddle_token", return_value="configured-token"), patch.object(
                deck_text_hints, "paddle_pages", side_effect=RuntimeError("mock endpoint failure")
            ), patch.object(sys, "argv", argv):
                self.assertEqual(deck_text_hints.main(), 0)
            hints = json.loads((page / "text_hints.json").read_text(encoding="utf-8"))
            self.assertEqual(hints["backend"], "builtin-ink")
            self.assertTrue(hints["lines"])

    def test_image_pages_are_submitted_directly_one_job_per_page(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            run = Path(tmp) / "run"
            page_dirs = []
            for index in range(2):
                page = run / "pages" / f"page_{index + 1:03d}"
                page.mkdir(parents=True)
                Image.new("RGB", (40, 30), "white").save(page / "source.png")
                page_dirs.append(page)
            deck = {"input_type": "images"}
            pruned = {"width": 40, "height": 30, "parsing_res_list": []}

            with patch.object(
                paddle_text_hints,
                "submit_and_fetch",
                side_effect=[[pruned], [pruned]],
            ) as submit:
                results = deck_text_hints.paddle_pages(run, deck, page_dirs, "token", 30)

            self.assertEqual(list(results), page_dirs)
            self.assertEqual(
                [call.args[0] for call in submit.call_args_list],
                [page / "source.png" for page in page_dirs],
            )

    def test_original_pdf_is_submitted_as_one_multi_page_job(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            run = Path(tmp) / "run"
            input_dir = run / "input"
            input_dir.mkdir(parents=True)
            original = input_dir / "original.pdf"
            original.write_bytes(b"pdf fixture")
            page_dirs = []
            for index in range(2):
                page = run / "pages" / f"page_{index + 1:03d}"
                page.mkdir(parents=True)
                Image.new("RGB", (40, 30), "white").save(page / "source.png")
                page_dirs.append(page)
            deck = {"input_type": "pdf"}
            pruned = {"width": 40, "height": 30, "parsing_res_list": []}

            with patch.object(
                paddle_text_hints,
                "submit_and_fetch",
                return_value=[pruned, pruned],
            ) as submit:
                results = deck_text_hints.paddle_pages(run, deck, page_dirs, "token", 30)

            self.assertEqual(list(results), page_dirs)
            submit.assert_called_once_with(original, "token", paddle_text_hints.DEFAULT_MODEL, 30)

    def test_pdf_without_original_submits_normalized_images_directly(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            run = Path(tmp) / "run"
            page = run / "pages" / "page_001"
            page.mkdir(parents=True)
            source = page / "source.png"
            Image.new("RGB", (40, 30), "white").save(source)
            pruned = {"width": 40, "height": 30, "parsing_res_list": []}

            with patch.object(
                paddle_text_hints,
                "submit_and_fetch",
                return_value=[pruned],
            ) as submit:
                results = deck_text_hints.paddle_pages(
                    run,
                    {"input_type": "pdf"},
                    [page],
                    "token",
                    30,
                )

            self.assertEqual(list(results), [page])
            submit.assert_called_once_with(source, "token", paddle_text_hints.DEFAULT_MODEL, 30)

    def test_public_result_response_converts_to_editable_pptx(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            page = Path(tmp)
            slide_size = {"width": 12.8, "height": 7.2}
            (page / "page_request.json").write_text(
                json.dumps({"slide": slide_size}), encoding="utf-8",
            )
            image = Image.new("RGB", (320, 180), "white")
            draw = ImageDraw.Draw(image)
            for char in range(8):
                x = 40 + char * 18
                draw.rectangle([x, 60, x + 10, 82], outline="black", width=2)
            image.save(page / "source.png")
            pruned = {
                "width": 320,
                "height": 180,
                "parsing_res_list": [
                    {"block_label": "text", "block_content": "示例文本", "block_bbox": [30, 50, 210, 95]},
                    {"block_label": "image", "block_content": "ignore", "block_bbox": [0, 0, 320, 180]},
                ],
            }
            result_url = "https://results.example.com/job.jsonl"
            result = {"result": {"layoutParsingResults": [{"prunedResult": pruned}]}}
            with patch.object(paddle_text_hints.requests, "post", return_value=FakeResponse(
                payload={"data": {"jobId": "conversion-regression"}},
            )), patch.object(paddle_text_hints.requests, "get", side_effect=[
                FakeResponse(payload={"data": {"state": "done", "resultUrl": {"jsonUrl": result_url}}}),
                FakeResponse(text=json.dumps(result)),
            ]) as get:
                results = paddle_text_hints.submit_and_fetch(
                    page / "source.png", "fixture-token", paddle_text_hints.DEFAULT_MODEL, 30,
                )
            self.assertEqual(results, [pruned])
            self.assertEqual(get.call_count, 2)
            get.assert_called_with(result_url, timeout=60)
            hints = paddle_text_hints.build_page_hints(page, results[0])
            self.assertEqual(hints["backend"], "paddleocr-vl")
            self.assertEqual([line["text"] for line in hints["lines"]], ["示例文本"])
            self.assertIn(hints["lines"][0]["glyph_source"], {"ink-measured", "bbox-estimate"})
            self.assertIn("font_pt", hints["lines"][0])

            # Only the HTTP boundary is replayed: measurement and PPTX writing run for real.
            line = hints["lines"][0]
            manifest = {
                "slide": slide_size,
                "source": hints["source"],
                "text_boxes": [{
                    "text": line["text"], "box_px": line["box_px"],
                    "font_size": line["font_pt"], "font_size_source": "measured",
                }],
            }
            manifest_path = page / "manifest.json"
            manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
            output = page / "converted.pptx"
            build_pptx_from_manifest.write_pptx(manifest, output, manifest_path)
            with zipfile.ZipFile(output) as package:
                self.assertIsNone(package.testzip())
                slide = ET.fromstring(package.read("ppt/slides/slide1.xml"))
            ns = {
                "a": "http://schemas.openxmlformats.org/drawingml/2006/main",
                "p": "http://schemas.openxmlformats.org/presentationml/2006/main",
            }
            boxes = slide.findall(".//p:sp", ns)
            self.assertEqual(len(boxes), 1)
            self.assertEqual([node.text for node in boxes[0].findall(".//a:t", ns)], ["示例文本"])
            self.assertEqual(boxes[0].find("p:nvSpPr/p:cNvSpPr", ns).get("txBox"), "1")
            self.assertEqual(slide.findall(".//p:pic", ns), [])
            self.assertEqual(
                int(boxes[0].find(".//a:rPr", ns).get("sz")), int(line["font_pt"] * 100),
            )
            x, y, width, height = line["box_px"]
            off = boxes[0].find("p:spPr/a:xfrm/a:off", ns)
            extent = boxes[0].find("p:spPr/a:xfrm/a:ext", ns)
            self.assertEqual(int(off.get("x")), round(x / 320 * 12.8 * 914400))
            self.assertEqual(int(off.get("y")), round(y / 180 * 7.2 * 914400))
            self.assertEqual(int(extent.get("cx")), round(width / 320 * 12.8 * 914400))
            self.assertEqual(int(extent.get("cy")), round(height / 180 * 7.2 * 914400))


if __name__ == "__main__":
    unittest.main()
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