Convert resize photos images – Konwertuj zdjęcia

#!/bin/sh

# Purpose: To convert jpg images,
# convert big pictures from you digital camera to small version
# Howto use: sizeconv.sh <pictures' directory path>
# it's just a sample: ls -1Q *.[Jj][Pp][Gg]
# assumption: photos' extension *.jpg  or *.JPG
# convert_param veriable:
# -option parameter_value
# example:
# -scale 50%
# -resize 150
# -sample 200%

#specify conversion option and value:

convert_param="-resize 600"

# Check, whether Imagemagick is being installed
if [ ! -x /usr/bin/convert ]
then    echo "Imagemagick is not installed"

    if [ -x /usr/bin/apt-get ]
    then     echo; echo "Let's try to install it..."
        sudo apt-get -y install imagemagick
        if [ $? != 0 ]
        then echo "Installation Error... please verify it and try to install imagemagick manualy"; sleep 3; exit
        fi
    else    echo "Package imagemagick must be installed on your system"; sleep 3; exit
    fi
fi

# statemant validation
if [ -z $1 ]
    then
    echo "Target directory not specified"
    echo "Usage: sizeconv.sh <target directory>"
    sleep 1
    exit 1
fi

echo "Eliminate all spaces from the directories and files names"
echo "Processing directories names..."

find $1 -type d -exec rename 's/ /_/g' {} ; > /dev/null 2>&1

echo "Done..."
echo "Processing filenames..."

find $1 -type f -exec rename 's/ /_/g' {} ;

echo "Done..."
sleep 2

#total_files=`find $1 -name '*.[Jj][Pp][Gg]' -print0 | xargs -0 ls -1 |wc -l`
total_files=`find $1 -type f -name '*.[Jj][Pp][Gg]' | grep -v converted_files |wc -l`
dir_list=`find $1 -type d |egrep -v '^.{1,2} |grep -v converted_files`
clear

for z in $dir_list; do
    cd $z
    dir1=`pwd`

    #Check whether any jpg files exist in the directory
    ls -1 *.[Jj][Pp][Gg]  > /dev/null 2>&1
    if [ $? != 0 ]
    then cd - > /dev/null
    continue
    fi

    ls -1 converted_files/*.[Jj][Pp][Gg]  > /dev/null 2>&1
    if [ $? = 0 ]
    then cd - > /dev/null
    continue
    fi

    if [ ! -d converted_files ]
            then mkdir converted_files
    fi

    #let's roll
        nr_of_files=`ls -1 *.[Jj][Pp][Gg] | wc -l` > /dev/null 2>&1
    g=0
    for i in *.[Jj][Pp][Gg]; do
        echo  "<<<< START >>>>"
        h="$(($h + 1))"
        g="$(($g + 1))"
        echo "Converting: [$h from $total_files] in total"
        echo
        echo "Converting $i: [$g from $nr_of_files] within: $dir1"
        convert $i $convert_param converted_files/$i
        clear
    done
    cd - > /dev/null

done

echo ">>>> Work Complete for $1 <<<<"
exit

#End-Script#