Skip to content

Instantly share code, notes, and snippets.

@tobiasbu
Last active September 20, 2020 02:31
Show Gist options
  • Select an option

  • Save tobiasbu/75707386723a280e93693024c1eef1b3 to your computer and use it in GitHub Desktop.

Select an option

Save tobiasbu/75707386723a280e93693024c1eef1b3 to your computer and use it in GitHub Desktop.
Generate Android mipmap/drawables from SVG file to 6 DPI images
#!/bin/bash
# Example usage:
#
# ./generate-drawables.sh ic_logo.svg 80 /Users/tobiasbu/REPOS/mobile-app/android/app/src/main/res
#
# Arguments:
# #1 <REQUIRED> The SVG file to be converted
# #2 <REQUIRED> DP/DPI of the vector file
# #3 Output path. Default: out/<filename>/*
#
# Make sure to run the script in the same directory of the SVG
#
# Will generate 80dp android drawables for 6 DPI on out/my_image.png/ directory.
# Be sure your original image has sustainable resolution for xxxhdpi drawable,
# which is 80 x 4 PX in case of this example.
#
# Requires ImageMagic or Inkscape#
# SVG conversion recommended to be done with Inkscape:
#
# See: https://inkscape.org/en/
# See: https://inkscape.org/doc/inkscape-man.html
#
# Tested only in MacOS.
#
# Source https://gist.github.com/tobiasbu/75707386723a280e93693024c1eef1b3
# Based on https://gist.github.com/tntclaus/18f4fe7e8540f1cb283d2c7d8ad21d69
#
IMAGE=$(basename "$1")
IMAGENAME="${IMAGE%.*}"
IMAGEEXT="${IMAGE##*.}"
DPSIZE=$2
SUFFIX="mipmap-"
OUT_DIR="out"
OUT_DIR_IMAGES="${OUT_DIR}/${IMAGENAME}"
TAGET_DRAWABLES_DIR="${OUT_DIR_IMAGES}/${SUFFIX}"
TAGET_DIR=0
CREATE_TARGET=$([[ $4 == "-f" ]] && echo 1 || echo 0)
if [ ! -d "$3" ] ; then
if [[ $CREATE_TARGET == 0 ]] || [[ $# -le 3 ]] ; then
echo -e "\033[93mDirectory '$3' not exists. Using ${DRAW_DIR}<\*dpi> folder images target\x1B[39m"
echo -e "\033[93mIf you want to create target folder use '-f' parameter at end of command line\x1B[39m"
elif [[ $CREATE_TARGET == 1 ]] ; then
TAGET_DIR="$3"
mkdir -p $TAGET_DIR
fi
else
TAGET_DIR="$3"
fi
if [[ TAGET_DIR != 0 ]] ; then
TAGET_DRAWABLES_DIR="${TAGET_DIR}/${SUFFIX}"
fi
# OSX_INKSCAPE_PATH="/Applications/Inkscape.app/Contents/Resources/bin/inkscape"
OSX_INKSCAPE_PATH="/Applications/Inkscape.app/Contents/MacOS/inkscape"
USE_INKSCAPE=0
INKSCAPE_PATH=""
if [ $IMAGEEXT == "svg" ] ; then
unamestr="$(uname)"
if [[ "$unamestr" == 'Darwin' ]]; then
INKSCAPE_PATH=$OSX_INKSCAPE_PATH
else
INKSCAPE_PATH=$(which inkscape)
fi
if [ ! -x "$INKSCAPE_PATH" ] ; then
echo "\x1B[93mWARNING:\x1B[39m Inkskape is not in PATH"
else
USE_INKSCAPE=1
fi
fi
if [ ! -x "$(which convert)" ] && [ "${USE_INKSCAPE}" == 0 ] ; then
echo "\x1B[1;31mERROR:\x1B[39m ImageMagic convert is not in PATH while required for this script"
exit 2
fi
function calcSize() {
if [[ $# -eq 0 ]] ; then
echo -e "\033[91mcalcSize: No arguments supplied\x1B[39m" >&2
return -1
fi
local result=$1
if [[ $# -ge 2 ]] ; then
result=$(echo "$result * $2" | bc)
fi
echo "($result+0.5)/1" | bc
}
SIZE_ldpi=$(calcSize $DPSIZE 0.75)
SIZE_mdpi=$(calcSize $DPSIZE)
SIZE_hdpi=$(calcSize $DPSIZE 1.5)
SIZE_xhdpi=$(calcSize $DPSIZE 2)
SIZE_xxhdpi=$(calcSize $DPSIZE 3)
SIZE_xxxhdpi=$(calcSize $DPSIZE 4)
mkdir -p "${TAGET_DRAWABLES_DIR}ldpi"
mkdir -p "${TAGET_DRAWABLES_DIR}mdpi"
mkdir -p "${TAGET_DRAWABLES_DIR}hdpi"
mkdir -p "${TAGET_DRAWABLES_DIR}xhdpi"
mkdir -p "${TAGET_DRAWABLES_DIR}xxhdpi"
mkdir -p "${TAGET_DRAWABLES_DIR}xxxhdpi"
if [ $USE_INKSCAPE == 1 ] ; then
echo "Using inkscape to convert SVG"
COMMONS="--export-overwrite"
INPUT_SVG=`pwd`/$IMAGE
$INKSCAPE_PATH ${COMMONS} -o ${TAGET_DRAWABLES_DIR}ldpi/$IMAGENAME.png -w $SIZE_ldpi $INPUT_SVG
$INKSCAPE_PATH ${COMMONS} -o ${TAGET_DRAWABLES_DIR}mdpi/$IMAGENAME.png -w $SIZE_mdpi $INPUT_SVG
$INKSCAPE_PATH ${COMMONS} -o ${TAGET_DRAWABLES_DIR}hdpi/$IMAGENAME.png -w $SIZE_hdpi $INPUT_SVG
$INKSCAPE_PATH ${COMMONS} -o ${TAGET_DRAWABLES_DIR}xhdpi/$IMAGENAME.png -w $SIZE_xhdpi $INPUT_SVG
$INKSCAPE_PATH ${COMMONS} -o ${TAGET_DRAWABLES_DIR}xxhdpi/$IMAGENAME.png -w $SIZE_xxhdpi $INPUT_SVG
$INKSCAPE_PATH ${COMMONS} -o ${TAGET_DRAWABLES_DIR}xxxhdpi/$IMAGENAME.png -w $SIZE_xxxhdpi $INPUT_SVG
else
convert $IMAGE \
\( +clone -resize $SIZE_ldpi -write ${TAGET_DRAWABLES_DIR}ldpi/$IMAGENAME.png +delete \) \
\( +clone -resize $SIZE_mdpi -write ${TAGET_DRAWABLES_DIR}mdpi/$IMAGENAME.png +delete \) \
\( +clone -resize $SIZE_hdpi -write ${TAGET_DRAWABLES_DIR}hdpi/$IMAGENAME.png +delete \) \
\( +clone -resize $SIZE_xhdpi -write ${TAGET_DRAWABLES_DIR}xhdpi/$IMAGENAME.png +delete \) \
\( +clone -resize $SIZE_xxhdpi -write ${TAGET_DRAWABLES_DIR}xxhdpi/$IMAGENAME.png +delete \) \
\( +clone -resize $SIZE_xxxhdpi -write ${TAGET_DRAWABLES_DIR}xxxhdpi/$IMAGENAME.png +delete \) \
null:
fi
echo -e "\033[92m * Drawables for '$IMAGENAME' were successfully created for $DPSIZE DP \x1B[39m"
echo -e " * Output: $TAGET_DRAWABLES_DIR<\*dpi>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment