Reputation:
I want to convert multiple SVG files in the folder C:\Users\Eric\Desktop\svg
to 512x512 PNG files with the name [SVG File Name].svg.png
.
I tried the following command:
for /f %f in ('dir /b "C:\Users\Eric\Desktop\svg"') do inkscape -z -e %f.png -w 512 -h 512 %f
The command line detects the SVG files correctly and goes through them but Inkscape says the following:
C:\Users\Eric\Desktop\inkscape>inkscape -z -e [SVG File Name].svg.png -w 512 -h 512 [SVG File Name].svg
** (inkscape.exe:8412): WARNING **: Can't open file: [SVG File Name].svg (doesn't exist)
** (inkscape.exe:8412): WARNING **: Can't open file: [SVG File Name].svg (doesn't exist)
** (inkscape.exe:8412): WARNING **: Specified document [SVG File Name].svg cannot be opened (does not exist or not a valid SVG file)
I opened one file in the normal Inkscape program, and it worked.
Upvotes: 5
Views: 2279
Reputation: 386
I had a single-file conversion script and was also looking for a folder-based option.
I tested the previous answers on a set of 200 SVGs, converting them into 2000px PNGs and the results were pretty interesting!...
Method | Example | Time | Comparison |
---|---|---|---|
Basic | FOR %%G IN ("*.svg") DO ( inkscape.exe --export-type=png --export-width=2000 %%G ) |
3:42 | |
Basic +Shell | FOR %%G IN ("*.svg") DO ( inkscape.exe --export-type=png --export-width=2000 %%G --shell ) |
3:44 | No improvement |
Action List | FOR %%G in ("*.svg") DO ( ECHO file-open:%%~fsG;export-width:2000;export-type:png;export-filename:%%~nG.png;export-do;file-close; >> TempFile.txt ); TYPE TempFile.txt | Inkscape.exe --shell |
0:42 | 5x FASTER |
I have included my folder-based SVG converstion script below...
:: Convert-SVG-Folder.bat
:: • Converts any SVGs in the current folder to PNGs
:: • Supply a folder path to convert SVGs in a different folder
:: • Any existing PNGs will be overwritten
@ECHO OFF
SETLOCAL EnableDelayedExpansion
:: Script config.
SET /A ExportWidth=2048
SET Inkscape="C:\Program Files\Inkscape\bin\inkscape.exe"
:: Temp file added in case of any future debugging
SET TempFile="%Temp%\SVGList.txt"
DEL %TempFile% >NUL 2>&1
ECHO Generating script file...
IF NOT "%~1"=="" PUSHD %1
FOR %%G in ("*.svg") DO (
ECHO file-open:%%~fsG; export-width:%ExportWidth%; export-type:png; export-filename:%%~nG.png; export-overwrite; export-do; file-close; >> %TempFile%
)
ECHO Converting SVG files...
TYPE %TempFile% | %Inkscape% --shell
DEL %TempFile% >NUL 2>&1
Upvotes: 0
Reputation: 1957
Inkscape 1.3 on Windows 10:
for %G in ("*.svg") do ("C:\Program Files\Inkscape\bin\inkscapecom.com" --export-width=512 --export-height=512 --export-type=png %G)
In their answer, user10609288 wrote:
For better performance, you should use shell mode
Yes, I agree.
My initial answer, like the example in the question, invokes Inkscape separately for each SVG file.
Instead, you can use the Inkscape shell mode to convert all SVG files in a directory using the same single instance of Inkscape:
(for %G in ("*.svg") do @echo file-open:%~fsG;export-width:512;export-height:512;export-type:png;export-filename:%~nG.png;export-do;file-close;) | "C:\Program Files\Inkscape\bin\inkscapecom.com" --shell
inkscapecom.com
[sic]? com.com
? Really?!Yes, really.
At least, that's the file name in the Inkscape 1.3 (version 0e150ed6c4, 2023-07-21) that I downloaded today (2023-09-12).
I gather that's a mistake. For details, see Inkscape GitLab issue #8893, "inkscape.com replaced with inkscapecom.com in Inkscape 1.3".
*.png
instead of *.svg.png
From the question:
I want ... the name
[SVG File Name].svg.png
If you decide you would prefer [SVG File Name].png
, without the .svg
, then add the following option to the command line:
--export-filename=%~nG.png
The modifier ~n
extracts the base file name without the file extension.
I also have SVG files that I want to batch-convert to PNG.
Here's the command line that I use:
for %G in ("*.svg") do ("C:\Program Files\Inkscape\bin\inkscapecom.com" --export-dpi=115 --export-background=#ffffff --export-background-opacity=1.0 --export-type=png --export-filename=%~nG.png %G)
or, using Inkscape shell mode:
(for %G in ("*.svg") do @echo file-open:%~fsG;export-dpi:115;export-background:#ffffff;export-background-opacity:1.0;export-type:png;export-filename:%~nG.png;export-do;file-close;) | "C:\Program Files\Inkscape\bin\inkscapecom.com" --shell
In my case:
To ensure that text in the PNG matches the 16px text in those contexts, I use the export-dpi
option to increase the size of the image by a factor of 1.2 (12 divided by 10) according to the following calculation:
1.2 * 96 = 115.2
where 96 is, effectively, the DPI of the original SVG.
Upvotes: 0
Reputation: 191
For SVG to PNG conversion I found cairosvg (https://cairosvg.org/) performs better than ImageMagick. Steps for install and running on all files in your directory.
pip3 install cairosvg
Open a python shell in the directory which contains your .svg files and run:
import os
import cairosvg
for file in os.listdir('.'):
if os.path.isfile(file) and file.endswith(".svg"):
name = file.split('.svg')[0]
cairosvg.svg2png(url=name+'.svg',write_to=name+'.png')
This will also ensure you don't overwrite your original .svg files, but will keep the same name. You can then move all your .png files to another directory with:
$ mv *.png [new directory]
Upvotes: 2
Reputation:
Inkscape is a good program. Sometimes we don't understand the possibilities it has. For better performance, you should use shell mode. This mode consists of 2 steps:
type .\command.txt | inkscape --shell
where command.txt
your file name, in windows console or bash.All commands located in action-list
, after typing inkscape --shell
.
For example, if you want to convert SVG to png, your txt file should contains:
file-open:1.svg; export-filename:1.png; export-do; file-close
file-open:2.svg; export-filename:2.png; export-do; file-close
Syntax is command
:arg
; command2
:arg2
; etc
You can create this file using your favorite language like C++, Java, C# or Python.
P.S.
It's faster than using inkscape
command with every file in PowerShell, but don't use it for long time operations because it has a memory leak:
Link to Gitlab
Upvotes: 0