21

I have these SVGS and I'd like to export them to PNG images, I could export them with Inkscape but that would mean open each file and export that file to PNG which is not efficient (I have hundreds of them).

How can I do this?

5 Answers 5

27

It appears you can use Inkscape from command line:

`#{INKSCAPE_PATH} -z -f #{source_svg} -w #{width} -j -e #{dest_png}`

more details

I imagine you can write a simple bash script to process all SVG files:

#!/bin/sh

for file in *.svg
do
     /usr/bin/inkscape -z -f "${file}" -w 640 -e "${file}.png"
done

the example above converts all .svg files in the current directory, adding .png extension to the output files.

6
  • I'm trying to export several hundreds of svgs, how can I set the export value (dest) so they keep their name? because this seems to work fine for a small quantity. Oct 30, 2012 at 4:33
  • @UriHerrera: I updated the answer
    – Sergey
    Oct 30, 2012 at 4:46
  • all the files are saved {file}.svg.png format instead of {file}.png? how to fix that? and also on the original SVG it appears little shortcut icon on the bottom right which disappear when it's converted to PNG (I'm trying to convert icons pack)
    – Tosho
    Dec 15, 2012 at 15:52
  • @Tosho Currently I'm on Windows, but it should be something like this: pastebin.com/TEDfvxPC
    – user31389
    Sep 23, 2013 at 12:05
  • 2
    @Tosho You can also do ${file%svg}png. You can read here for more possibilities.
    – jja
    May 12, 2015 at 15:22
24

Inspired by the previously accepted answer I came up with this one-liner:

For Inkscape version 0.92.4 and earlier:

for file in *.svg; do inkscape "$file" -e "${file%svg}png"; done

This way you don't need to call a script. If you wanted to, you could create an alias for converting all svgs in the current directory to pngs:

alias svgtopng='for file in *.svg; do inkscape "$file" -e "${file%svg}png"; done'

For Inkscape version 1.0 Beta and later:

for file in *.svg; do inkscape "$file" -o "${file%svg}png"; done

This way you don't need to call a script. If you wanted to, you could create an alias for converting all svgs in the current directory to pngs:

alias svgtopng='for file in *.svg; do inkscape "$file" -o "${file%svg}png"; done'
3
  • 1
    ${file%svg}png is a great trick! I had not seen that before.
    – Chester
    Jan 19, 2017 at 20:49
  • 1
    I think this doesn't work with spaces in file names. Jul 31, 2019 at 9:23
  • You can wrap $file and ${file%svg}png with double quotes. This will fix the "doesn't work with spaces in file names" problem.
    – sotirov
    Feb 20, 2023 at 20:57
5

Graphical Nautilus Script


Overview

The command line is great for batch conversions but sometimes you just don't want to leave the comfort of your GUI. That's why I coded a GUI-based Nautilus script to batch convert SVG files to PNG images. Other file managers with custom actions (e.g. Thunar) should be supported, too.

Screenshot

enter image description here

Script

#!/bin/bash

# NAME:         SVG2PNG
# VERSION:      0.1
# AUTHOR:       (c) 2014 Glutanimate (https://github.com/Glutanimate)
#
# DESCRIPTION:  Simple application to convert SVG files to PNG files based on DPI or resolution. 
#               Designed to work as a context menu script in file managers like Nautilus and Thunar.
#
# FEATURES:     - Converts SVG image file to PNG raster of a specific DPI or width
#               - SVG preview
#               - choose between converting the full SVG page or only the cropped image
#
# DEPENDENCIES: inkscape imagemagick yad
#               YAD (1) is an advanced for of Zenity with many improvements. It's not included in the
#               official Ubuntu repos yet (2) but can be installed from a webupd8 PPA (3)
#
# LICENSE:      MIT license (http://opensource.org/licenses/MIT)
#
# NOTICE:       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#               INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
#               PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
#               LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
#               TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
#               OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# USAGE:        SVG2PNG image1.svg image2.svg [..]
#               I recommend installing this script as a context menu action for your file manager.
#               Instructions for Nautilus may be found on AskUbuntu (4).
#
# NOTES:        The script uses convert for previews because it's faster. For optimal results
#               the actual conversion is done with inkscape's command line interface.
#
# LINKS:        (1) https://code.google.com/p/yad/
#               (2) https://bugs.launchpad.net/ubuntu/+bug/796633
#               (3) https://launchpad.net/~webupd8team/+archive/y-ppa-manager
#               (4) https://askubuntu.com/questions/236414/how-can-i-install-a-nautilus-script

############## DIALOGS ###################

TITLE="SVG to PNG"
ICON="svg"

############## USGCHECKS #################

# checks if user selected an item

if [ $# -eq 0 ]
  then
      yad --title="$TITLE" \
          --image=dialog-error \
          --window-icon=dialog-error \
          --class="$WMCLASS" \
          --text="Error: no file selected" \
          --button="Ok":0
      echo "Error: no file selected"
      exit
fi

############### GLOBVAR ##################

SVGFILES="$@"
TEMPDIR=$(mktemp -d)
PREVIEWIMG="$TEMPDIR/svgpreview.png"

############### CLEANUP ##################

trap "rm -r $TEMPDIR" EXIT 

############## FUNCTIONS #################

converttosvg_dpi(){

echo "Converting based on DPI."

while [ $# -gt 0 ]; do

    echo "$# file(s) left to convert."
    SVGFILE="$1"
    FILESTEM="${SVGFILE%%.*}"
    PNGFILE="$FILESTEM".png
    inkscape "$SVGFILE" -z --export-dpi="$DPI" \
    --"$AREASETTING" --export-png="$PNGFILE"
    shift

done
echo "Done."

}

converttosvg_res(){

echo "Converting based on Width."

while [ $# -gt 0 ]; do

    echo "$# file(s) left to convert."
    SVGFILE="$1"
    FILESTEM="${SVGFILE%%.*}"
    PNGFILE="$FILESTEM".png
    inkscape "$SVGFILE" -z --export-width="$WIDTH" \
    --"$AREASETTING" --export-png="$PNGFILE"
    shift

done
echo "Done."

}

createpreview() {
convert -resize 128x "$1" "$PREVIEWIMG"
}

getsettings() {

SETTINGS=$(yad --window-icon "$ICON" --image "$PREVIEWIMG" --width 300 --height 200 --always-print-result \
--form --separator="|" --title="$TITLE" --text "Please choose the DPI or resolution to convert to." \
--field="DPI:NUM" 10[!80..600[!10]] --field="Width in px:NUM" 16[!16..4096[!16]] \
--field="Area:":CB "Drawing"\!"Page" \
--button="Convert based on DPI:2" --button="Convert based on Resolution:3" --button="gtk-cancel:1")

RET=$? # Exit code?

if [ "$RET" = 252 ] || [ "$RET" = 1 ]  # WM-Close or "Abort"
  then
      echo "Exiting..."
      exit
fi

DPI=$(printf %.0f $(cut -d "|" -f 1 <<<"$SETTINGS")) #round values
WIDTH=$(printf %.0f $(cut -d "|" -f 2 <<<"$SETTINGS"))
AREA=$(cut -d "|" -f 3 <<<"$SETTINGS")

case "$AREA" in

Drawing)
  AREASETTING="export-area-drawing"
  ;;

Page)
  AREASETTING="export-area-page"
  ;;

esac

echo "DPI set to $DPI"
echo "Width set to $WIDTH"
echo "Area set to $AREA"

}


################ MAIN ####################

createpreview "$1"
getsettings

case "$RET" in

2)
  echo 2
  converttosvg_dpi "$@"
  ;;

3)
  echo 3
  converttosvg_res "$@"
  ;;

esac

exit 0

I will try to keep this answer updated but please check out my Github repository for the latest version of the script.

Installation

Generic installation instructions for all Nautilus scripts may be found here. The following commands should cover all the necessary dependencies:

sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install yad inkscape imagemagick

For further information please consult the script header above.

Usage

After installing the script you should be able to invoke it from your file manager's context menu. Simply select one or more SVG files and click on the appropriate entry in your context menu. A GUI dialog should come up with several options concering the conversion.

You can either convert the SVG based on DPI or width. The aspect ratio will be conserved in both cases. Make sure to supply your DPI or width of choice before clicking on the conversion buttons.

You can also choose between exporting the full SVG file or only the cropped drawing. If your SVG canvas has a lot of empty space it's advisable to choose "Drawing" as the export option.

2

Here's a slightly different alternative solution in a more readable scripting language - python. It can batch export all your svgs. Particularly ideal if you're doing Android dev and have to make multiple pngs from a single svg.

Disclaimer: I wrote the lib. Hope it helps someone.

Click here.

For a simple use, download the library into a folder, put the svgs in the same folder, then run

python exporter.py

in the command line/terminal after you cd to the folder. For more advanced options, do check out the README.

1
  • Thanks David, I have edited my answer to provide more details!
    – Kevin Lee
    Mar 28, 2016 at 14:16
1

If not all files, but only certain SVG files need to be converted to PNG, one might use sed to automatically generate the file names:

inkscape --without-gui --export-width=1280 --export-png=`echo $1 |sed -e 's/svg$/png/'` $1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .