06 julio 2014

Android image drawables - Resize bash linux script

I wrote a bash script to resize image drawables for your apps on android.
It requires the imagemagick package.

The usage is very simple:
./image_resizer <filename> <MDPI size>
./image_resizer.sh filename.png 48

At the moment, you can't provide the filename like /path/to/filename.png.
Instead, you have to copy the image or images, in the script's path.
Also you have to use an image at once. For now, you can't resize two or more images at the same time. Example: *.png it's not supported.

You are free to improve this script, but please, send me a copy to mito150 at gmail.com

#!/bin/bash
#
# Author: Mateu S. <mito150 at gmail.com>
# USAGE: ./image_resizer.sh <filename> <MDPI size> 
#
# TIP: 1 px = 1 dip on MDPI
#
# Example: ./image_resizer.sh  icon.png  48
#

# Path for generated images
RUTA=Drawable-Resized/res

# Detect the extension of the file
EXT=${1##*.}
FILENAME=${1%.*}

# Making Dirs
#mkdir -p $RUTA/drawable-ldpi
mkdir -p $RUTA/drawable-mdpi
mkdir -p $RUTA/drawable-hdpi
mkdir -p $RUTA/drawable-xhdpi
mkdir -p $RUTA/drawable-xxhdpi

# Calculate size for each density
# TIP: LDPI=0.75x, MDPI=1x, HDPI=1.5x, XHDPI=2x, XXHDPI=3x
#SIZE1="$(echo "$2 * 0.75" | bc)"
SIZE2=$2
SIZE3="$(echo "$2 * 1.5" | bc)"
SIZE4="$(echo "$2 * 2" | bc)"
SIZE5="$(echo "$2 * 3" | bc)"

# Scaling images
# TIP: -adaptive-resize is more accurate with details ; -resize less accurate
#convert "$1" -resize $SIZE1 "$RUTA/drawable-ldpi/$FILENAME.$EXT"
convert "$1" -resize $SIZE2 "$RUTA/drawable-mdpi/$FILENAME.$EXT"
convert "$1" -resize $SIZE3 "$RUTA/drawable-hdpi/$FILENAME.$EXT"
convert "$1" -resize $SIZE4 "$RUTA/drawable-xhdpi/$FILENAME.$EXT"
convert "$1" -resize $SIZE5 "$RUTA/drawable-xxhdpi/$FILENAME.$EXT"