Skip to content

Instantly share code, notes, and snippets.

@sakiwei
Last active September 18, 2018 08:58
Show Gist options
  • Save sakiwei/629e4d8cf253e6489edb to your computer and use it in GitHub Desktop.
Save sakiwei/629e4d8cf253e6489edb to your computer and use it in GitHub Desktop.
Convert 3x scaled images to Android drawable resources
#!/bin/bash
# images (@3x) to
# drawable-xxhdpi(3x),
# drawable-xhdpi(2x),
# drawable-mdpi(1x)
# Usage
# -------
#
# Make script as executable:
# $ chmod -x convertImages.sh
#
# Install imagemagick if needed
# (MaOSX) $ brew install imagemagick
# (Linux) $ apt-get install imagemagick
#
# Run script
# $ sh convertImages.sh /path/to/imagesDir/
SRC=$1
# res folders locations
DIR_XXHDPI=$SRC/res/drawable-xxhdpi/ #3x
DIR_XHDPI=$SRC/res/drawable-xhdpi/ #2x
DIR_MDPI=$SRC/res/drawable-mdpi/ #1x
CreateCleanFolder () {
local directory=$1
if [ -d "$directory" ]; then
rm -rf "$directory"
fi
mkdir -p "$directory"
}
CreateResFolder () {
local directory=$1
local scale=$2
CreateCleanFolder $directory
cp $SRC/*.png $directory
for image in $directory/*.png;
do
convert $image -resize $scale $image;
# rename files to lower case
local output_name=$(echo "$image" | tr '[:upper:]' '[:lower:]')
mv $image $output_name
done
}
CreateResFolder $DIR_XXHDPI 100%
CreateResFolder $DIR_XHDPI 66.7%
CreateResFolder $DIR_MDPI 33.3%
echo "DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment