Batch processing svg to eps/pdf/png

Share your Inkscape tricks and tutorials here.
User avatar
Johannski
Posts: 63
Joined: Wed May 05, 2010 3:13 am
Location: Austria
Contact:

Batch processing svg to eps/pdf/png

Postby Johannski » Tue Jan 21, 2014 12:08 pm

Hello there!

I made a small shell script, to convert all files in a directory with the extensions svg, eps, pdf to the file types pdf, eps, png.

Linux + Inkscape necessary
1. put the script in the same folder where the files are
2. open up a terminal (Strg + Shift + T)
3. cd to the same directory where the script is
4. write "sh InkscapeBatchConverter.sh"
5. follow the instructions

Here's the code of the script:

Code: Select all

#!/bin/sh
count=0
validInput1="svg"
validInput2="pdf"
validInput3="eps"
validOutput1="eps"
validOutput2="pdf"
validOutput3="png"

echo "This script allows you to convert all files in this folder from one file type to another."

valid=0
echo "Allowed file types for source: $validInput1, $validInput2, $validInput3"
while [ "$valid" != "1" ]
do
    read -p "What file type do you want to use as a source? " sourceType
    if [ "$sourceType" = "$validInput1" ] || [ "$sourceType" = "$validInput2" ] || [ "$sourceType" = "$validInput3" ]; then
        valid=1
    else
        echo "Invalid input! Please use one of the following: $validInput1, $validInput2, $validInput3"
    fi
done

valid=0
echo "Allowed file types for output: $validOutput1, $validOutput2, $validOutput3"
while [ "$valid" != "1" ]
do
    read -p "What file type do you want to convert to? " outputType
    if [ "$outputType" = "$validOutput1" ] || [ "$outputType" = "$validOutput2" ] || [ "$outputType" = "$validOutput3" ]; then
        valid=1
    else
        echo "Invalid input! Please use one of the following: $validOutput1, $validOutput2, $validOutput3"
    fi
done

read -p "With what dpi should it be exported (e.g. 300)? " dpi

for fileSource in *.$sourceType
do
    if [ -f "$fileSource" ]; then   
        count=$((count+1))
        file=$(echo $fileSource | cut -d'.' -f1)
        echo $count". "$fileSource" -> "$file.$outputType
        inkscape $fileSource --export-$outputType=$file.$outputType --export-dpi=$dpi
    else
        echo "no file $fileSource found!"
    fi
done
echo "$count file(s) converted!"



You can download the file here: http://ge.tt/7C8JFmF1/v/0?c
I hope this helps!
€dit: Windows script will come in the next days. ;)
Last edited by Johannski on Wed Jan 22, 2014 2:32 am, edited 2 times in total.
Image

User avatar
ragstian
Posts: 1154
Joined: Thu Oct 11, 2012 2:44 am
Location: Stavanger-Norway

Re: Batch processing svg to eps/pdf/png

Postby ragstian » Wed Jan 22, 2014 2:02 am

Hi.

Great script, nice to see the command line use of Inkscape.
What about a DOS/Windows batch file as well?

Recon there is one small "adjustment" to be made to your code;
The last read statement should be;

Code: Select all

read -p "What file type do you want to use as output? " outputType


I've been bitten by the "Copy_N_Paste" bug a few times as well!

RGDS
Ragnar
Good Luck!
( ͡° ͜ʖ ͡°)
RGDS
Ragnar

User avatar
Johannski
Posts: 63
Joined: Wed May 05, 2010 3:13 am
Location: Austria
Contact:

Re: Batch processing svg to eps/pdf/png

Postby Johannski » Wed Jan 22, 2014 2:27 am

Hey Ragstian!

Thanks for your reply! I'll try to create a bat file as well, there are probably more windows users than linux/mac users in this forum.
Oh yeah, thanks for the hint! I expanded the script, so that it checks for valid input. I haven't used script shells that much, so I'm not sure, if its the best way to check, but it works. New file and code can be found in the first post :)
Image

User avatar
Johannski
Posts: 63
Joined: Wed May 05, 2010 3:13 am
Location: Austria
Contact:

Re: Batch processing svg to eps/pdf/png

Postby Johannski » Tue Feb 18, 2014 6:21 am

I tried to get it also working for windows, sadly I don't know how to use the inkscape command. Maybe somebody has an idea, everything else should work. Here is the code:

Code: Select all

@Echo off

set /a count=0
set validInput1=svg
set validInput2=pdf
set validInput3=eps
set validOutput1=eps
set validOutput2=pdf
set validOutput3=png

echo This script allows you to convert all files in this folder from one file type to another.

set valid=0
echo Allowed file types for source: %validInput1%, %validInput2%, %validInput3%

:whileInNotCorrect
    set /p sourceType=What file type do you want to use as a source?
    if "%sourceType%" EQU "%validInput1%" set valid=1
   if "%sourceType%" EQU "%validInput2%" set valid=1
   if "%sourceType%" EQU "%validInput3%" set valid=1
   if %valid% EQU 0 (
        echo Invalid input! Please use one of the following: %validInput1%, %validInput2%, %validInput3%
      goto :whileInNotCorrect
      )

set valid=0
echo Allowed file types for output: %validOutput1%, %validOutput2%, %validOutput3%      
:whileOutNotCorrect
    set /p outputType=What file type do you want to convert to?
    if "%outputType%" EQU "%validOutput1%" set valid=1
   if "%outputType%" EQU "%validOutput2%" set valid=1
   if "%outputType%" EQU "%validOutput3%" set valid=1
   if %valid% EQU 0 (
        echo Invalid input! Please use one of the following: %validOutput1%, %validOutput2%, %validOutput3%   
      goto :whileOutNotCorrect
      )
      
set /p dpi=With what dpi should it be exported (e.g. 300)?

for %%i in (.\*.%sourceType%) do (
   set /a count=count+1
   echo !count!.  %%i - !count!.%outputType%
   inkscape %fileSource% --export-%outputType%=!count!.%outputType% --export-dpi=%dpi%
   )
   
echo !count! file(s) converted!
   
pause


Cheers :)
Image

alexcotoranu
Posts: 1
Joined: Thu Jun 19, 2014 4:49 am

Re: Batch processing svg to eps/pdf/png

Postby alexcotoranu » Thu Jun 19, 2014 5:06 am

Hi,

The script works marvelously in windows (using git-bash).

In order for windows to globally recognize "inkscape" as a command you need to add the path to inkscape (i.e. C:\Program Files (x86)\Inkscape\ ) to your system path under "environment variables" in the "advanced system settings" window (right-click onto My Computer / Computer and select properties).

Cheers,

cullam
Posts: 1
Joined: Fri Sep 12, 2014 3:26 am

Re: Batch processing svg to eps/pdf/png

Postby cullam » Fri Sep 12, 2014 3:30 am

Hey guys. I'm using Inkscape on Windows, and am still having trouble with this script. I've added my inkscape directory to my system path, but when running the script, I still get an output saying "'inkscape' is not rocognized as an internal or external command, operable program or batch file."
Am I missing something? I'm running this straight from the cmd window.

puiutz777
Posts: 1
Joined: Fri Nov 21, 2014 4:51 am

Re: Batch processing svg to eps/pdf/png

Postby puiutz777 » Fri Nov 21, 2014 4:56 am

Thanks a lot. The script works perfectly, it is a great help. Also works in Windows with bash. If you don't really want to create Inkscape as a command, you can add in the script, the path in your computer where Inkscape is located. For example, instead of inkscape.exe will be: "C:\Program Files (x86)\Inkscape\inkscape.exe".
Wish you all best!
Thanks again!

DerSiedler
Posts: 1
Joined: Wed Mar 23, 2016 2:55 am

Re: Batch processing svg to eps/pdf/png

Postby DerSiedler » Wed Mar 23, 2016 3:01 am

Hi all,

I can't get it to work. The scripts runs in Git Bash on Windows 10 with the latest Inkscape version 0.91. But I only get "no file *.svg found!"

Any ideas?

Regards
DerSiedler

Marcos Valle
Posts: 1
Joined: Wed Mar 08, 2017 6:01 am

Re: Batch processing svg to eps/pdf/png

Postby Marcos Valle » Wed Mar 08, 2017 6:12 am

After looking all over, including this post, I've found a simple way using only one command line to export multiple files in a folder.

I've never used command line before, but after some experimentation i've came with the following solution:

Code: Select all

    for /f "tokens=1* delims=." %i in ('dir /b *.svg') do "C:\Program Files\Inkscape\inkscape.exe" --without-gui --file="%i.svg" --export-emf="%i.emf"

Breaking it down:

Code: Select all

    for
for is a command to perform an action to multiple files. It's structure is something like: for "files in a certain condition" do "especific command"

Code: Select all

    /f "tokens=1* delims=." %i in ('dir /b *.svg')
This is the "condition". In my case, I'm looking for all SVG files, but of course it works with others formats. All this "fancy" code does is store the file name, before the extention, in the variable "%i". This will be usefull in the comand.

Code: Select all

    do
That is what you will perform in all the files.

Code: Select all

    "C:\Program Files\Inkscape\inkscape.exe" --without-gui --file="%i.svg" --export-emf="%i.emf"
This is will call the program and open the file by file and export then. PS: I'm a newbie, i don't know how you use just "inkscape", so i've putted all the way to the .exe.

The last part is where we use the variable to instruct the program to wich file it should pick and how export.

Again, I'm exporting SVG to EMF, but just change it to what you want. Here is why the variable are so important. So you can keep the same name, just changing the extention.

Don't forget to use the quotes, since your files can have some space in it's name.

Of course you also need to be in the folder where your files are. If your are also a newbie do this:

Code: Select all

    C:\

It will put you in the correct drive. Chage it to the drive where your files are.

Code: Select all

    CD "path"

It will take you to the specific folder. You can use the windows explorer and click with right button and copy the path as text.

That's it. I'm newbie, soo it might be someway more efective to do it, but this will work.

Last tip: If you are working with a lot of files, you can use CTRL + C to stop the command line if it's not working how it should, and try again.


Return to “Tricks & Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests