You are not logged in.

#1 2009-12-23 12:38:48

monotiz
Member
Registered: 2007-10-20
Posts: 140

My script for resize and retouch all photo in a folder

Hi,
I'm excuse for my english but I'm italian.

I've create a script that use mmv and ImageMagick for elaborate the photos in a folder and all subfolder. Actually all the folder and file can't have space in a name. Check this topic to rename the file and folder.

My script:
- remane all *.JPG in *r.jpg
- process all folder (eccept ${Not_Proc_Dir})
- adjust color, resize dimension and reduce the JPEG quality for all file that not contain ${postfix} postfix.
- resize dimension and reduce the JPEG quality for all file that not contain ${prefix} prefix.
- ask you if original file must move in a ${move_folder} folder.

Any suggest and contribut are much appreciated.

#!/bin/sh
echo "+-----------------------------------------+"
echo "|           Powered by MonoTiz            |"
echo "+-----------------------------------------+"
echo "v. 1.1"

#var
Not_Proc_Dir=Sfondi
move_folder=Trash
postfix=magick
prefix=ncp
quality=75
size=2880




#current directory
current=`pwd`

#get all subdirectory
for d in `ls ${current} -R | grep ".*:" | sed 's/://'`
do
    echo ">>>>>>>>>>>> Next directory:" ${d}
    
    #do not process Not_Proc_Dir
    str1=`echo ${d} | grep ${Not_Proc_Dir}`
    strt=`echo ${d} | grep ${move_folder}`
    if [ "${str1}" == "${d}" ]
    then
        echo "xxxxxx Folder" ${d} "not processed"
    else
        if [ "${strt}" == "${d}" ]
        then
            echo "xxxxxx Folder" ${d} "not processed"
        else
        
            echo "+++++ Check" ${d}
            cd ${d}
            
            mmv '*.JPG' '#1r.jpg'
            for i in `ls *.jpg`
            do
                str2=`echo ${i} | sed -n /${postfix}.jpg$/p`
                if [ "${str2}" != "${i}" ]
                then
                    str3=`echo ${i} | sed -n /^${prefix}/p`
                    if [ "${str3}" != "${i}" ]
                    then
                        echo "---------- Processing file" ${i}
                        file=`echo ${i} | sed 's/.jpg//'`
                        convert ${file}.jpg -normalize -modulate 100,108 -resize ${size}x${size}\> -quality ${quality} ${file}_${postfix}.jpg
                    else
                        echo "---------- Processing file (no color correct)" ${i}
                        file=`echo ${i} | sed -n 's/.jpg//'`
                        convert ${file}.jpg -resize ${size}x${size}\> -quality ${quality} ${file}_${postfix}.jpg
                    fi                
                else
                    echo "xxxxxxxxxx File" ${i} "not processed"    
                fi
            done
            echo "+++ Going to root folder"
            cd ${current}
            
        fi
    fi
done
echo "Move original file in" ${move_folder} "(y/n)?"
read var
if [ "${var}" == "y" ]
then

    cd ${current}
    mkdir ./Trash
    
    for d in `ls ${current} -R | grep ".*:" | sed 's/://'`
    do    
        echo ">>>>>>>>>>>> Next directory:" ${d}
    
        #do not process Not_Proc_Dir
        str1=`echo ${d} | grep ${Not_Proc_Dir}`
        strt=`echo ${d} | grep ${move_folder}`

        if [ "${str1}" == "${d}" ]
        then
            echo "xxxxxx Folder" ${d} "not processed"
        else
            if [ "${strt}" == "${d}" ]
            then
                echo "xxxxxx Folder" ${d} "not processed"
            else

                echo "+++++ cd" ${d}
                cd ${d}
            
                for i in `ls *.jpg`
                do
                    str2=`echo ${i} | sed -n /${postfix}.jpg$/p`
                    if [ "${str2}" != "${i}" ]
                    then
                        echo "---------- Move" ${i}
                        mv ${i} ${current}/${move_folder}/
                    fi
                done
                echo "+++ Going to root folder"
                cd ${current}
                
            fi        
        fi
    done
fi

Last edited by monotiz (2009-12-23 13:25:19)

Offline

#2 2009-12-24 23:22:11

crouse
Arch Linux f@h Team Member
From: Iowa - USA
Registered: 2006-08-19
Posts: 907
Website

Re: My script for resize and retouch all photo in a folder

About the only suggestion I would make is to have the script take arguments for some of the variables that would change.  IE: $1 would be for "quality" $2 would  be for "size" etc so you could run your script like

myscript 75 2880

make sense ?  I'm always interested to see imagemagick scripts that manipulate images.

Several years ago, I wrote several shell scripts that do similar things to this.
http://sourceforge.net/projects/bbips/
I have all of them listed http://bashscripts.org/forum/viewforum.php?f=34 here as well.

my resize script looks like this:
WARNING - this script REPLACES your images so ALWAYS backup your original images ... yeah... i wrote a script for that too.... wink

#!/bin/bash
# FILE : bbresize
# Function: Resizes all .jpg images in a directory.
# Copyright (C) 2006-2009 Dave Crouse <crouse@uxxxxx>
# ------------------------------------------------------------------------ #
if [[ -z $( type -p convert ) ]]; then echo -e "ImageMagick -- NOT INSTALLED !";exit ;fi

if [[ $1 = "--help" || $1 = "-h" || $1 = "help" ]]; then
        echo "          Function: Resizes all .jpg images in a directory.";
        echo "          Usage: $0 width height";
        echo "          Requires: Imagemagick";
        echo "          EXAMPLE: $0 400 400";
        echo "          Note: width and height variables MUST be numeric ! ";
        echo "          In the example 400 400 creates images with a maximum width of 400 and maximum height of 400.";
        echo "          ";
        echo "          Comments/Suggestions/Bugfixes to <crouse@xxxxxxxxx>";
        echo "          BASHSCRIPTS.org - http://bashscripts.org";
        echo "          ";
exit 0
fi

if [[ -z "$2" || $1 = *[^0-9]* || $2 = *[^0-9]* ]] ; then
        echo " ";
        echo "          ######### COMMAND FAILED ########## ";
        echo "          USAGE: $0 width height";
        echo "          EXAMPLE: $0 400 400";
        echo "          Note: width and height variables MUST be numeric ! ";
        echo "          In the example 400 400 creates images with a maximum width of 400 and maximum height of 400.";
        echo "          ######### COMMAND FAILED ########## ";echo " ";
else

export IFS=$'\n';
for i in $(find . -maxdepth 1 -type f -iname "*.jpg");
do
echo "Resizing ${i:2} to $1 x $2";convert ${i:2} -resize $1\x$2 new_${i:2}; mv new_${i:2} ${i:2} ;
done
fi
exit 0

Last edited by crouse (2009-12-27 01:11:05)

Offline

#3 2009-12-26 21:37:14

jwcxz
Member
Registered: 2008-09-23
Posts: 239
Website

Re: My script for resize and retouch all photo in a folder

Oh what the heck, while we're at it, here's my script that simply takes a bunch of images and resizes them, makes thumbnails, and orders them sequentially.  It's what I use whenever I post images to my blog.

function mkpics {
    fmt="jpg"
    imgsz="100%"
    tmbsz="15%"
    id=1

    while [ $# -gt 0 ] ; do
        case "$1" in
            -f)
                fmt=$2 ; shift 2 ;;
            -i)
                imgsz=$2 ; shift 2 ;;
            -t)
                tmbsz=$2 ; shift 2 ;;
            -n)
                id=$2 ; shift 2 ;;
            -s)
                sfx=$2 ; shift 2 ;;
            -p)
                pfx=$2 ; shift 2 ;;
            *)
                echo 'error: ' $1 $2
                shift 1 ;;
        esac
    done

    mkdir final

    ls *.* | while read img ; do
            echo "$img    -> $id.$fmt"
            
            convert $img -scale $imgsz final/$pfx$id$sfx.$fmt &
            convert $img -scale $tmbsz final/$pfx$id\_t$sfx.$fmt &
            
            id=$((id+1))
    done
}

-- jwc
http://jwcxz.com/ | blog
dotman - manage your dotfiles across multiple environments
icsy - an alarm for powernappers

Offline

#4 2010-04-12 12:20:31

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: My script for resize and retouch all photo in a folder

Thanks for those scripts. I edited them a little and wrapped them as functions so i could put them in my .bashrc.

This is what i'm using to resize pictures now:

# resize a single picture
function pres () {
#no arguments, print usage
if [ $# = 0 ]
then
 echo "Usage: pres filename."
 echo "Example: pres P103845.JPG"
else
 echo "Resizing ${i:2} to 1920 x 1200 at 90% quality with filename res-$1";convert -resize 1920x1200 -quality 90 $1 res-$1
fi
}

# resize all pictures in a directory
function presa () { # usage: picresa width height quality
for i in $(find . -maxdepth 1 -type f \-iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.bmp");
do
echo "Resizing ${i:2} to $1 x $2 at $3% quality";convert ${i:2} -quality $3 -resize $1\x$2 new_${i:2}; /bin/mv new_${i:2} ${i:2} ;
done
}

# resize one picture specifying quality, width and height
function press () {
#no arguments, print usage
if [ $# = 0 ]
then
 echo "Usage: presz width height quality filename."
 echo "Example: presz 1920 1200 90 P103845.JPG"
else
 echo "Resizing ${i:2} to $1 x $2 at $3% quality with filename ress-$4";convert -resize $1\x$2 -quality $3 $4 ress-$4
fi
}

# move pictures to dir "final", rename and create thumbnails
function prest {
    fmt="jpg"
    imgsz="100%"
    tmbsz="15%"
    id=1

    while [ $# -gt 0 ] ; do
        case "$1" in
            -f)
                fmt=$2 ; shift 2 ;;
            -i)
                imgsz=$2 ; shift 2 ;;
            -t)
                tmbsz=$2 ; shift 2 ;;
            -n)
                id=$2 ; shift 2 ;;
            -s)
                sfx=$2 ; shift 2 ;;
            -p)
                pfx=$2 ; shift 2 ;;
            *)
                echo 'error: ' $1 $2
                shift 1 ;;
        esac
    done

    mkdir final

    ls *.* | while read img ; do
            echo "$img    -> $id.$fmt"
            
            convert $img -scale $imgsz final/$pfx$id$sfx.$fmt &
            convert $img -scale $tmbsz final/$pfx$id\_t$sfx.$fmt &
            
            id=$((id+1))
    done
}

I also tried some packages from the aur to resize pictures and found sir to be the best (only tested gtk apps).

Regards,
demian

Last edited by demian (2011-04-30 15:00:11)


no place like /home
github

Offline

Board footer

Powered by FluxBB