Skip to content

Instantly share code, notes, and snippets.

@technoir42
Last active November 8, 2016 21:31
Show Gist options
  • Save technoir42/b31ba76adcd90711695a1513e88ed384 to your computer and use it in GitHub Desktop.
Save technoir42/b31ba76adcd90711695a1513e88ed384 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Generate a set of PNG images for multiple densities from SVG image.
#
# Usage: ./generate_pngs.sh input.svg
#
# Prerequisites:
# macOS: brew install librsvg
# Linux: sudo apt install librsvg2-bin
#
svg_file=$1
output_dir="$(pwd)/res"
generated_densities=( "mdpi:1"
"hdpi:1.5"
"xhdpi:2"
"xxhdpi:3"
"xxxhdpi:4" )
function generate_pngs {
svg_file_name=$(basename "${svg_file}")
svg_name="${svg_file_name%.*}"
echo "Input: ${svg_file_name}"
svg_full_path="$(cd "$(dirname "${svg_file}")"; pwd)/${svg_file_name}"
for i in ${generated_densities[@]}
do
density=${i%%:*}
scale=${i#*:}
density_dir="${output_dir}/drawable-${density}"
mkdir -p ${density_dir}
png_file="${density_dir}/${svg_name}.png"
echo "Generating PNG with density: ${density}"
rsvg-convert -z ${scale} ${svg_full_path} > ${png_file}
done
}
if ! type rsvg-convert >/dev/null 2>&1; then
echo "rsvg-convert was not found. Please install librsvg2."
exit 1
fi
if [[ -n "${svg_file}" ]]; then
generate_pngs
else
echo "Usage: generate_pngs.sh input.svg"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment