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

@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

@MaydayGD
Copy link

MaydayGD commented Jan 6, 2023

@knightknight

Try this: Create a folder on the desktop, name the folder A, just the letter A, and put this SVG inside that folder: https://upload.wikimedia.org/wikipedia/commons/f/fa/ABB_world_map.svg

The put the script there and run the script and let us know what happened.

From the looks of it those files do not appear to be SVG files.

Maybe someone just changed their extension. Why is the file size of all your SVG files 1KB?

@knightknight
Copy link

@MaydayGD, ok I have tried in C:\A and on the desktop as A. Both gave the same output as my previous post.

Found 0 file(s) of type *.svg in the current folder (C:\A\)
0 file(s) converted from svg to png (Saved in out folder)
Found 0 file(s) of type *.svg in the current folder (C:\A\)
0 file(s) converted from svg to png (Saved in out folder)

Fyi, I'm running this on Windows 11. As for the .svg files those where from IonIcon Designers Pack.

@JohannesDeml
Copy link
Author

@knightknight I think you are missing the inkscape.exe to your path:

set inkscapePath5="C:\Users\<name>\scoop\apps\inkscape\current\bin\inkscape.exe"

Then I get the following:

This script allows you to convert all files in this folder from one file type to another
Running with Inkscape 1.2.1 (9c6d41e410, 2022-07-14) from "C:\Users\Mailuft\scoop\apps\inkscape\current\bin\inkscape.exe"
(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 1 file(s) of type *.svg in the current folder (C:\Data\Documents\Gists\inkscape-batch-convert\)

C:\Data\Documents\Gists\inkscape-batch-convert\test.svg -> C:\Data\Documents\Gists\inkscape-batch-convert\out\test.png [1/1]

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

Press any key to continue . . .

@knightknight
Copy link

knightknight commented Jan 6, 2023

@JohannesDeml, adding .exe might have worked. Except what did work was just double clicking the .bat file instead of 'Run as administrator'. Which I thought would be needed. However the out folder was empty and is being created as 'Read Only'.

Found 1 file(s) of type *.svg in the current folder (C:\Users\<name>\Desktop\A\)

C:\Users\<name>\Desktop\A\ABB_world_map (1).svg -> C:\Users\<name>\Desktop\A\out\ABB_world_map (1).png [1/1]

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

The first part of the script that prints the Inkscape version still didn't work.

'--version' is not recognized as an internal or external command,
operable program or batch file.

@JohannesDeml
Copy link
Author

@knightknight I'm quite sure inkscape is not found correctly in your case. I updated the script to also include the path for scoop as a possible location. Please download again and run it by double clicking (running through the terminal or command prompt should be possible as well).

@knightknight
Copy link

@JohannesDeml, perfecto worked!!

Something must have gone wrong when I copied & pasted the RAW code first time around.

It detected the Inkscape version Running with Inkscape 1.2.1 (9c6d41e410, 2022-07-14) from "C:\Users\<name>\scoop\apps\inkscape\current\bin\inkscape.exe" and I'm getting the output folder with converted .png files.

👍

@JohannesDeml
Copy link
Author

@knightknight Coolcool, glad it works now for you as well. Thanks for letting us know 🎉

@RobG73
Copy link

RobG73 commented Jan 19, 2023

Many thanks for this batch-file. It works perfectly in Windows 11.

I am producing multiple .svg files using a Python program and this library: https://github.com/RobG73/InkscapeFileMaker. Using a program to make the inkscape files means that I can make small changes to every file by changing the program. The problem is that I need to export/convert every svg file to .png before I can include them in a LibreOffice document.

Your program enables me to do this with one click. Brilliant! Thank you.

@JohannesDeml
Copy link
Author

Hi @RobG73 ,

thanks for you comment, glad to hear that the script helps you in your workflow 🥳

Copy link

ghost commented Feb 18, 2023

Hello. I just tried the batch file. Unfortunately, after converting an emf file to a png file, it removes the text contained in it for me. What can I do?
This is the png-file after convert:
1_SeiteL_Lichtgr___1
This ist the emf-file before convert:
1_SeiteL_Lichtgr___1

@JohannesDeml
Copy link
Author

Hi @wethlo448 ,

I tried reproducing the problem by creating an emf through Inkscape. I tried with both convert text to paths enabled and disabled, and in both cases the generated png included the text correctly.
inkscape_wOBQK4MW3h
Did you try to export the file file inkscape directly? And did this work? Did you also make sure to have the specific font installed? And are you using the latest inkscape version (1.2.2)?
If the answer to all of those is yes, please share a file, then I can try to reproduce it :)

Copy link

ghost commented Feb 19, 2023 via email

@JohannesDeml
Copy link
Author

Hi @wethlo448,

You replied on the gist (for which I can't see the attachment), I think you might have wanted to send me an email - You can do so by writing to [email protected] :)

@db333385
Copy link

Hi -thanks for this script - is it possible to convert a PDF file that has multiple pages? So far I can only get it to produce the first page.

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