Skip to main content

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.

  1. Install the resvg utility (if you use text(s) in your SVG, you should list the font families in the style section or as an attribute).

  2. Install cwebp if you plan to generate *.webp files for Android projects or to run demo for the Android target

  3. Install ImageMagick if you plan to generate an .ico file from (multiple) .png file(s) or to run demo for the Windows target

  4. Install svg2many from PyPI.

  5. 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 .css file or under <style>. You can also look at examples/assets/images/app_icon.svg in the application bundle or from the repo's examples

  6. Create configuration file or take that from the source mentioned above.

  7. Create env file(s) or take those from the source mentioned above if you need. You can skip this step and decide later.

  8. Ensure the installation directory for svg2many is listed in PATH.

  9. Run:

    svg2many -c CONFIG [-e ENV] [-d] [-q] [-v] [ARGUMENTS]
    

All options:

  • -h, --help

    Help screen.

  • -D, --demo DEMO

    Run 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, require resvg to be installed. Specifically for the Android demo, cwebp should be installed too. And specifically for the Windows demo, install ImageMagick (to convert multiple .pngs to a single .ico). Demo will read configuration files from examples/config as well as env files from examples/env under the package installation directory. The sample input SVG file will be copied to the demo output directory too, under assets/images.

  • -c, --config CONFIG

    A 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, --delete

    Delete all generated files, but not directories.

  • -e, --env ENV

    Directory containing env files. Env files are text files with lines in the name = value format 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 containing posix and env in their name on POSIX-compatible OSes (Linux, BSD, macOS, AIX), then from files containing bsd and env in their name (for FreeBSD, NetBSD, macOS, and similar), then from files containing <plat> and env in their name (<plat> is what Python's platform.system() call returns, as well as from files containing macos and env on 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.env as well as something exotic like abc-env_def.windows--ghi. The last file to load will be svg2many.env if 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, --quiet

    Do not print any information to the console. Mutually exclusive with -v, --verbose.

  • -v, --verbose

    Detailed logging, primarily, to trace issues. Mutually exclusive with -q or --quiet.

  • ARGUMENTS

    Anything 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.after

    Default command to run upon completion of all exports in a target (like magick for Windows icons). The latter overrides the former.

  • $.export, $.targets.target.export

    Command to export an input SVG image to an output raster image file. The latter overrides the former. Might be useful to generate webp icons for Android icons specifically.

  • $.rootDir

    Top 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.

  • $.inpFile

    Input SVG file. Read from <stdin> if not specified, null, empty, or "-".

  • $.bkgClass, $.targets.target.bkgClass

    CSS class to detect background. Default for the former: "background". The latter overrides the former.

  • $.frgClass, $.targets.target.frgClass

    CSS class to detect foreground. Default for the former: "foreground". The latter overrides the former.

  • $.outType, $.targets.target.outType

    The output file type (no leading dot): png, webp, etc. The latter overrides the former.

  • $.targets

    Array of rules for each output image file defined as follows:

  • $.targets.target.title

    Plain text to log the progress.

  • $.targets.target.check

    File(s) to compare config and input timestamps to during incremental (non-forced) runs. Useful when .png files get deleted after some further processing produces different result(s) like .ico file. Supports wildcards. Examples: {O}/*.ico, {O}/{n}.ico ({O} and {n} will be expanded as placeholders)

  • $.targets.target.delete

    If true, delete all files listed under the target (useful if, for instance, all images have been converted to a single .ico file).

  • $.targets.target.outDir

    Output sub-directory (relative to the top-level rootDir) or an absolute path.

  • $.targets.target.output

    Array of the output dimensions and filenames (under outDir or a path).

  • $.targets.target.output.size

    Dimensions 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.file

    Name 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svg2many-0.8.0.tar.gz (55.3 kB view details)

Uploaded Source

Built Distribution

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

svg2many-0.8.0-py3-none-any.whl (57.4 kB view details)

Uploaded Python 3

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

Hashes for svg2many-0.8.0.tar.gz
Algorithm Hash digest
SHA256 5b5dd7fbf712a3da3a2beb18dd43c9319692f010e339ff3824604291a95315df
MD5 f86e0bcad3e7432df1fd34b6519357c6
BLAKE2b-256 bdf655a10aaf409f93163ab0df1f376c99f34bc54eb8cfd244a0cb0145519dd7

See more details on using hashes here.

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

Hashes for svg2many-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 620d920165fe97de134b6ff041cfc78a622643ebc1800cb675a32cf1c9e4714b
MD5 355d79bf6c723ec6f7ae87ffde369182
BLAKE2b-256 99462e113b4fdc85d8435c46b5697a3baba32d85803d0160340f58e9cf3112e3

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