Skip to main content

Python wrapper for svg-matrix - arbitrary-precision SVG optimization, validation, and manipulation

Project description

svg-matrix (Python)

Python wrapper for svg-matrix - arbitrary-precision SVG optimization, validation, and manipulation.

Installation

pip install svg-matrix

Prerequisites: Requires Bun (recommended) or Node.js installed on your system. The package will attempt to auto-install Bun if neither is found.

# Install Bun (recommended, faster)
curl -fsSL https://bun.sh/install | bash

# Or use Node.js
# https://nodejs.org/

Quick Start

from svg_matrix import validate_svg, optimize_svg, to_plain_svg

# Validate an SVG file
result = validate_svg("icon.svg")
if result["valid"]:
    print("SVG is valid!")
else:
    for issue in result["issues"]:
        print(f"Issue: {issue['reason']}")

# Optimize an SVG file
optimize_svg("large.svg", "small.svg", precision=4)

# Convert Inkscape SVG to plain SVG
to_plain_svg("inkscape_drawing.svg", "plain.svg")

Features

Validation

from svg_matrix import validate_svg, validate_svg_async

# Synchronous validation
result = validate_svg("file.svg")
# Returns: {"valid": bool, "issues": list, "error": str|None}

# Async validation
import asyncio
result = await validate_svg_async("file.svg")

# Validate SVG string content
result = validate_svg('<svg xmlns="http://www.w3.org/2000/svg">...</svg>')

Optimization

from svg_matrix import optimize_svg, optimize_paths

# Full optimization
optimize_svg("input.svg", "output.svg",
    precision=6,        # Decimal precision (default: 6)
    minify=True,        # Minify output
    remove_comments=True,
    remove_metadata=True
)

# Path-only optimization (lighter)
optimize_paths("input.svg", "output.svg", precision=4)

Conversion

from svg_matrix import to_plain_svg, flatten, convert_shapes

# Remove Inkscape namespaces
to_plain_svg("inkscape.svg", "plain.svg")

# Flatten transforms, groups, clipPaths
flatten("complex.svg", "flat.svg",
    flatten_transforms=True,
    flatten_groups=True,
    flatten_clipaths=True
)

# Convert shapes to paths
convert_shapes("shapes.svg", "paths.svg")

Direct CLI Access

from svg_matrix import run_svgm, run_svg_matrix, get_info

# Run svgm with any arguments
result = run_svgm(["input.svg", "-o", "output.svg", "-p", "4"])
print(result["stdout"])

# Run svg-matrix commands
result = run_svg_matrix(["info", "file.svg"])

# Get SVG info
info = get_info("file.svg")
print(f"Width: {info.get('width')}, Height: {info.get('height')}")

CLI Commands

The package installs Python wrapper commands:

# Optimize SVG
svgm-py input.svg -o output.svg -p 4

# Get SVG info
svg-matrix-py info input.svg

# Lint SVG
svglinter-py input.svg

# Font operations
svgfonts-py embed input.svg -o output.svg

Batch Processing

from pathlib import Path
from svg_matrix import validate_svg, optimize_svg

# Process all SVGs in a directory
svg_dir = Path("./svgs")
output_dir = Path("./optimized")
output_dir.mkdir(exist_ok=True)

for svg_file in svg_dir.glob("*.svg"):
    result = validate_svg(svg_file)
    if result["valid"]:
        optimize_svg(svg_file, output_dir / svg_file.name)
        print(f"Optimized: {svg_file.name}")
    else:
        print(f"Skipped (invalid): {svg_file.name}")

API Reference

Validation Functions

Function Description
validate_svg(svg_input, strict=False) Validate SVG file or string
validate_svg_async(svg_input, strict=False) Async validation

Optimization Functions

Function Description
optimize_svg(input, output, ...) Full SVG optimization
optimize_paths(input, output, precision) Path-only optimization

Conversion Functions

Function Description
to_plain_svg(input, output) Remove Inkscape namespaces
flatten(input, output, ...) Flatten transforms/groups
convert_shapes(input, output) Convert shapes to paths

CLI Functions

Function Description
run_svgm(args, timeout) Run svgm CLI
run_svg_matrix(args, timeout) Run svg-matrix CLI
get_info(svg_path) Get SVG file info

How It Works

This package is a thin Python wrapper around the @emasoft/svg-matrix npm package. It uses bunx (or npx as fallback) to execute the Node.js CLI tools.

Advantages:

  • Always uses the latest svg-matrix version
  • Minimal Python package size (~20KB)
  • Full feature parity with the Node.js library

Requirements:

  • Bun or Node.js must be installed
  • First run may take a few seconds to download the npm package

License

MIT License - see LICENSE

Links

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

svg_matrix-1.3.6.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

svg_matrix-1.3.6-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file svg_matrix-1.3.6.tar.gz.

File metadata

  • Download URL: svg_matrix-1.3.6.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for svg_matrix-1.3.6.tar.gz
Algorithm Hash digest
SHA256 12af0bcbbe9d6cb1d38b947a915bd9726b2cb4447f1e154d107d5abf9d11ea22
MD5 0531e517e3199c1d82dfd937018ad17e
BLAKE2b-256 798015b73be2356a7c2c10045641eb6fe1651a7b07feb1aec8f1d54fda740fc1

See more details on using hashes here.

File details

Details for the file svg_matrix-1.3.6-py3-none-any.whl.

File metadata

  • Download URL: svg_matrix-1.3.6-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for svg_matrix-1.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7f851041a27e18dc273cc29a8f6b7c277ec8b3635e1dc30a3ca3872503c71c37
MD5 c956e01007883056e22dae2176973b33
BLAKE2b-256 a727fa30ecbb02333c56e17d6eacdf314c5a7498e528ac004153cadb71707ae7

See more details on using hashes here.

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