Skip to content

Instantly share code, notes, and snippets.

@matsen
Created December 12, 2012 01:05
Show Gist options
  • Save matsen/4263955 to your computer and use it in GitHub Desktop.
Save matsen/4263955 to your computer and use it in GitHub Desktop.
A script to convert SVG to a TIFF acceptable to PLOS
#!/bin/sh
# Convert all arguments (assumed SVG) to a TIFF acceptable to PLOS
# Requires Inkscape and ImageMagick 6.8 (doesn't work with 6.6.9)
for i in $@; do
BN=$(basename $i .svg)
inkscape --without-gui --export-png="$BN.png" --export-dpi 300 $i
convert -compress LZW -alpha remove $BN.png $BN.tiff
mogrify -alpha off $BN.tiff
rm $BN.png
done
@asoplata
Copy link

asoplata commented Jan 6, 2017

This is EXACTLY what I needed! Thank you, you rock!

@cossio
Copy link

cossio commented Nov 1, 2017

Thanks! Very useful

@lczech
Copy link

lczech commented Jun 25, 2018

Perfect, Erick! I still do not understand why PLOS wants tiffs, but your script helps a lot!

@mirix
Copy link

mirix commented Nov 3, 2019

My version. This autocrops the image (-trim) with convert and sets a fixed width (-geometry) with mogrify while keeping the aspect ratio.

#!/bin/bash

for i in $(ls *.svg | cut -d. -f1); do
  inkscape --without-gui --export-png="${i}.png" --export-dpi 300 --export-area-drawing ${i}.svg
  convert -compress LZW -alpha remove -trim ${i}.png ${i}.tiff
  mogrify -alpha off -geometry 1000x ${i}.tiff
  rm ${i}.png
done

@twesleyb
Copy link

Still works in 2020!

@MarcoSorbona
Copy link

I need to save my figures in TIFF for publication, this script seems great! However, I'm pretty new to Inkscape and programming, could you please show me how to implement the script?

Thanks!
Marco

@twesleyb
Copy link

twesleyb commented Jun 2, 2020

This is how I use it:

svg2tiff.sh

#!/bin/sh
# Convert all arguments (assumed to be SVG) to a TIFF.
# Requires Inkscape and ImageMagick 6.8 (doesn't work with 6.6.9).
# From matsen: https://gist.github.com/matsen/4263955

for i in $@; do
  BN=$(basename $i .svg)
  inkscape --without-gui --export-png="$BN.png" --export-dpi 300 $i
  convert -compress LZW -alpha remove $BN.png $BN.tiff
  mogrify -alpha off $BN.tiff
  rm $BN.png
done

Download a svg image and convert it:

wget https://svgsilh.com/svg_v2/2442125.svg
svg2tiff 2442125.svg

@AnthonyMobbs
Copy link

Sometimes you need to alter the size of the image to meet PLOS requirements. The following script requires two argument. $1 = name of svg file, $2 = number of pixels wide. Also note that this script generates 600 dpi image for high resolution. PLOS accepts images in range 300 - 600 dpi.

svg2tiff abc pixels

#!/bin/bash
inkscape -o "$1.png" --export-width $2 --export-area-drawing $1.svg
convert -compress LZW -units PixelsPerInch -density 600 -alpha remove -trim $1.png $1.tif
mogrify -alpha off $1.tif
rm $1.png

@fayjustin
Copy link

To export only the drawing area, but pad it with 50 points of whitespace regardless of size replace mogrify command with:
mogrify -alpha off -gravity center -background white -extent $(identify -format '%[fx:W+50]x%[fx:H+50]' $BN.tiff) $BN.tiff

@ricmedveterinario
Copy link

I'm in 2022 and it still works. Thanks to the creators of these little scripts.

@ben-heil
Copy link

Works great! Thanks @matsen

@AliASafdari
Copy link

Thank you @matsen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment