Read-only Python tools for parsing binary and clear-text CGM files, composing final SVG output, and extracting hotspots.
Project description
python-cgm
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 Arrayelements (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/regionand 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) -> strextract_vector_svg_from_bytes(data) -> strextract_vector_svg_to_directory(file_path, output_dir, stem="image") -> Pathextract_data_json(file_path) -> strextract_data_json_from_bytes(data) -> strextract_data_json_to_directory(file_path, output_dir, stem="image") -> Pathextract_hotspots(file_path) -> list[HotSpot]extract_hotspots_from_bytes(data) -> list[HotSpot]extract_hotspots_to_directory(file_path, output_dir, stem="image") -> Pathextract_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
367fdee69c2a713e018c43fd740ed6e87fd60dd8252332960aca2bf143dba809
|
|
| MD5 |
2f11a9559d609157e3eab32ab4a29ba9
|
|
| BLAKE2b-256 |
7a35fde53f16abee59e23168530d6a1b9502edbfa551b52b53672f3a94c4bc74
|
Provenance
The following attestation bundles were made for python_cgm-0.2.0.tar.gz:
Publisher:
publish-to-pypi.yml on kForth/python-cgm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_cgm-0.2.0.tar.gz -
Subject digest:
367fdee69c2a713e018c43fd740ed6e87fd60dd8252332960aca2bf143dba809 - Sigstore transparency entry: 2169727632
- Sigstore integration time:
-
Permalink:
kForth/python-cgm@07a4712de083ac6f10e8d2188fef2b2c330858fd -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/kForth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@07a4712de083ac6f10e8d2188fef2b2c330858fd -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab8fc2080168a339a6906b40e115a9e088c53ff36e4b83058724de08dfb3f269
|
|
| MD5 |
b9275f89567c696c5dde072f6b8eedec
|
|
| BLAKE2b-256 |
aece1e8f34de5a21f55c0be8eec8c30ff4a983abc8de9dc895d8f6763ccac729
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_cgm-0.2.0-py3-none-any.whl -
Subject digest:
ab8fc2080168a339a6906b40e115a9e088c53ff36e4b83058724de08dfb3f269 - Sigstore transparency entry: 2169727640
- Sigstore integration time:
-
Permalink:
kForth/python-cgm@07a4712de083ac6f10e8d2188fef2b2c330858fd -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/kForth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@07a4712de083ac6f10e8d2188fef2b2c330858fd -
Trigger Event:
push
-
Statement type: