Skip to main content

Read-only Python tools for parsing binary and clear-text CGM files, composing final SVG output, and extracting hotspots.

Project description

python-cgm

GitHub GitHub License GitHub Forks GitHub Stars

PyPI Version Pepy Total Downloads PyPI Downloads

Read-only Python tools for parsing binary and clear-text CGM (ISO/IEC 8632) files, producing a single final SVG image output (with optional embedded raster background), and extracting hotspot metadata as JSON.

This package focuses on practical CGM extraction workflows: parsing CGM content, extracting image-bearing Cell Array payloads, composing raster+vector SVG output, and recovering hotspots from both APD region properties and APS geometry fallback. It does not support writing CGM files.

Installation

Install the latest version using pip:

pip install python-cgm

What It Does

  • Parses binary and clear-text CGM command streams.
  • Finds Cell Array elements (class 4, element 9).
  • Extracts raw payload bytes from each image-bearing element.
  • Builds a final SVG output that can include an embedded raster background.
  • Stitches/decodes clear-text tiled bitonal arrays for SVG background composition.
  • Converts vector-like CGM drawing primitives into a best-effort SVG overlay.
  • Extracts hotspots from APD name/region and APS geometry groups (for files such as GR-77775).
  • Exports parsed element data, payload metadata, and embedded SVG as JSON.

Quick Start

from cgm import (
    extract_data_json,
    extract_final_image_and_hotspots,
    extract_hotspots,
    extract_raw_images,
    extract_raw_images_to_directory,
    extract_vector_svg,
)

images = extract_raw_images("drawing.cgm")
print(f"Found {len(images)} raster payload(s)")

for image in images:
    print(
        image.index,
        image.element_offset,
        image.width,
        image.height,
        len(image.payload),
    )

written = extract_raw_images_to_directory("drawing.cgm", "./out")
print("Wrote", len(written), "payload file(s)")

svg = extract_vector_svg("drawing.cgm")
print("SVG length:", len(svg))

snapshot_json = extract_data_json("drawing.cgm")
print("JSON length:", len(snapshot_json))

final = extract_final_image_and_hotspots("drawing.cgm")
print("Final SVG length:", len(final["image"]))
print("Hotspots:", len(final["hotspots"]))

hotspots = extract_hotspots("drawing.cgm")
print("Hotspot objects:", len(hotspots))

CLI

After installation, use the CLI to export the final SVG and hotspot JSON:

cgm-extract file.cgm ./out

By default this writes:

  • <basename>_0000.svg
  • <basename>_0000.hotspots.json

With debug enabled it also writes:

  • <basename>_decode_report.json

Optional flag:

cgm-extract file.cgm ./out --debug

API

  • extract_raw_images(file_path) -> list[RawImage]
  • extract_raw_images_from_bytes(data) -> list[RawImage]
  • extract_raw_images_to_directory(file_path, output_dir, stem="image") -> list[Path]
  • extract_vector_svg(file_path) -> str
  • extract_vector_svg_from_bytes(data) -> str
  • extract_vector_svg_to_directory(file_path, output_dir, stem="image") -> Path
  • extract_data_json(file_path) -> str
  • extract_data_json_from_bytes(data) -> str
  • extract_data_json_to_directory(file_path, output_dir, stem="image") -> Path
  • extract_hotspots(file_path) -> list[HotSpot]
  • extract_hotspots_from_bytes(data) -> list[HotSpot]
  • extract_hotspots_to_directory(file_path, output_dir, stem="image") -> Path
  • extract_final_image_and_hotspots(file_path) -> dict[str, object]
  • extract_rendered_images_to_directory(file_path, output_dir, stem="image", debug_report=False) -> list[Path]

RawImage fields:

  • index: zero-based image index.
  • element_offset: byte offset of the CGM element in the source file.
  • payload: raw image payload bytes.
  • width / height: best-effort dimensions when present in common binary layouts.

Scope And Limitations

  • Supports binary and clear-text CGM streams used by this project.
  • Raster decoding/composition is best effort and depends on payload encoding patterns.
  • SVG output is best effort and depends on CGM command patterns in the file.
  • Hotspot extraction is best effort and supports APD region records plus APS geometry fallback.
  • JSON output can be large because it includes full element parameter/payload hex data.
  • Does not support CGM writing.

License

python-cgm (C) 2026 Kestin Goforth.

This project is licensed under the BSD 3-Clause License - see the license file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_cgm-0.2.0.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_cgm-0.2.0-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file python_cgm-0.2.0.tar.gz.

File metadata

  • Download URL: python_cgm-0.2.0.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_cgm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 367fdee69c2a713e018c43fd740ed6e87fd60dd8252332960aca2bf143dba809
MD5 2f11a9559d609157e3eab32ab4a29ba9
BLAKE2b-256 7a35fde53f16abee59e23168530d6a1b9502edbfa551b52b53672f3a94c4bc74

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_cgm-0.2.0.tar.gz:

Publisher: publish-to-pypi.yml on kForth/python-cgm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_cgm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: python_cgm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_cgm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab8fc2080168a339a6906b40e115a9e088c53ff36e4b83058724de08dfb3f269
MD5 b9275f89567c696c5dde072f6b8eedec
BLAKE2b-256 aece1e8f34de5a21f55c0be8eec8c30ff4a983abc8de9dc895d8f6763ccac729

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_cgm-0.2.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on kForth/python-cgm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page