Skip to main content

Read-only Python tools for parsing binary CGM (ISO/IEC 8632) files and exporting image-related data as raw payloads, SVG, and JSON.

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 CGM (ISO/IEC 8632) files and exporting image-related data as raw payloads, SVG, and JSON.

This package focuses on binary CGM parsing and extraction of image-bearing Cell Array elements, plus best-effort vector conversion for vector-oriented CGM content. It does not support writing CGM files.

Installation

Install the latest version using pip:

pip install python-cgm

What It Does

  • Parses binary CGM command streams.
  • Finds Cell Array elements (class 4, element 9).
  • Extracts raw payload bytes from each image-bearing element.
  • Converts vector-like CGM drawing primitives into a best-effort SVG.
  • Exports parsed element data, payload metadata, and embedded SVG as JSON.

Quick Start

from cgm import (
    extract_data_json,
    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))

CLI

After installation, use the CLI to extract payloads directly:

cgm-extract file.cgm ./out

Export vector SVG:

cgm-extract file.cgm ./out --svg

Export parsed data snapshot as JSON:

cgm-extract file.cgm ./out --json

Export both SVG and JSON in one run:

cgm-extract file.cgm ./out --svg --json

Optional flags:

cgm-extract file.cgm ./out --stem payload
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

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 CGM streams.
  • Provides raw payload extraction for raster data, not full pixel decoding to PNG/JPEG.
  • SVG output is best effort and depends on CGM command patterns in the file.
  • 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.1.0.tar.gz (18.1 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.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_cgm-0.1.0.tar.gz
  • Upload date:
  • Size: 18.1 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.1.0.tar.gz
Algorithm Hash digest
SHA256 f36cda01d6bb6502dae26cdbca1df50befb66cdc11474577f97e04c57be34f31
MD5 fa8ecbd92e5b4aa69e0c475304783cb4
BLAKE2b-256 6beb2c03dacd26b5b94c87ecd9e8c4386d4943be7ae9d0b322b5b1cbc0248266

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_cgm-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_cgm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a9846babe0371ce5016327cb592b0037cd3fd679da9b329adc771aff8e5be1c6
MD5 de275fa818a8507d9d462fabf871c90f
BLAKE2b-256 7fd71bb1f392ea5ba316039d14404395325bd7a49343457ee4fe036a78296256

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_cgm-0.1.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