Linux command line Image Manipulation for Android Developers

Sometimes while developing Applications for Android one has to transform a lot of images at once. In those cases, it is easier to use the command line than standard programs such as gimp.

Resize multiple Images via Command Line

for i in *.png; do convert "$i" -resize 50% "${i%%.png*}.png"; done

I use this a lot when developing for Android, as it is best to supply differently sized images for different devices to save memory.

Lets say I want to display a couple of square icons. The width of the icons should be 1/4th of the screen. I would create base icons with a dimension of 500×500 pixels. The images should be 60×60/80×80/120×120/160×160 pixel for ldpi/mdpi/hdpi/xhdpi respectively. What I do is copy the base images to the correct subdirectory of the res folder and execute the following commands in it (the percentage is calculated by dividing the desired width through the base width, for example: 80/500 = 0.16 = 16%):

# from base
# to xhdpi:
for i in *.png; do convert "$i" -resize 32% "${i%%.png*}.png"; done
# to hdpi:
for i in *.png; do convert "$i" -resize 24% "${i%%.png*}.png"; done
# to mdpi:
for i in *.png; do convert "$i" -resize 16% "${i%%.png*}.png"; done
# to ldpi:
for i in *.png; do convert "$i" -resize 12% "${i%%.png*}.png"; done

Optimize multiple Images via Command Line

I use optipng as well as advpng to optimize the size of my images:

optipng *.png -o5
advpng -z --shrink-insane *.png

Related Posts:

Leave a Reply

Your email address will not be published.