python-cgm
Read-only Python tools for parsing binary and clear-text CGM (ISO/IEC 8632) files, producing final SVG output with optional raster tile backgrounds, and extracting hotspot metadata as JSON.
This package focuses on practical CGM extraction workflows: parsing CGM content,
extracting image-bearing Cell Array payloads, decoding clear-text tile arrays,
composing raster+vector SVG output, and recovering hotspots from 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) and extracts their raw payload bytes. - Decodes clear-text tiled bitonal, indexed, and direct-color arrays.
- Decodes prefixed class 4, id 29 raster payloads with conservative fallback candidates.
- Builds a final SVG output that can include an embedded raster background.
- Converts vector-like CGM drawing primitives into SVG overlays.
- Extracts hotspots from APD
name/regionrecords and APS geometry groups. - Exports parsed element data, payload metadata, rendered SVG, and hotspots 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_rendered_images_to_directory(file_path, output_dir, stem="image", debug_report=False) -> 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]
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: dimensions when present in common binary or clear-text tile layouts.local_color_precision: declared color precision for the payload when available.cell_representation_mode: declared cell representation mode when available.
Supported CGM Elements And Features
The module focuses on practical extraction/rendering coverage for common binary and clear-text CGM workflows.
Binary CGM Element Coverage
class 1, id 3(VDC Type): used to choose coordinate decoding path.class 1, id 10(Color Value Extent): used for 16-bit direct-color scaling.class 1, id 11(VDC Integer Precision): used for strict integer VDC decode.class 1, id 12(VDC Real Precision): used for strict real VDC decode.class 2, id 6(VDC Extent): used to set SVG view extents and raster placement.class 3, id 4(Transparency): mapped to SVG background behavior.class 3, id 5(Clip Rectangle): mapped to SVG clip paths.class 3, id 6(Clip Indicator): enables/disables clipping.class 4, id 1(Polyline): rendered to SVG polylines.class 4, id 2(Disjoint Polyline): rendered as segment polylines.class 4, id 3(Polymarker): rendered as SVG marker circles.class 4, id 4(Textcontinuation context): appended to prior text where applicable.class 4, id 5(Text): rendered to SVG text.class 4, id 6(Append Text): appended to prior text runs.class 4, id 7(Polygon): rendered to SVG polygons.class 4, id 8(Polygon Set): rendered as polygon geometry.class 4, id 9(Cell Array): extracted asRawImagepayloads and used as raster candidates.class 4, id 10andid 26(GDP-like primitives): decoded as polyline-style vectors.class 4, id 11(Rectangle): rendered as SVG rect.class 4, id 12(Circle): rendered as SVG circle.class 4, ids 13-16, 18-25, 27(arc families): rendered as best-effort polyline geometry.class 4, id 17(Ellipse): rendered as SVG ellipse.class 4, id 28: parsed but no strict vector fallback rendering.class 4, id 29(Restricted Textor prefixed raster payload): restricted text is rendered when text payload decodes; non-text prefixed payloads are evaluated as raster candidates.class 5, id 3(Line Width): applied to SVG stroke width.class 5, id 4(Line Color): applied via palette/index mapping.class 5, id 15(Character Height): applied to SVG text size.class 5, id 34(Color Table): used for indexed palette and color mapping.class 9, id 1(Application Data/ APD): used for hotspotname/regionextraction.class 0, id 21/22/23(APS begin/end forms): used for hotspot grouping.
Clear-Text Command Coverage
- Vector primitives:
LINE,POLYLINE,DISJOINTPOLYLINE,POLYMARKER,POLYGON,POLYGONSET,RECTANGLE,CIRCLE,ARC3PT,ARCCENTRE,ELLIPSE,ELLIPARC,GDP. - Text primitives:
TEXT,APPENDTEXT,RESTRICTEDTEXT. - Raster/tile commands:
CELLARRAY,BEGTILEARRAY/ENDTILEARRAY,BITONALTILE,MONOCHROMETILE,INDEXCOLORTILE,COLORTILE,DIRECTCOLORTILE(and colour spelling variants). - Attributes/control:
VDCEXT,COLRVALUEEXT,COLRTABLE,LINECOLR,TRANSPARENCY,CLIPRECT,CLIPIND. - Hotspot-related data:
BEGAPS,APD,ENDAPS.
Raster Decoding Features
- Extracts raw
Cell Arraypayload bytes with metadata where present. - Decodes bitonal raster data for uncompressed, CCITT Group 3, and CCITT Group 4 paths.
- Decodes indexed-color and direct-color tile payloads when dimensions/precision are usable.
- Composes raster backgrounds into SVG (embedded PNG data URI) before vector overlays.
- For multi-payload class 4, id 29 rasters, emits separate SVG
<image>tile overlays when a simple inferred tile grid decodes successfully.
Hotspot Features
- Extracts APD
nameandregionrecords into hotspot JSON. - Falls back to APS geometry-based bounding boxes when explicit region data is absent.
Scope And Limitations
- The exact supported CGM elements/commands are listed in the Supported CGM Elements And Features section above.
- This project is extraction-oriented: it parses and exports data/SVG/JSON, but does not support CGM authoring or round-trip editing.
- Rendering is best-effort for many real-world files; unsupported or profile-specific constructs may be skipped rather than heuristically rewritten.
- Raster composition is metadata-dependent. Clear-text tile arrays are composed directly; binary Cell Array payloads are extracted and used as raster candidates.
- Raster decoding requires runtime dependencies
Pillowandimagecodecs(installed with this package by default). - JSON exports can be large because they include full element parameter and payload hex data.
License
python-cgm (C) 2026 Kestin Goforth.
This project is licensed under the BSD 3-Clause License - see the license file for details.
Release files for python-cgm 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_cgm-0.3.3.tar.gz | 50.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_cgm-0.3.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 92.4 kB
Release files / python_cgm-0.3.3.tar.gz
| Download URL | python_cgm-0.3.3.tar.gz |
|---|---|
| Size | 50.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
112ac28ef9c8d11c9d8b02d8659a696b3f9a846304063bcc302b793f169f1e53
|
|
BLAKE2b-256 checksum How to use checksums |
f967dbb7c03f4d375a30255720ba7de3789f113d4683963bc36a67301bbfd0d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 21, 2026.
Transparency logRelease files / python_cgm-0.3.3-py3-none-any.whl
| Download URL | python_cgm-0.3.3-py3-none-any.whl |
|---|---|
| Size | 41.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3844ea894ea31e721f45e1dca9c4851810c13f3dc38d169803bc2cb23023bf3
|
|
BLAKE2b-256 checksum How to use checksums |
4e9749a5bbba98f806256300c6b7c4248e51842a3f67ec35d5443d85577c4208
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 21, 2026.
Transparency log