Skip to content

Instantly share code, notes, and snippets.

@JohannesDeml
Last active May 17, 2024 23:09
Show Gist options
  • Save JohannesDeml/779b29128cdd7f216ab5000466404f11 to your computer and use it in GitHub Desktop.
Save JohannesDeml/779b29128cdd7f216ab5000466404f11 to your computer and use it in GitHub Desktop.
Batch convert images with inkscape on windows

Batch convert svg|pdf|eps|emf|wmf|ai|ps|cdr to eps|pdf|png|jpg|tiff|svg|ps|emf|wmf

Screenshot Batch converter for Windows using Inkscape with the command line
InkscapeBatchConvert is an easy to use solution to quickly convert all files of a folder to another type without the need to open Inkscape. The program uses Windows Batch scripting and will only work on Windows.
Tested with Inkscape 1.0.x - 1.3.x ✅ (The last version that supports Inkscape 0.9.x can be found here)

Usage

  1. Download _InkscapeBatchConvert.bat
  2. Put it in the folder where you have files you wish to convert (will also scan on all subfolders for files of input type).
  3. Then double click the file to start it.

Troubleshooting

Problem Solution
Windows blocks the program Right click the file → Properties → Unblock
Windows protected your PC Disable blocking
I can't convert EPS / PDF files. The program says it runs the conversion, but there are no files in the out folder. Make sure you have ghost script installed and added to your environment path. For more information read the Inkscape FAQ
Add ghostscript to path
Can't find Inkscape installation All common file paths are checked for the installation. If you haven't installed Inkscape download it here, otherwise you can also define your own path in the script by adding the next array element of inkscapePaths to the script
Can't find inkscape installation custom inkscape path
I'm missing a setting in the script, e.g. defined output width Have a look at the Commandline Documentation or read the inkscape help file inkscape --help and add the needed properties to the script
Inkscape help output Script change position
I'm missing a specific input or output type If inkscape supports those types, you can easily add them to the list in the script (search for validInput and validOutput)
Add input / output type
The files are saved to out\, but I would like to have it saved in the same directory or a subdirectory with a different name Just change the variable relativeOutputFolder - If you want to save the files to the same directory, set it to nothing (set relativeOutputFolder=)
Set subdirectory

Todo

Use shell or pipe to avoid starting inkscape for every file as pointed out by @vaifrax here. I would be very happy to get help for that 🙂

Credits

:: From https://gist.github.com/JohannesDeml/779b29128cdd7f216ab5000466404f11
:: Convert vector file formats in the current folder and their subsfolders
:: The variables in the beginning can be changed to your needs
:: Tested for Inkscape 1.0 - 1.3
@echo off
setlocal ENABLEDELAYEDEXPANSION
:: Define supported input file extensions
set validInput=svg pdf eps emf wmf ps ai cdr
:: Define supported output file extensions
:: All supported extnesions: [.dxf, .emf, .eps, .fxg, .gpl, .hpgl, .html, .jpg, .odg, .pdf, .png, .pov, .ps, .sif, .svg, .svgz, .tar, .tex, .tiff, .webp, .wmf, .xaml, .zip]
set validOutput=svg png tiff jpg webp pdf eps emf wmf ps cdr
:: Define relative output folder, if you want to write to the same folder set the var to an empty string
set relativeOutputFolder=out\
:: Possible paths to check for the installation
set "inkscapePaths[1]=C:\Program Files\Inkscape\bin\inkscape.com"
set "inkscapePaths[2]=C:\Program Files\Inkscape\bin\inkscape.exe"
set "inkscapePaths[3]=C:\Program Files\Inkscape\inkscape.com"
set "inkscapePaths[4]=C:\Program Files (x86)\Inkscape\bin\inkscape.com"
set "inkscapePaths[5]=C:\Program Files (x86)\Inkscape\bin\inkscape.exe"
set "inkscapePaths[6]=C:\Program Files (x86)\Inkscape\inkscape.com"
set "inkscapePaths[7]=%UserProfile%\scoop\apps\inkscape\current\bin\inkscape.exe"
set "inkscapePath="
set /a "pathIndex=1"
:: Find the inkscape installation path
:inkscapepath_loop_start
if defined inkscapePaths[%pathIndex%] (
set "currentPath=!inkscapePaths[%pathIndex%]!"
if exist "!currentPath!" (
:: found installation path
set "inkscapePath=!currentPath!"
goto :inkscapepath_loop_end
)
set /a "pathIndex+=1"
goto :inkscapepath_loop_start
) else (
echo Can't find Inkscape installation, aborting.
goto end
)
:inkscapepath_loop_end
:: Set the path to allow for spaces without any needed additional quotes
set inkscapePath="%inkscapePath%"
set validInputString=
(for %%a in (%validInput%) do (
set validInputString=!validInputString!, %%a
))
:: Remove the leading comma
set validInputString=%validInputString:~2%
set validOutputString=
(for %%a in (%validOutput%) do (
set validOutputString=!validOutputString!, %%a
))
:: Remove the leading comma
set validOutputString=%validOutputString:~2%
:: If this can't be run, then the version is an older one
FOR /F "tokens=* USEBACKQ" %%g IN (`%inkscapePath% --version`) do (SET "inkscapeVersion=%%g")
if "%inkscapeVersion%" EQU "" (
set inkscapeVersion=0.x
set /a inkscapeMajorVersion=0
) else (
set /a inkscapeMajorVersion=%inkscapeVersion:~9,1%
)
echo.
echo This script allows you to convert all files in this folder from one file type to another
echo Running with %inkscapeVersion% from %inkscapePath%
echo (type q to quit at any question)
echo.
set valid=0
echo Allowed file types for source: %validInputString%
:whileInNotCorrect
set /p sourceType=What file type do you want to use as a source?
if "%sourceType%" EQU "q" exit /b
for %%a in (%validInput%) do (
if "%%a"=="%sourceType%" (
set valid=1
goto inSet
)
)
if %valid% EQU 0 (
echo Invalid input! Please use one of the following: %validInputString%
goto :whileInNotCorrect
)
:inSet
echo.
set valid=0
echo Allowed file types for output: %validOutputString%
:whileOutNotCorrect
set /p outputType=What file type do you want to convert to?
if "%outputType%" EQU "q" exit /b
for %%a in (%validOutput%) do (
if "%%a"=="%outputType%" (
set valid=1
goto outSet
)
)
if %valid% EQU 0 (
echo Invalid input! Please use one of the following: %validOutputString%
goto :whileOutNotCorrect
)
:outSet
if "%outputType%" EQU "%sourceType%" (
echo Input and Output are the same, no point in doing anything. Exiting...
exit /b
)
echo.
:: Set DPI for exported file
:whileNotValidDpiNumber
set /p dpi=With what dpi should it be exported (e.g. 300)?
if "%dpi%" EQU "q" exit /b
IF %dpi% NEQ +%dpi% (
echo Invalid input! Please input an actual number.
goto :whilenotValidDpiNumber
)
echo.
:: count how many files we need to convert before converting!
set /a total=0
for /R %%i in (*.%sourceType%) do ( set /a total=total+1 )
echo Found %total% file(s) of type *.%sourceType% in the current folder (%~dp0)
echo.
:: Create output folder if it is set and does not exist
if not "!relativeOutputFolder!"=="" (
if not exist "!relativeOutputFolder!" mkdir "!relativeOutputFolder!"
)
set /a count=0
:: Running through all files found with the defined ending
if %inkscapeMajorVersion% NEQ 0 (
:: Inkscape 1.0 and newer
for /R %%i in (*.%sourceType%) do (
set /a count=count+1
echo %%i -^> %%~di%%~pi!relativeOutputFolder!%%~ni.%outputType% ^[!count!/%total%^]
%inkscapePath% --export-filename="%%~di%%~pi!relativeOutputFolder!%%~ni.%outputType%" --export-dpi=%dpi% "%%i"
)
)
echo.
echo %count% file(s) converted from %sourceType% to %outputType%! (Saved in out folder)
echo.
:end
pause
@JohannesDeml
Copy link
Author

Thanks, this works but is slow.
There's also the --shell option. In a script you could either build a text file containing the commands and pass the file as the argument (inkscape --shell all-commands.txt) or you can pipe to inkscape. This avoids starting inkscape every time and greatly speeds up the whole process.

How would you do that?

Hi @turbogauchiste,

that's a bit different to build up, I've never done it, so I didn't feel like the first question of yours was directed at me. Anyhow, if you need that extra performance, take a look at https://wiki.inkscape.org/wiki/index.php/Using_the_Command_Line#Shell_mode, there is the documentation of how to use the shell (batch) mode.

@Elmstrom
Copy link

Elmstrom commented Jun 3, 2021

This is awesome! thanks a lot for sharing.

@adamovichg
Copy link

I had to upgrade to Inkscape 1.1 for this to work. Was previously working in Inkscape 0.92.4. Don't know, perhaps this is obvious, but it's good to know :)

@JohannesDeml
Copy link
Author

I had to upgrade to Inkscape 1.1 for this to work. Was previously working in Inkscape 0.92.4. Don't know, perhaps this is obvious, but it's good to know :)

Interesting, thanks for letting me know! I tried to keep backwards compatibility, but I guess I broke something along the way. I hope, I'll find the time to do a 0.9.x setup to see whats wrong at some point :)

@TimoRJensen
Copy link

Awesome stuff! Thanks a lot for sharing! Had to update too (from 0.9 to 1.1), but that did the trick. Before that I had errors like invalid argument --export-filename... (can't recall exactly now.)

@JohannesDeml
Copy link
Author

@TimoRJensen, thank you also for your note. I found the time to fix the logic for 0.x versions (the problem was, that the version call does not properly work for 0.x and then the script used the logic for 1.x for 0.x). Should be all fixed now and is now compatible for all versions again :)

@loganpowell
Copy link

Just found this!! Is there a way to run the batch over svg -> optimized svg?

@MaydayGD
Copy link

I have like bazzilion SVGs I need to convert/export to PNG, and I Googled "How to convert SVG to PNG with ImageMagick" and I could not find one tutorial for idiots anywhere. All put some line of the code as if we knew what to do. I LOVE you placed everything in one place and all I have to do is copy paste in notepad, save as bat. in folder with SVG, create OUT folder and click run. And it is asking me all the good questions. Thank you. Please continue to share simpelton codes.

@JohannesDeml
Copy link
Author

@MaydayGD Awesome, thanks for the kind words. I'm happy to UX meets your needs :)

@fwitte
Copy link

fwitte commented Nov 24, 2021

Hi, very useful, thank you! I often generated .svg plots with something like matplotlib and need to embed those into ms powerpoint or ms word. Therefore conversion from .svg to .wmf might be useful as well. I added the functionality in my fork of the gist, it works for .svg to .wmf conversion, I however did not test the other file types.

@umarmansoor
Copy link

Awesome. It worked for me and converted my 122 svg files to PNG. It is a bit slow but no problem because it is just running in the background.

@MaydayGD
Copy link

Hi, I have 2 questions, it was mentioned above as resolved but to me, it is still happening, latest Windows update and latest Inkscape, the folder name must not have space in it (this is easy, I just put the folder on the desktop and name it with a single word) and when running action Inkscape keeps popping up and opening on the main monitor. This was mentioned as reported and being resolved. I can't seem to find a script where this is fixed? Thank you.

@JohannesDeml
Copy link
Author

Hi @MaydayGD,
I can't quite reproduce your problem with spaces. I tried both folder and file with spaces, but have no problems in executing the program:

This script allows you to convert all files in this folder from one file type to another
Running with Inkscape 1.1.2 (b8e25be833, 2022-02-05) from "C:\Program Files\Inkscape\bin\inkscape.com"
(type q to quit at any question)

Allowed file types for source: svg, pdf, eps, emf, wmf
What file type do you want to use as a source? svg

Allowed file types for output: eps, pdf, png, svg
What file type do you want to convert to? pdf

With what dpi should it be exported (e.g. 300)? 300

Conversion started. Will do 1 file(s).

C:\Data\Documents\Gists\inkscape-batch-convert\Folder with Spaces\File with spaces.svg -> C:\Data\Documents\Gists\inkscape-batch-convert\Folder with Spaces\out\File with spaces.pdf [1/1]

1 file(s) converted from svg to pdf (Saved in out folder)

I also don't have the problem with seeing inkscape open all the time. When did you download the script? Maybe you just don't have the latest version of it (maybe just try to download it again)?

@MaydayGD
Copy link

Hi @JohannesDeml , I had a version from Nov 13, 2021, and once agaîn you saved the day, I copied ˛the version that is here today and it works with ˛folders with spaces, also Inks˘c¸ape window is not opening äny more, the previˇoüs versions gave me eřrör if folder had space in name, and Inkscape was constant^l¨y opening on main monitor.

˙The only thing ˘iś if I type while it is running I get funny text sign˘s¨(as presented, I am typing as t˝he¸script is runninˇg).

T˝hank you for the update!!

@JohannesDeml
Copy link
Author

@MaydayGD Hahaha, wow! I was already wondering what was going on with your text. Haven't heard of that problem so far, but I also have no idea on how to fix this 😄
Glad the other issues are resolved for you!

@Jawel-cloud
Copy link

Jawel-cloud commented Feb 22, 2022

Hi, Thank you for this tool :)
In some files, My png output file is cut in half
maze

@MaydayGD
Copy link

@mysticJawel Tool exports what is on the artboard. So if your maze is not on the artboard it will export what you see. Where did you get mazes from? When you open SVG file in Inkscape are mazes on the page or over the page.

@shaye24
Copy link

shaye24 commented Jul 20, 2022

Hi @JohannesDeml,

Firstly, thank you for your batch script. After following the details of @MaydayGD (thank you too!), I managed to run the batch script, was asked the 3 questions, but it always says 0 files are saved. Any further advice would be much appreciated!
Screen Shot 2022-07-20 at 11 45 52 AM
Screen Shot 2022-07-20 at 11 46 05 AM

@shaye24
Copy link

shaye24 commented Jul 20, 2022

I changed the file "kind" to SVG, but still the same result.
I also tried renaming the OUT folder to "out". No effect either.

@JohannesDeml
Copy link
Author

JohannesDeml commented Jul 20, 2022

@shaye24 I tested it with the same version you used, and for me it does not have any problems.
image

My idea would be: Maybe there is a problem with the path, could you try to just run this under C:\Test\ or something similar (so you won't have too long paths and no special characters such as spaces).

@shaye24
Copy link

shaye24 commented Jul 21, 2022

Hi @JohannesDeml,

Thank you for your suggestion to simplify the path, I tried that and it still doesn't work. I will keep trying!

Screen Shot 2022-07-21 at 2 41 32 PM

@JohannesDeml
Copy link
Author

Hi @shaye24 ,

Ah I see, maybe there is a difference when running the script in Parallels? I updated the version a bit to output the actual folder where it is searching for files
cmd_UV7dh39ZQj
Maybe that helps you debugging the problem?

@shaye24
Copy link

shaye24 commented Jul 23, 2022

Hi @JohannesDeml,

Thank you once again for trying to help, still 0 files with the folder change. I think I might have to ask a friend to try it on a proper PC!

Screen Shot 2022-07-23 at 1 29 10 PM

@shaye24
Copy link

shaye24 commented Jul 25, 2022

Hi @JohannesDeml,

Just to update that I enlisted some help and my friend didn't have any problems running your batch file on his PC, thanks once again!!

@machen1929
Copy link

How could I modify the script so that Inkscape will use the Poppler/Cairo Import instead of embedding fonts?

@JohannesDeml
Copy link
Author

@machen1929 from the documentation, I would suggest having a look at --pdf-poppler, see https://wiki.inkscape.org/wiki/index.php/Using_the_Command_Line#New_options

Add this to line 147 like so %inkscapePath% --export-filename="%%~di%%~piout\%%~ni.%outputType%" --pdf-poppler --export-dpi=%dpi% "%%i"

However this is just a guess, I haven't done it so far. let us know if it works / what you found working :)

@idma-omar
Copy link

Thanks for the wonderful tool! However, it would've been great if it allowed for setting the dimensions of the png files. I have multiple svg files with different sizes and I want for the output images to have the same height (512px) since they are icons (dpi doesn't do the trick). Is there a way to implement this feature?
Also, would it be possible to add a suffix to the output files' title?

Thanks again!

@MaydayGD
Copy link

What you are asking is user specific script. Everyone will have different needs. I have scripts for everything you mentioned. Resizer per width height and with distortion or not. Renamer as well. You can research that on internet.

@JohannesDeml
Copy link
Author

Thanks for the wonderful tool! However, it would've been great if it allowed for setting the dimensions of the png files. I have multiple svg files with different sizes and I want for the output images to have the same height (512px) since they are icons (dpi doesn't do the trick). Is there a way to implement this feature? Also, would it be possible to add a suffix to the output files' title?

Thanks again!

@idma-omar As @MaydayGD mentioned, you should be able to do those steps with another script/program. Maybe FastStone Photo Resizer meets your needs. Then you would export everything with a high enough resolution to png and then use this tool to rename / bring them all to the same height. I think for this script it is better to keep it simple to meet the needs of most while not making it too complicated to use/change.

@knightknight
Copy link

I didn't get it working. Any suggestions?

  • Inkscape 1.2.1 (9c6d41e410, 2022-07-14)
  • Installed via Scoop app.

Added:

`set inkscapePath5="C:\Users\<name>\scoop\apps\inkscape\current\bin"`
(
if exist %inkscapePath5% (
set inkscapePath=%inkscapePath5%
) else
'--version' is not recognized as an internal or external command,
operable program or batch file.

This script allows you to convert all files in this folder from one file type to another
Running with  from
(type q to quit at any question

Allowed file types for source: svg, pdf, eps, emf, wmf
What file type do you want to use as a source? svg

Allowed file types for output: eps, pdf, png, svg
What file type do you want to convert to? png

With what dpi should it be exported (e.g. 300)? 300

Found 0 file(s) of type *.svg in the current folder (C:\inkscape-convert\)


0 file(s) converted from svg to png (Saved in out folder)

Press any key to continue . . .

Screenshot_20230106_125128

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