Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple script to convert SVG icons to fitbit Grayscale Magic compatible PNGs
#!/bin/bash
# Simple script to convert SVG icons to fitbit grayscale magic compatible PNGs
# Usage: convert-svg.sh [icons-path]
#
# Author: Marco Trevisan (Treviño) <mail@3v1n0.net>
# Licensed under GPLv3
PNG_SIZE=${PNG_SIZE:-80}
icons_path=${1:-$(dirname "$0")}
if ! command -v convert &>/dev/null; then
echo "ImageMagick's convert is needed"
exit 1
fi
convert_svg() {
svg="$1"
png="$2"
if command -v rsvg-convert &>/dev/null; then
rsvg-convert "$svg" -w $PNG_SIZE -h $PNG_SIZE -f png -o "$png"
elif command -v inkscape &>/dev/null; then
if inkscape --version 2>/dev/null | grep -qs "Inkscape 0\.9[0-9]"; then
inkscape -z -w $PNG_SIZE -h $PNG_SIZE "$svg" -e "$png"
else
inkscape -w $PNG_SIZE -h $PNG_SIZE "$svg" -o "$png"
fi
else
echo "WARNING: rsvg-convert or inkscape not found, using ImageMagick"
echo " rendered PNG may not have the expected quality"
convert -density 1200 -resize $PNG_SIZEx$PNG_SIZE "$svg" "$png"
fi
}
for svg in "$icons_path"/*.svg; do
png="$icons_path/$(basename "$svg" .svg)".png
tmp="$(mktemp ${TMPDIR:-/tmp}/tmp.XXXXXXXXX.png)"
convert_svg "$svg" "$png"
convert "$png" -channel RGB -negate -colorspace gray -background black \
-alpha remove -alpha off "$tmp"
mv "$tmp" "$png"
echo "$svg converted to $png"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment