Skip to content

Instantly share code, notes, and snippets.

@agarzola
Last active August 20, 2025 21:37
Show Gist options
  • Select an option

  • Save agarzola/cf497da7fbc3720f7859e6516a51d5c2 to your computer and use it in GitHub Desktop.

Select an option

Save agarzola/cf497da7fbc3720f7859e6516a51d5c2 to your computer and use it in GitHub Desktop.
A very naive script that converts any SVGs in the current directory into PNGs using the rsvg-convert tool. Requires installing librsvg (brew install librsvg).
#!/bin/zsh
# Convert all SVG files in current directory to PNG using librsvg.
for svg_file in *.svg; do
# Check that this is in fact an SVG file.
if [[ ! -f "$svg_file" ]]; then
echo "$svg_file is not an SVG file"
continue
fi
# Get filename without extension.
base_name="${svg_file:r}"
# Run rsvg-convert command.
echo "Converting: $svg_file -> ${base_name}.png"
rsvg-convert --width 1024 --keep-aspect-ratio "$svg_file" --output "${base_name}.png"
# Check if conversion was successful.
if [[ $? -eq 0 ]]; then
echo "✓ Successfully converted $svg_file"
else
echo "✗ Failed to convert $svg_file"
fi
done
echo "Conversion complete!"
@agarzola
Copy link
Author

agarzola commented Aug 20, 2025

The only case I can think of would be if you had a sub-directory named subdirectory.svg, in which case the edge case error message should be "Not a file," not "No svg files".

@apotek Good point. I updated the script to echo a more useful message and also to continue instead of break in such a case.

Thanks for the feedback!

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