Convert an SVG file to raster images of multiple sizes, optionally splitting into background and foreground images
Project description
svg2many (C) Alexander Iurovetski 2025-2026
Convert an SVG file to raster images of multiple sizes, optionally splitting into background and foreground images
This application produces all raster images you need (.png, .webp, or others) from a single .svg file. Since the SVG format is text (XML), there is a rational way to find spots where the width, height, scale, offset and visibility should be substituted with the required ones.
Dependencies:
envara by the same author: a package for the comprehensive environment variable expansion on various platforms.
Usage
This example can be used to generate all launcher icons for a Flutter project. See also: How Do I Use It at the end of this document.
-
Install the
resvgutility (if you use text(s) in your SVG, you should list the font families in thestylesection or as an attribute). -
Install
cwebpif you plan to generate*.webpfiles forAndroidprojects or to run demo for theAndroidtarget -
Install
ImageMagickif you plan to generate an.icofile from (multiple).pngfile(s) or to run demo for theWindowstarget -
Install
svg2manyfrom PyPI. -
Create your beautiful SVG image. It is highly recommended NOT to use percentages as co-ordinates or sizes: those are scaled inconsistently. You can define a 100x100 pixel image, and all absolute co-ordinates and sizes will essentially express the same. Example:
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"> <g class="background"> <!--Place your actual background drawing here--> </g> <g class="foreground"> <!--Place your actual foreground drawing here--> </g> </svg>
Ideally, don't specify any attributes for these nodes as well as don't style them in a
.cssfile or under<style>. You can also look atexamples/assets/images/app_icon.svgin the application bundle or from the repo's examples -
Create configuration file or take that from the source mentioned above.
-
Create
envfile(s) or take those from the source mentioned above if you need. You can skip this step and decide later. -
Ensure the installation directory for
svg2manyis listed inPATH. -
Run:
svg2many -c CONFIG [-e ENV] [-d] [-q] [-v] [ARGUMENTS]
All options:
-
-h, --helpHelp screen.
-
-D, --demo DEMORun demonstration, DEMO is one of (case ignored): Android, Flutter (default), iOS, Linux, macOS, Web, Windows. If you run demo, the application will create directory
~/Projects/my_app. It will place the output images there (you can safely remove it later). Demo, and possibly, your future runs, requireresvgto be installed. Specifically for the Android demo,cwebpshould be installed too. And specifically for the Windows demo, installImageMagick(to convert multiple.pngs to a single.ico). Demo will read configuration files fromexamples/configas well as env files fromexamples/envunder the package installation directory. The sample input SVG file will be copied to the demo output directory too, underassets/images. -
-c, --config CONFIGA file in JSON format that holds rules of conversion. Supports comma-separated masks with wildcards to process multiple config files. Can also represent the whole actual JSON content (should start and end with the curly braces). If this setting is omitted, the application will expect configuration (just the content) in its
<stdin>. -
-d, --deleteDelete all generated files, but not directories.
-
-e, --env ENVDirectory containing env files. Env files are text files with lines in the
name = valueformat with any number of spaces around=. This format allows you to define extra environment variables to use in configuration files. If the non-empty beginning of the file content represents line comment (#for POSIX,::for Windows,!for OpenVMS,|for RiscOS), it defines that platform's rules for the environment variables' expansion. Please note that when running on Windows, the escape character will still be defined as^rather than\in order to avoid possible issues with paths. So, if you wish to use POSIX rules of expansion, do that in the env files that do not contain escaped characters (you can define those via variables in the platform-specific env files). The application will load the content from.env, then from files containingposixandenvin their name on POSIX-compatible OSes (Linux, BSD, macOS, AIX), then from files containingbsdandenvin their name (for FreeBSD, NetBSD, macOS, and similar), then from files containing<plat>andenvin their name (<plat> is what Python'splatform.system()call returns, as well as from files containingmacosandenvon macOS). Every pattern should appear in the beginning or in the end of the filename, or should be separated by.or-or_. For instance, on Windows, it could be,.env,windows.envas well as something exotic likeabc-env_def.windows--ghi. The last file to load will besvg2many.envif present. Please note that there was no way for the author to test the application in OpenVMS or RiscOS, so expect issues there. You are welcome to report those in the GitHub repository. -
-q, --quietDo not print any information to the console. Mutually exclusive with
-v, --verbose. -
-v, --verboseDetailed logging, primarily, to trace issues. Mutually exclusive with
-qor--quiet. -
ARGUMENTSAnything that you can refer to in the configuration file(s) like
$1,$2(POSIX) or%1,%2, etc. For example, it could represent the name of a project which will appear as part of a path:~/Projects/$1.
The SVG-to-raster-image conversion is done via an arbitrary external command defined in the config (export or after). You can use any rasterizer: resvg, ImageMagick, Inkscape, headless Chrome. The command receives the modified SVG content via pipe (stdin) or via a temporary file (if {i} is present in the command).
The preferred exporter is resvg (you can install that using apt, yum, brew or scoop), as it is faster than any headless browser or tools like ImageMagick or Inkscape, and produces images of good quality.
Another option for Linux is rsvg-convert. However, when run on Windows, it has issues with the text alignment. Also the project does not support the Windows executable, so you need to look for a third-party build.
Normally, you should create (or copy and customize) a proper configuration file (JSON) as well as relevant env files. However, you can try a purely inline example to convert an SVG to a 64x64 PNG for Linux, BSD/macOS or PowerShell:
svg2many -c '{"export":"resvg {i} {o}","inpFile":"your-inp-dir/your.svg","targets":[{"outDir":"your-out-dir","output":[{"size":"64","file":"{n}.png"}]}]}'
Same example for Windows batch file or Windows command prompt:
svg2many -c "{^"export^":^"resvg {i} {o}^",^"inpFile^":^"your-inp-dir\your.svg^",^"targets^":[{^"outDir^":^"your-out-dir^",^"output^":[{^"size^":^"64^",^"file^":^"{n}.png^"}]}]}"
But if you run in the PowerShell terminal, use the backtick as escape character rather than the caret.
JSON Configuration File Format
Nodes
-
$.after,$.targets.target.afterDefault command to run upon completion of all exports in a target (like
magickfor Windows icons). The latter overrides the former. -
$.export,$.targets.target.exportCommand to export an input SVG image to an output raster image file. The latter overrides the former. Might be useful to generate
webpicons forAndroidicons specifically. -
$.rootDirTop directory for the input and output files. Defaults to the directory containing the current configuration file, or the current working directory if that configuration is not read from a file.
-
$.inpFileInput SVG file. Read from
<stdin>if not specified, null, empty, or"-". -
$.bkgClass,$.targets.target.bkgClassCSS class to detect background. Default for the former:
"background". The latter overrides the former. -
$.frgClass,$.targets.target.frgClassCSS class to detect foreground. Default for the former:
"foreground". The latter overrides the former. -
$.outType,$.targets.target.outTypeThe output file type (no leading dot): png, webp, etc. The latter overrides the former.
-
$.targetsArray of rules for each output image file defined as follows:
-
$.targets.target.titlePlain text to log the progress.
-
$.targets.target.checkFile(s) to compare config and input timestamps to during incremental (non-forced) runs. Useful when
.pngfiles get deleted after some further processing produces different result(s) like.icofile. Supports wildcards. Examples:{O}/*.ico,{O}/{n}.ico({O} and {n} will be expanded as placeholders) -
$.targets.target.deleteIf
true, delete all files listed under the target (useful if, for instance, all images have been converted to a single.icofile). -
$.targets.target.outDirOutput sub-directory (relative to the top-level
rootDir) or an absolute path. -
$.targets.target.outputArray of the output dimensions and filenames (under
outDiror a path). -
$.targets.target.output.sizeDimensions in pixels of the background as a single number (square) or
WxH(rectangle). Optionally followed by dimensions of the foreground. Use space(s) to separate foreground size from the background one. -
$.targets.target.output.fileName of the output image file (sub-path relative to
outDir).
Placeholders
Do not wrap those in quotes unless necessary, and not for being "safe" with possible spaces inside:
-
{i}Input (SVG) file path. If this placeholder is not present in the export command, the scaled and/or shifted content of the original input file will be fed into the respective application via its
<stdin>. Otherwise, the scaled and/or shifted content will be saved to a temporary SVG file in the output directory, and that file's path will be inserted into the export command. -
{n}Input filename without type (extension). With multiple extensions, all of those will be stripped.
-
{o}Current output file path.
-
{t}Current output file type defined in
outType. -
{w}Current output image width: largest of two widths (background and foreground).
-
{h}Current output image height: largest of two heights (background and foreground).
-
{I}Input directory path.
-
{O}Output directory path.
Extra Expansions
References to the environment variables will be expanded:
- Portable:
$ABC,${ABC:-$DEF}. - Windows:
%ABC%.
References to the arguments passed to the script will also be expanded:
- Portable:
$1,${2:-abc}. - Windows:
%1,%2.
Good Luck!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file svg2many-0.8.0.tar.gz.
File metadata
- Download URL: svg2many-0.8.0.tar.gz
- Upload date:
- Size: 55.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b5dd7fbf712a3da3a2beb18dd43c9319692f010e339ff3824604291a95315df
|
|
| MD5 |
f86e0bcad3e7432df1fd34b6519357c6
|
|
| BLAKE2b-256 |
bdf655a10aaf409f93163ab0df1f376c99f34bc54eb8cfd244a0cb0145519dd7
|
File details
Details for the file svg2many-0.8.0-py3-none-any.whl.
File metadata
- Download URL: svg2many-0.8.0-py3-none-any.whl
- Upload date:
- Size: 57.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
620d920165fe97de134b6ff041cfc78a622643ebc1800cb675a32cf1c9e4714b
|
|
| MD5 |
355d79bf6c723ec6f7ae87ffde369182
|
|
| BLAKE2b-256 |
99462e113b4fdc85d8435c46b5697a3baba32d85803d0160340f58e9cf3112e3
|