Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
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. –  Uri Herrera Oct 30 '12 at 4:33
    
@UriHerrera: I updated the answer –  Sergey Oct 30 '12 at 4:46
    
Yeah, that's great Thanks. –  Uri Herrera Oct 30 '12 at 5:34
    
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 '12 at 15:52
    
@Tosho Currently I'm on Windows, but it should be something like this: pastebin.com/TEDfvxPC –  user31389 Sep 23 '13 at 12:05
add comment

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) http://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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.