Skip to main content

A library for text_to_svg, image_to_svg, and SVG resizing and optimization.

Project description

✨ Pysvgenius ✨

Text ➜ SVG | Image ➜ SVG | Smart SVG Resizing
Turn your text or images into optimized, scalable SVGs effortlessly.

PyPI Version License Python GitHub Stars


📖 Description

Pysvgenius is a Python library that enables:

  • Text to SVG conversion
  • Image to SVG vectorization
  • Smart SVG resizing with optimization

Easily generate scalable vector graphics (SVG) for design, AI, or web applications.


🖼️ Demo

Input Text-to-Image Image-to-SVG Optimized SVG
"A lighthouse overlooking the ocean" Generated Image SVG Conversion Optimized SVG
"A serene Asian dragon" Dragon Image Dragon SVG Dragon Optimized
"Futuristic skyscraper with neon lights" Skyscraper Image Skyscraper SVG Skyscraper Optimized

📦 Installation

# Basic installation
pip install pysvgenius

# With OpenAI CLIP support
pip install pysvgenius[clip]

# Full installation with all extras
pip install pysvgenius[clip,diff_jpeg]

🔧 (Optional) Build DiffVG for Optimizer

To use advanced SVG optimization features, you need to build diffvg from source.

# 1. Clone diffvg repository
git clone https://github.com/BachiLi/diffvg.git
cd diffvg

# 2. Initialize submodules
git submodule update --init --recursive

# 3. Install into current Python environment
python setup.py install

🚀 Usage

1️⃣ Text-to-SVG Generation

Generate SVGs directly from text prompts using the built-in generator:

from pysvgenius.generator import load_generator
from pysvgenius.common import registry

# List all available generator models
print(registry.list_generator())  

# Load the generator (example: SDXL-Turbo)
generator = load_generator("sdxl-turbo")

# Generate 5 SVGs from a text prompt
images = generator("A lighthouse overlooking the ocean", num_images=5)

# images is a list of PIL.Image objects or SVG paths depending on the mode
for idx, img in enumerate(images):
    img.save(f"lighthouse_{idx}.png")

2️⃣ Image-to-SVG Converter

Convert images to SVG paths with the built-in converters:

from pysvgenius.converter import load_converter
from pysvgenius.common import registry

# List all available converters
print(registry.list_converter())

# Load the converter (example: VTracer Binary Search)
converter = load_converter("vtracer-binary-search")

# Convert an image to SVG paths
# `image` can be a PIL.Image or a path to an image
svgs = converter(image, limit=10000)

# `svgs` is a list of SVG path strings
for idx, svg in enumerate(svgs):
    with open(f"output_{idx}.svg", "w") as f:
        f.write(svg)

3️⃣ SVG Ranking (Optional)

After generating SVG candidates, you can rank them using different strategies:

  • Aesthetic Ranker → Scores based on visual aesthetics.
  • SigLIP Ranker → Scores based on semantic similarity to a text prompt.
from pysvgenius import setup_path
from pysvgenius.ranker import load_ranker
from pysvgenius.common import registry

# ✅ Setup paths (run ONCE at the start of your script)
setup_path()

# Check available rankers
print(registry.list_ranker())

# Load rankers
aesthetic_ranker = load_ranker("aesthetic")
siglip_ranker = load_ranker("siglip")

# Rank purely by visual aesthetics (top 5 SVGs)
aesthetic_results = aesthetic_ranker(svgs=svgs, top_k=5)

# Rank by semantic similarity to a text prompt (top 1 SVG)
prompt = "a serene Asian dragon flying over green mountains"
siglip_results = siglip_ranker(svgs=svgs, prompt=prompt, top_k=1)

print("Aesthetic Ranking:", aesthetic_results)
print("SigLIP Ranking:", siglip_results)

4️⃣ Optimize SVGs with DiffVG (Optional)

from pysvgenius.optimizer import load_optimizer
from pysvgenius import load_config, setup_path

# Initialize paths and configuration
setup_path()                     # Run once before loading any model
config = load_config()           # Load default configuration
args = config.optimizer_cfg["diffvg"]["args"]  # Get DiffVG optimizer arguments

# Load the DiffVG Optimizer
optimizer = load_optimizer("diffvg")

# Optimize the SVG based on the original image
# ⚠ Note: 'limit' should match the converter's limit for the best results
optimized_svg = optimizer(
    svg=svgs[0],         # Input SVG
    image=image,         # Original image for comparison
    args=args,           # Optimizer arguments
    limit=20000          # Sampling points, ideally the same as converter's limit
)

📂 Project Structure

pysvgenius/
├── src/
│   ├── generator/          # Text-to-image generation models
│   │   ├── sdxl_turbo_generator.py
│   │   ├── factory.py
│   │   └── base.py
│   ├── converter/          # Image-to-SVG conversion
│   │   ├── vtracer.py
│   │   ├── factory.py
│   │   └── base.py
│   ├── ranker/            # Aesthetic & similarity ranking
│   │   ├── aesthetic_ranker.py
│   │   ├── siglip_ranker.py
│   │   ├── paligemma_ranker.py
│   │   ├── factory.py
│   │   └── base.py
│   ├── optimizer/         # SVG optimization with DiffVG
│   │   ├── diffvg_optimizer.py
│   │   ├── factory.py
│   │   └── base.py
│   ├── utils/             # Utilities and helpers
│   │   ├── image_utils.py
│   │   ├── svg_utils.py
│   │   └── logger.py
│   └── services/          # Service layer
├── configs/               # Configuration files
│   └── configs.yaml
├── models/               # Pre-trained model cache
├── data/                 # Test data and results
│   ├── test/            # Sample input files
│   └── results/         # Output results
├── notebooks/            # Example notebooks

🤝 Contributing

Contributions are welcome! Please open an issue or submit a pull request.

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

pysvgenius-0.1.4.tar.gz (15.3 MB view details)

Uploaded Source

Built Distribution

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

pysvgenius-0.1.4-py3-none-any.whl (16.8 MB view details)

Uploaded Python 3

File details

Details for the file pysvgenius-0.1.4.tar.gz.

File metadata

  • Download URL: pysvgenius-0.1.4.tar.gz
  • Upload date:
  • Size: 15.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for pysvgenius-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c11a1a208632c817ee0e5a5c4b58b95d33170cc67d1da56997848dc81698819f
MD5 9c058f7fc675283c4437d49e87208362
BLAKE2b-256 51a1e76e35bfdb3c8c2dfe32259b50fb24295daaa4501526c6d6198d4a94714d

See more details on using hashes here.

File details

Details for the file pysvgenius-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pysvgenius-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 16.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for pysvgenius-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc5f4c65003ea1fc46f7e8819b3ae706478f4114b1c4b1c27c887f17d927318d
MD5 3b6b7804cf9cf2039407ae314e4e4f3a
BLAKE2b-256 70e6048be684bdffbd30e6f9c9db1fb21076968692530b38cf24af3da0c03880

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