Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Reduce image resolution BASH script

One of my problem in System Administration is auto-converting images to a slight lower resolution when its stored in our File Server, Most of our JPGs are gathered from numbers of digital cameras which they only intend to store in their private network folder. A high resolution image consume too much resources.

I also impost a policy which restrict of storing unrelated documents in our File Server, but still some refuse to abide and still storing images in high resolution. So here’s one of my script that might be useful to you in case we are on same scenario.

# by http://twitter.com/c0l2e
#    ronartos@gmail.com
# converts images with resolution higher than 1024x1024 to lower at least 50% or original

TARGET_FILES=`find "$LOCATION" -iname "*.JPG" | sed 's/ /|/g'`

for files in $TARGET_FILES
       #TESTFILE=`echo "$files" | sed 's/|/ /g' | xargs file -b | awk '{print $1}'`
       TEST1=`echo $files | sed 's/|/ /g'`  #$TESTFILE
       TEST2=`file "$TEST1" -b | awk '{print $1}'`
       if [ "$TEST2" = "JPEG" ]; then
            #echo $TEST1
            find "$TEST1" -iname "*.$OLDEXT" |
            while read F
                 #echo "mv \"${F}\" \"${NEWFILE}\""
                 convert "$F" "$NEWFILE"
                 CHECKSIZE=`file "$NEWFILE" -b | sed 's/ //g' | sed 's/,/ /g' | awk  '{print $2}' | sed 's/x/ /g' | awk '{print $1}'`
                 if [ $CHECKSIZE -ge  1024  ]; then
                    convert  "$F" -resize 50% "$F"
                 rm "$NEWFILE"


One response to “Reduce image resolution BASH script

  1. Justin Hayork May 29, 2009 at 7:01 am

    Usesful enough for me. although there are several image management tool in linux, but most of them are in GUI, and your script is very good script for a file server to clean up unnecessary file, not by deleting them but resizing them.

    Good job

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: