Skip to content

Instantly share code, notes, and snippets.

@Mercandj
Last active November 2, 2020 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mercandj/9671f17be9efc929bcf175f7a37f1fc6 to your computer and use it in GitHub Desktop.
Save Mercandj/9671f17be9efc929bcf175f7a37f1fc6 to your computer and use it in GitHub Desktop.
Derivation of images into Android drawable folders
#!/bin/sh
# GOAL
# Convert images png or jpg into Android drawable folder and convert into webp
# HOW TO USE IT
# · Setup: "brew install imagemagick" // https://formulae.brew.sh/formula/imagemagick
# · Place your images in the same folder of this script
# · Run the script
# POST SCRIPTUM
# Adapted from https://stackoverflow.com/a/25355105/12057504
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
pushd "${BASE_DIR}" || exit
rm -rf drawable-xxxhdpi
rm -rf drawable-xxhdpi
rm -rf drawable-xhdpi
rm -rf drawable-hdpi
rm -rf drawable-mdpi
mkdir -p drawable-xxxhdpi
mkdir -p drawable-xxhdpi
mkdir -p drawable-xhdpi
mkdir -p drawable-hdpi
mkdir -p drawable-mdpi
for path in *.png; do
if [ "$path" = "*.png" ]; then
echo "Skip png"
else
echo "Creating different drawable dimensions for: ${path}"
if [ "$path" = "ic_launcher.png" ]; then
convert ic_launcher.png -resize 144x144 drawable-xxhdpi/ic_launcher.png
convert ic_launcher.png -resize 96x96 drawable-xhdpi/ic_launcher.png
convert ic_launcher.png -resize 72x72 drawable-hdpi/ic_launcher.png
convert ic_launcher.png -resize 48x48 drawable-mdpi/ic_launcher.png
rm -i ic_launcher.png
else
cp "${path}" "drawable-xxxhdpi/$path"
convert "${path}" -resize 75% "drawable-xxhdpi/$path"
convert "${path}" -resize 50% "drawable-xhdpi/$path"
convert "${path}" -resize 38% "drawable-hdpi/$path"
convert "${path}" -resize 25% "drawable-mdpi/$path"
magick "drawable-xxxhdpi/$path" -quality 80 -define webp:lossless=true "drawable-xxxhdpi/${path%.*}.webp"
magick "drawable-xxhdpi/$path" -quality 80 -define webp:lossless=true "drawable-xxhdpi/${path%.*}.webp"
magick "drawable-xhdpi/$path" -quality 80 -define webp:lossless=true "drawable-xhdpi/${path%.*}.webp"
magick "drawable-hdpi/$path" -quality 80 -define webp:lossless=true "drawable-hdpi/${path%.*}.webp"
magick "drawable-mdpi/$path" -quality 80 -define webp:lossless=true "drawable-mdpi/${path%.*}.webp"
rm -f "drawable-xxxhdpi/$path"
rm -f "drawable-xxhdpi/$path"
rm -f "drawable-xhdpi/$path"
rm -f "drawable-hdpi/$path"
rm -f "drawable-mdpi/$path"
fi
fi
done
for path in *.jpg; do
if [ "$path" = "*.jpg" ]; then
echo "Skip jpeg"
else
echo "Creating different drawable dimensions for: ${path}"
cp "${path}" "drawable-xxxhdpi/$path"
convert "${path}" -resize 75% "drawable-xxhdpi/$path"
convert "${path}" -resize 50% "drawable-xhdpi/$path"
convert "${path}" -resize 38% "drawable-hdpi/$path"
convert "${path}" -resize 25% "drawable-mdpi/$path"
magick "drawable-xxxhdpi/$path" -quality 80 -define webp:lossless=true "drawable-xxxhdpi/${path%.*}.webp"
magick "drawable-xxhdpi/$path" -quality 80 -define webp:lossless=true "drawable-xxhdpi/${path%.*}.webp"
magick "drawable-xhdpi/$path" -quality 80 -define webp:lossless=true "drawable-xhdpi/${path%.*}.webp"
magick "drawable-hdpi/$path" -quality 80 -define webp:lossless=true "drawable-hdpi/${path%.*}.webp"
magick "drawable-mdpi/$path" -quality 80 -define webp:lossless=true "drawable-mdpi/${path%.*}.webp"
rm -f "drawable-xxxhdpi/$path"
rm -f "drawable-xxhdpi/$path"
rm -f "drawable-xhdpi/$path"
rm -f "drawable-hdpi/$path"
rm -f "drawable-mdpi/$path"
fi
done
popd || exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment