Last active
January 6, 2021 13:05
-
-
Save 3v1n0/bb991e635100220fd307ebab5310b968 to your computer and use it in GitHub Desktop.
Simple script to convert SVG icons to fitbit Grayscale Magic compatible PNGs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) <[email protected]> | |
# 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