(CVPR 2025) Code of "Chat2SVG: Vector Graphics Generation with Large Language Models and Image Diffusion Models"
250
stars
30
commits
Python
primary language
Apr 2, 2025
updated

Chat2SVG is a framework for generating vector graphics using large language models and image diffusion models. The system works in multiple stages to generate, enhance, and optimize SVG from text descriptions.
.env file. You can also adjust the max_tokens parameter in utils/gpt.py on line 127. Thanks to @potpov's contribution.
Clone the repository:
git clone git@github.com:kingnobro/Chat2SVG.git
cd Chat2SVG
conda create --name chat2svg python=3.10
conda activate chat2svg
Install PyTorch and other dependencies:
conda install pytorch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 pytorch-cuda=11.8 -c pytorch -c nvidia
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install -r requirements.txt
Install diffvg for differentiable rendering:
git clone https://github.com/BachiLi/diffvg.git
cd diffvg
git submodule update --init --recursive
conda install -y -c anaconda cmake
conda install -y -c conda-forge ffmpeg
pip install svgwrite svgpathtools cssutils torch-tools
python setup.py install
cd ..
Install picosvg for SVG cleaning:
git clone git@github.com:googlefonts/picosvg.git
cd picosvg
pip install -e .
cd ..
[!TIP] We provide two ways to generate SVG templates:
- If you want to create high-quality SVG, we recommend checking the output of each stage to ensure the generated SVG meet "human-preferred" criteria.
- If you want to compare the performance of our method with your own SVG generation method, we also provide a simple way to automatically generate all outputs.
[!CAUTION] Hong Kong is banned by Anthropic/OpenAI. Therefore, I use a third-party API from WildCard to forward requests to Claude. If you are in a region where you can access Anthropic/OpenAI directly, you can modify lines 64-65 in
utils/gpt.pyto use the original Anthropic API. Additional modifications may be required. Sorry for the inconvenience.
We have provided some sample generation and intermediate results in the
output/example_generationfolder. You can check them to get a better understanding of the pipeline.
First, paste your Anthropic API key into the .env file:
OPENAI_API_KEY=<your_key>
Then, run the following command to generate SVG templates:
cd 1_template_generation
bash run.sh
utils/util.py → get_prompt().output/example_generation/stage_1 folder.target_template.svg (e.g., apple_template.svg) file in the root directory.[!TIP] Our visual rectification process can solve common issues in SVG. However, we've observed that in some cases, VLM may actually degrade the quality of the SVG during rectification. We recommend double-checking the output before and after rectification to ensure the best results.
cd 2_detail_enhancement
bash download_models.sh # download pretrained model weights
bash run.sh # detail enhancement
The above command will:
apple_clean.svgapple_target.pngapple_with_new_shape.svg[!TIP]
- Adjust the
strengthto control the strength of the SDEdit (Image to Image). We recommend0.75for mild enhancement and1.0for strong enhancement.- The default number of generated target images is
4, and we select the first one as the default target image. You can check all generated images to select your preferred one.- Adjust
points_per_sidein SAM to control the granularity of the added shapes, and adjustthresh_iouto control the threshold that determines whether a shape is a new shape or not.- As mentioned in the paper's limitation section, SAM sometimes may not add appropriate shapes. Please check the output and modify if necessary.
cd 3_svg_optimization
bash download_models.sh # download pretrained SVG VAE model
bash run.sh # optimize SVG shapes (GPU consumption: less than 4GB)
[!TIP]
- We turn off
enable_path_iou_lossby default, which can greatly improve time efficiency. To avoid path semantic meaning shifts, you can set it toTrue.- We proportionally scale up the loss weights (different from the paper) to ensure faster convergence.
- Results:
apple_optim_latent.svgandapple_optim_point.svg
Code coming soon. Alternatively, you can enter each folder and run the run.sh script to generate all outputs.
Python
99.6%
(CVPR 2025) Code of "Chat2SVG: Vector Graphics Generation with Large Language Models and Image Diffusion Models"
250
stars
30
commits
Python
primary language
Apr 2, 2025
updated

Chat2SVG is a framework for generating vector graphics using large language models and image diffusion models. The system works in multiple stages to generate, enhance, and optimize SVG from text descriptions.
.env file. You can also adjust the max_tokens parameter in utils/gpt.py on line 127. Thanks to @potpov's contribution.
Clone the repository:
git clone git@github.com:kingnobro/Chat2SVG.git
cd Chat2SVG
conda create --name chat2svg python=3.10
conda activate chat2svg
Install PyTorch and other dependencies:
conda install pytorch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 pytorch-cuda=11.8 -c pytorch -c nvidia
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install -r requirements.txt
Install diffvg for differentiable rendering:
git clone https://github.com/BachiLi/diffvg.git
cd diffvg
git submodule update --init --recursive
conda install -y -c anaconda cmake
conda install -y -c conda-forge ffmpeg
pip install svgwrite svgpathtools cssutils torch-tools
python setup.py install
cd ..
Install picosvg for SVG cleaning:
git clone git@github.com:googlefonts/picosvg.git
cd picosvg
pip install -e .
cd ..
[!TIP] We provide two ways to generate SVG templates:
- If you want to create high-quality SVG, we recommend checking the output of each stage to ensure the generated SVG meet "human-preferred" criteria.
- If you want to compare the performance of our method with your own SVG generation method, we also provide a simple way to automatically generate all outputs.
[!CAUTION] Hong Kong is banned by Anthropic/OpenAI. Therefore, I use a third-party API from WildCard to forward requests to Claude. If you are in a region where you can access Anthropic/OpenAI directly, you can modify lines 64-65 in
utils/gpt.pyto use the original Anthropic API. Additional modifications may be required. Sorry for the inconvenience.
We have provided some sample generation and intermediate results in the
output/example_generationfolder. You can check them to get a better understanding of the pipeline.
First, paste your Anthropic API key into the .env file:
OPENAI_API_KEY=<your_key>
Then, run the following command to generate SVG templates:
cd 1_template_generation
bash run.sh
utils/util.py → get_prompt().output/example_generation/stage_1 folder.target_template.svg (e.g., apple_template.svg) file in the root directory.[!TIP] Our visual rectification process can solve common issues in SVG. However, we've observed that in some cases, VLM may actually degrade the quality of the SVG during rectification. We recommend double-checking the output before and after rectification to ensure the best results.
cd 2_detail_enhancement
bash download_models.sh # download pretrained model weights
bash run.sh # detail enhancement
The above command will:
apple_clean.svgapple_target.pngapple_with_new_shape.svg[!TIP]
- Adjust the
strengthto control the strength of the SDEdit (Image to Image). We recommend0.75for mild enhancement and1.0for strong enhancement.- The default number of generated target images is
4, and we select the first one as the default target image. You can check all generated images to select your preferred one.- Adjust
points_per_sidein SAM to control the granularity of the added shapes, and adjustthresh_iouto control the threshold that determines whether a shape is a new shape or not.- As mentioned in the paper's limitation section, SAM sometimes may not add appropriate shapes. Please check the output and modify if necessary.
cd 3_svg_optimization
bash download_models.sh # download pretrained SVG VAE model
bash run.sh # optimize SVG shapes (GPU consumption: less than 4GB)
[!TIP]
- We turn off
enable_path_iou_lossby default, which can greatly improve time efficiency. To avoid path semantic meaning shifts, you can set it toTrue.- We proportionally scale up the loss weights (different from the paper) to ensure faster convergence.
- Results:
apple_optim_latent.svgandapple_optim_point.svg
Code coming soon. Alternatively, you can enter each folder and run the run.sh script to generate all outputs.
Python
99.6%