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
@myzenhost
Copy link

thank you for adding it but it is adding the images to the root folder and not the same folder as the original files that reside in sub folders.

also are you able to add any support for multithreading etc to do the process faster? If not i understand.

@myzenhost
Copy link

it also seems to now create a out folder in the root directory only and all copies of the converted files go to that instead of the out folders it made in the original version that would be in the sub folders if the target files are in subfolders which was better.

@myzenhost
Copy link

Also I was able to add CDR to acceptable input filetypes in mine and it worked great both on Old CDRs and New. There are not many conversion software titles out there that can batch convert both old and new CDR files so I would recommend adding it as a standard option for your script as it is truly usefull and already supported :)

@JohannesDeml
Copy link
Author

Hi @myzenhost,
Thanks for the feedback. I changed the script support cdr as input and the folders now stay relative - if you want to export to the same folder just change line 15 to

set relativeOutputFolder=

also are you able to add any support for multithreading etc to do the process faster? If not i understand.

Using the shell command or piping was suggested before. If you want to implement that, I'm happy to take over your changes to the script.

@SHIFT969
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!

I tried it with my icons and if I choose 300 DPI, I get 1600x1600 image.
Divide 512 by 1600, get 0.32.
Then multiply 300 by 0.32, get 96.
Run the script with 96 DPI, get 512x512 images.

Hope that helps someone.

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