Last active
December 30, 2021 11:59
-
-
Save edenwaith/f04c7d815bf1d8b82437a5da5937b058 to your computer and use it in GitHub Desktop.
Take an image and output to various Android resolutions (ldpi - xxxhdpi)
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/sh | |
# make-android-icons.sh | |
# Author: Chad Armstrong | |
# Date: 11 December 2020 | |
# Description: Take an image and output to various Android resolutions (ldpi - xxxhdpi) | |
# This script runs under macOS and requires ImageMagick to be installed. | |
# Need ImageMagick first | |
# 1. How to download Homebrew and install it if not present | |
# /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
# 2. Installing ImageMagick | |
# brew install imagemagick | |
# If there aren't the right parameters, display the Usage and bail out | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: make-android-icons file width height" | |
exit -1 | |
fi | |
# Get the command line parameters | |
# Parameters: | |
# filename: name of the original image | |
# width: custom width | |
# height: custom height | |
filename=$1 | |
# Get the file name without an extension | |
# Reference: https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash | |
name="${filename%%.*}" | |
# Get the custom width (set for the mdpi resolution) | |
width=$2 | |
# Get the custom height (set for the mdpi resolution) | |
height=$3 | |
# Generate folders | |
mkdir $name | |
mkdir $name/drawable-ldpi | |
mkdir $name/drawable-mdpi | |
mkdir $name/drawable-hdpi | |
mkdir $name/drawable-xhdpi | |
mkdir $name/drawable-xxhdpi | |
mkdir $name/drawable-xxxhdpi | |
# Scale the images | |
# Scaling ratio:: 3:4:6:8:12:16 | |
# LDPI @ 0.75x | |
convert -resize "$(($width*3/4))x$(($height*3/4))"! $filename "$name/drawable-ldpi/$name.png"; | |
# MDPI @ 1.0x | |
convert -resize "$(($width))x$(($height))"! $filename "$name/drawable-mdpi/$name.png"; | |
# HDPI @ 1.5x | |
convert -resize "$(($width*3/2))x$(($height*3/2))"! $filename "$name/drawable-hdpi/$name.png"; | |
# XHDPI @ 2.0x | |
convert -resize "$(($width*2))x$(($height*2))"! $filename "$name/drawable-xhdpi/$name.png"; | |
# XXHDPI @ 3.0x | |
convert -resize "$(($width*3))x$(($height*3))"! $filename "$name/drawable-xxhdpi/$name.png"; | |
# XXHDPI @ 4.0x | |
convert -resize "$(($width*4))x$(($height*4))"! $filename "$name/drawable-xxxhdpi/$name.png"; | |
# Once complete, open the new image directory in Finder | |
open $name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment