Batch optimisers for SVG and PNG files on Debian / WSL2 (Windows Subsystem for Linux).
Converts text to path, inlines CSS styles and optimises all SVG files in a folder. Inkscape runs in shell mode (single launch), Python (lxml) and scour run in parallel on all CPU cores.
- Step 1 – Inkscape: converts
<text>to<path>, preserves original canvas (use--trimto fit canvas to drawing) - Step 2 – Python + lxml: parses SVG with lxml, inlines the presentation properties of simple CSS
.classrules and removes the<style>block when no longer needed (files with@media,@keyframes,:hover... are left untouched) - Step 3 – scour: cleans and compresses SVG (metadata, comments,
xml:space="preserve"and leftover blank lines, IDs, precision);width/heightare always kept (use--keep-idsto leave IDs alone) - Step 4 – Python + lxml: verifies each output file is valid XML; restores from backup and marks
[RESTORED]in the report if corrupted
Original files are backed up to <folder>_backup_svg before any modification.
Inkscape, Python 3, python3-lxml and scour — installed automatically if missing.
mkdir -p ~/.local/bin
cp optimize_svg.bash ~/.local/bin/optimize_svg
chmod +x ~/.local/bin/optimize_svgoptimize_svg [--trim] [--keep-ids] <folder>For full documentation see README_SVG.md.
Losslessly recompresses all PNG files in a folder and strips unnecessary embedded data.
With --trim, also removes excess uniform margins.
- oxipng: lossless recompression; removes ICC profiles, EXIF data and PNG metadata chunks (pixels stay identical, a file is never made larger). Use
--keep-iccto keep the ICC profile - ImageMagick (only with
--trim): trims uniform background pixels at edges with zero colour tolerance, then oxipng runs
Original files are backed up to <folder>_backup_png before any modification.
oxipng — the latest release is downloaded automatically into /usr/local/bin if missing (x86_64 and aarch64).
ImageMagick — needed only for --trim, or as a fallback if oxipng cannot be installed; installed automatically via apt when needed.
mkdir -p ~/.local/bin
cp optimize_png.bash ~/.local/bin/optimize_png
chmod +x ~/.local/bin/optimize_pngoptimize_png [--trim] [--keep-icc] <folder>For full documentation see README_PNG.md.
Both scripts share the same design principles:
- process all files in a folder with a single command
- run in parallel on all available CPU cores
- create a backup of originals before making any change
- stop safely if the backup folder already exists
- print a
Mode:line at start and a per-file size report with total space saved at the end - install missing dependencies automatically via
apt
Both scripts run on Windows via WSL2 with a Debian distribution.
wsl --install -d DebianWindows drives are accessible under /mnt/:
optimize_svg /mnt/c/Users/YourName/Desktop/svg
optimize_png /mnt/c/Users/YourName/Desktop/png