A Python-based tool for processing SVG files, supporting optimization, conversion to SVGZ (gzip-compressed), and compilation to Vec format for Flutter applications. The tool offers two interfaces:
- Console-based:
svg_optimizer_console.py
for command-line usage. - GUI-based:
svg_optimizer_pyqt5.py
with a PyQt5 interface supporting drag-and-drop.
- Optimize SVG: Removes metadata, simplifies IDs, strips unnecessary attributes, and cleans up namespaces using
lxml
. - Convert to SVGZ: Creates gzip-compressed SVGZ files for smaller file sizes.
- Compile to Vec: Converts SVG files to Flutter’s Vec format using
vector_graphics_compiler
for efficient rendering in Flutter apps. - Batch Processing: Processes individual files or entire folders.
- Cross-Platform: Runs on macOS, Linux, and Windows.
- Python 3.6+: Required for running the scripts.
- Dart: Needed for installing
vector_graphics_compiler
. - Dependencies:
- Python packages:
lxml
,PyQt5
,Pillow
. - Global
vector_graphics_compiler
for Vec compilation.
- Python packages:
-
Clone the Repository:
git clone https://github.com/your-username/SVG-Converter.git cd SVG-Converter
-
Set Up a Virtual Environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Python Dependencies:
pip install lxml PyQt5 Pillow
-
Install vector_graphics_compiler Globally:
dart pub global activate vector_graphics_compiler
Ensure
~/.pub-cache/bin
is in your PATH:echo 'export PATH="$PATH:$HOME/.pub-cache/bin"' >> ~/.zshrc # or ~/.bashrc source ~/.zshrc
Verify installation:
vector_graphics_compiler --help
-
Run the Setup Script (optional): The included
run.sh
(orrun.bat
on Windows) automates dependency installation and script execution:chmod +x run.sh ./run.sh
Run the console script to process SVG files or folders via command-line arguments or interactive prompts.
-
Process a Single File:
python3 svg_optimizer_console.py --vec /path/to/file.svg
Outputs:
file_compiled.vec
(or_optimized.svg
/_optimized.svgz
if--optimize
/--svgz
is used). -
Process a Folder:
python3 svg_optimizer_console.py --folder /path/to/folder --optimize --svgz --vec
Processes all
.svg
and.svgz
files in the folder. -
Interactive Mode:
python3 svg_optimizer_console.py
Enter a file or folder path when prompted.
-
Options:
--optimize
(-o
): Optimize SVG (default: True).--svgz
(-z
): Output as SVGZ.--vec
(-v
): Compile to Vec.--folder
(-f
): Process all SVGs in a folder.
Run the GUI script for a graphical interface with drag-and-drop support.
-
Launch the GUI:
python3 svg_optimizer_pyqt5.py
Or via
run.sh
:./run.sh
Select option
2
. -
Usage:
- Drag and drop SVG files or folders onto the window.
- Or click Select Folder to choose a folder.
- Check desired options:
- Optimize SVG: Optimize the SVG file.
- Output as SVGZ: Create a compressed SVGZ file.
- Compile to Vec: Compile to Vec format.
- The status label shows processing results.
The run.sh
script simplifies setup and execution:
./run.sh
- Select
1
for console mode or2
for GUI mode. - For console mode, pass arguments:
./run.sh --vec /path/to/file.svg
To use Vec files in a Flutter project:
- Add the Vec file to your
pubspec.yaml
:flutter: assets: - assets/file_compiled.vec
- Use the
VectorGraphic
widget:import 'package:flutter/material.dart'; import 'package:vector_graphics/vector_graphics.dart'; void main() { runApp(MaterialApp( home: Scaffold( body: Center( child: VectorGraphic( loader: AssetBytesLoader('assets/file_compiled.vec'), width: 300, height: 300, ), ), ), )); }
- Run the app:
flutter run
- vector_graphics_compiler not found:
- Verify installation:
vector_graphics_compiler --help
. - Ensure
~/.pub-cache/bin
is in PATH. - Reinstall:
dart pub global activate vector_graphics_compiler
.
- Verify installation:
- PyQt5 issues:
- Reinstall:
pip install PyQt5
. - Ensure Python 3.6+.
- Reinstall:
- SVG processing errors:
- Check SVG validity (open in a browser or Inkscape).
- Ensure file permissions:
chmod +r file.svg
.
- PATH issues:
- Add
~/.pub-cache/bin
to PATH permanently (see Installation step 4).
- Add
Contributions are welcome! Please:
- Fork the repository.
- Create a feature branch.
- Submit a pull request with clear descriptions.