altium monkey v2026.11.26 · gobblerito
On This Page (6)

Examples Showcase / schdoc_add_embedded_image

Add an embedded image to an existing SchDoc

Load a blank schematic, create one detached embedded image, add it to the document, and save the modified SchDoc.

schdocmutationschdocmutationimageembedded-imagestorage

Open a blank schematic, add one embedded image, and save the modified .SchDoc.

This example shows the canonical public object path for embedded schematic images:

  1. load an existing SchDoc
  2. describe the image rectangle in mils
  3. call make_sch_embedded_image(...)
  4. insert the image with AltiumSchDoc.add_object(...)
  5. persist the change with AltiumSchDoc.save(...)

What It Shows

  1. creating a detached embedded image with make_sch_embedded_image(...)
  2. using SchRectMils for absolute placement
  3. using Rotation90 for image orientation
  4. using LineWidth and ColorValue for border styling
  5. saving embedded image payloads through the normal SchDoc object path
  6. reopening the written file and checking the embedded payload

Run

From the project root:

powershell
uv run python examples\schdoc_add_embedded_image\schdoc_add_embedded_image.py

Input

This sample uses:

text
examples/schdoc_add_embedded_image/input/blank.SchDoc
examples/schdoc_add_embedded_image/examples/monkey.png

Output

The script writes:

text
examples/schdoc_add_embedded_image/output/blank_with_embedded_image.SchDoc

Expected Result

The output file should contain one visible embedded image with these characteristics:

  1. source asset: monkey.png
  2. bounds: (1500, 2500) -> (3900, 4900) mil
  3. aspect ratio preserved
  4. border enabled with LineWidth.MEDIUM
  5. one embedded payload entry in the storage stream

The script also prints the reopened image format, bounds, border width, and embedded storage count for a quick terminal sanity check.

Source Code

examples/schdoc_add_embedded_image/schdoc_add_embedded_image.py
from __future__ import annotations

from pathlib import Path

from altium_monkey import (
    AltiumSchDoc,
    ColorValue,
    LineWidth,
    Rotation90,
    SchRectMils,
    make_sch_embedded_image,
)


SAMPLE_DIR = Path(__file__).resolve().parent
INPUT_SCHDOC = SAMPLE_DIR / "input" / "blank.SchDoc"
ASSET_PATH = SAMPLE_DIR / "assets" / "monkey.png"
OUTPUT_DIR = SAMPLE_DIR / "output"
OUTPUT_SCHDOC = OUTPUT_DIR / "blank_with_embedded_image.SchDoc"


def main() -> None:
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    schdoc = AltiumSchDoc(INPUT_SCHDOC)
    image = make_sch_embedded_image(
        bounds_mils=SchRectMils.from_corners_mils(1500, 2500, 3900, 4900),
        source_path=ASSET_PATH,
        keep_aspect=True,
        filename="monkey.png",
        orientation=Rotation90.DEG_0,
        draw_border=True,
        line_width=LineWidth.MEDIUM,
        border_color=ColorValue.from_hex("#000000"),
    )
    schdoc.add_object(image)
    schdoc.save(OUTPUT_SCHDOC)

    reopened = AltiumSchDoc(OUTPUT_SCHDOC)
    reopened_image = reopened.images[0]

    print(f"Loaded: {INPUT_SCHDOC.name}")
    print(f"Images after: {len(reopened.images)}")
    print(f"Image filename: {reopened_image.filename}")
    print(f"Image format: {reopened_image.image_format}")
    print(
        "Image bounds mils: "
        f"({reopened_image.bounds_mils.x1_mils:.0f}, {reopened_image.bounds_mils.y1_mils:.0f})"
        f" -> ({reopened_image.bounds_mils.x2_mils:.0f}, {reopened_image.bounds_mils.y2_mils:.0f})"
    )
    print(f"Image keep_aspect: {reopened_image.keep_aspect}")
    print(f"Image border width: {reopened_image.line_width.name}")
    print(f"Embedded storage entries: {len(reopened.embedded_images)}")
    print(f"Wrote: {OUTPUT_SCHDOC.relative_to(SAMPLE_DIR)}")


if __name__ == "__main__":
    main()
altium-monkey v2026.11.26 · gobblerito·Docs updated 2026.11.29 17:55 UTC (0665053)