Last active
June 10, 2019 15:55
-
-
Save mexomagno/4362b71e086e549d8c29b42ce7412254 to your computer and use it in GitHub Desktop.
Generate Android App compliant iconography resources
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 | |
######################################################################## | |
# Script to generate Android standard iconography from an image file | |
# | |
# Generated images comply with official iconography specifications. | |
# More at: http://www.androiddocs.com/design/style/iconography.html | |
# | |
# This script relies on ImageMagik's 'convert' command and the awesome ffmpeg lib. | |
# Usage: | |
# | |
# generate_icons IMG_SOURCE ICON_TYPE | |
# | |
# IMG_SOURCE: Path to a supported image from which icons will be generated | |
# ICON_TYPE: Can be 'launcher', 'action_bar', 'notification' | |
# | |
SUPPORTED_EXT=( "png" ) | |
# Get image from first parameter | |
img_path="$1" | |
if [ "$img_path" == "" ]; then echo "Please specify an image"; exit; fi | |
if [ ! -f "$img_path" ]; then echo "Image '$img_path' doesn't exist"; exit; fi | |
# Check extension | |
img_ext="${img_path#*.}" | |
img_ext="$(echo "$img_ext" | awk '{print tolower($0)}')" | |
echo "File extension: '$img_ext'" | |
if [[ ! " ${SUPPORTED_EXT[@]} " =~ " ${img_ext} " ]]; then echo "Extension '$img_ext' not supported (yet)"; exit; fi | |
# Get icon type | |
icon_type="$2" | |
SUPPORTED_TYPE=( "launcher" "action_bar" "notification" ) | |
if [[ ! " ${SUPPORTED_TYPE[@]} " =~ " ${icon_type} " ]]; then echo "Unknown icon type: '$icon_type'. Must be one from: "; printf '\t%s\n' "${SUPPORTED_TYPE[@]}"; exit; fi | |
# print dpi | |
dpi="$(identify -format '%x,%y\n' -units PixelsPerInch "$img_path")" | |
echo "Original image density: $dpi" | |
MDPI_FOLDER="drawable-mdpi" | |
HDPI_FOLDER="drawable-hdpi" | |
XHDPI_FOLDER="drawable-xhdpi" | |
XXHDPI_FOLDER="drawable-xxhdpi" | |
XXXHDPI_FOLDER="drawable-xxxhdpi" | |
if [ -d "$MDPI_FOLDER" ] || [ -d "$HDPI_FOLDER" ] || [ -d "$XHDPI_FOLDER" ] || [ -d "$XXHDPI_FOLDER" ] || [ -d "$XXXHDPI_FOLDER" ]; then | |
echo -n "WARNING: Some of the folders already exist. Continue? (y/n): " | |
read user_response | |
while [ "$user_response" != "y" ] && [ "$user_response" != "n" ]; do | |
echo -n "Invalid answer '$user_response'. Continue? (y/n): " | |
read user_response | |
done | |
if [ "$user_response" == "n" ]; then | |
echo "Doing nothing then" | |
exit | |
fi | |
fi | |
OUT_IMG="" | |
TOTAL_CREATED="0" | |
function print_image_info(){ | |
if [ -f "$OUT_IMG" ]; then | |
echo -e "\tCreated image: '$OUT_IMG'" | |
density="$(identify -format '%x,%y' -units PixelsPerInch "$OUT_IMG")" | |
echo -e "\t\tDensity: $density" | |
size="$(ls -lh $OUT_IMG | awk '{print $5}')" | |
echo -e "\t\tFile size: $size" | |
img_size="$(identify -format '%wx%h' "$OUT_IMG")" | |
echo -e "\t\tImage size: $img_size" | |
else | |
echo -e "\tImage could not be created!" | |
fi | |
} | |
function create_icon(){ | |
IMG=$1 | |
DIR=$2 | |
MULT=$5 | |
SIZE="$(echo "$3 * $5" | bc -l )" | |
DENSITY="$(echo "$4 * $5" | bc -l)" | |
# Create folder | |
if [ -d "$DIR" ]; then | |
echo "WARNING: using already existent directory $DIR" | |
else | |
mkdir "$DIR" | |
fi | |
# Create image | |
OUT_IMG="$DIR/$(basename $IMG)" | |
# Warn if it already exists | |
if [ -f "$OUT_IMG" ]; then | |
echo -n "WARNING: Image '$OUT_IMG' already exists. Override? (y/n): " | |
read user_responsee | |
while [ "$user_responsee" != "y" ] && [ "$user_responsee" != "n" ]; do | |
echo -n "Invalid answer '$user_responsee'. Override? (y/n): " | |
read user_responsee | |
done | |
if [ "$user_responsee" == "n" ]; then | |
echo "Skipping creation then" | |
return 0 | |
fi | |
echo "Overriding existing file" | |
fi | |
img_w="$(identify -format '%w' "$IMG")" | |
img_h="$(identify -format '%h' "$IMG")" | |
if [[ "$img_w" > "$img_h" ]]; then | |
echo | ffmpeg -i "$IMG" -vf scale=$SIZE:-1:flags=lanczos -loglevel quiet "$OUT_IMG" 2> /dev/null | |
else | |
echo | ffmpeg -i "$IMG" -vf scale=-1:$SIZE:flags=lanczos -loglevel quiet "$OUT_IMG" 2> /dev/null | |
fi | |
# Change DPI metadata | |
convert -density $DENSITY -units PixelsPerInch "$OUT_IMG" "$OUT_IMG" | |
TOTAL_CREATED=$(($TOTAL_CREATED+1)) | |
print_image_info | |
} | |
echo -n "Will create icons for " | |
if [ "$icon_type" == "launcher" ]; then | |
BASE_SIZE=48 | |
echo -n "Launcher" | |
elif [ "$icon_type" == "action_bar" ]; then | |
echo -n "Action bar" | |
BASE_SIZE=32 | |
elif [ "$icon_type" == "notification" ]; then | |
echo -n "Notifications" | |
BASE_SIZE=24 | |
fi | |
echo " (Base size: $BASE_SIZEx$BASE_SIZE dp)" | |
BASE_DPI=160 | |
echo "Creating MDPI version..." | |
create_icon $img_path $MDPI_FOLDER $BASE_SIZE $BASE_DPI 1 | |
echo "Creating HDPI version..." | |
create_icon $img_path $HDPI_FOLDER $BASE_SIZE $BASE_DPI 1.5 | |
echo "Creating XHDPI version..." | |
create_icon $img_path $XHDPI_FOLDER $BASE_SIZE $BASE_DPI 2 | |
echo "Creating XXHDPI version..." | |
create_icon $img_path $XXHDPI_FOLDER $BASE_SIZE $BASE_DPI 3 | |
echo "Creating XXXHDPI version..." | |
create_icon $img_path $XXXHDPI_FOLDER $BASE_SIZE $BASE_DPI 4 | |
echo "Finished! Created $TOTAL_CREATED images" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment