Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Converting Multiple Images via BASH Script

Previously, I posted a flawed script that be able to convert images and lower their resolution as well, though it works for me, but found it very problematic sometimes. Here’s another script I made to perform same purpose: Convert the image to either jpg|png|pdf|tif and has an option to modify the quality [1% to 100%].

example:

converting all images in /home/user/Desktop/rawimg to PDF with 50% quality

$> convert2.sh /home/user/Desktop/rawimg pdf 50

Here’s my script:

 

#!/bin/bash
TARGET_LOCATION="$1"
TARGET_TYPE="$2"
QUALITY="$3"
if [ "$1" = "" ] ; then
 echo "$0 <image location> <image type> [quality] "
 echo "convert a specific folder that contains images to different format and quality"
 echo "[imagetype] - can be either pdf jpg png tif"
 echo "[quality] - from 1 to 100%, default is 100%"
 exit
fi
if [[ $2 =~ jpg|png|pdf|tif ]] ; then
 MODE=1
else
 echo "please select image type from jpg,png,pdf or tif"
 exit
fi
if [ "$3" = "" ]; then
 QUALITY="100"
fi
find $TARGET_LOCATION -maxdepth 1 -type f > /tmp/tmpfile
cat /tmp/tmpfile | \
while read FILENAME; do
# echo "convert '$FILENAME' -quality $QUALITY '$FILENAME.$TARGET_TYPE'"
 BARENAME=$(echo "$FILENAME" | cut -d'.' -f1)
 EXTENSIONNAME=$(echo ${FILENAME: -4} | tr '[A-Z]' '[a-z]')
 if [[ $EXTENSIONNAME =~ jpg|png|pdf|tif ]]; then
 convert "$FILENAME" -quality $QUALITY "$BARENAME.$TARGET_TYPE"
 fi
done
rm -f /tmp/tmpfile
exit 0

 

  

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: