Script to quickly resize photos

Discussion and support for all Linux distributions and Unix flavours (FreeBSD, OpenBSD, etc).
Post Reply
AlphA
Registered User
Posts: 3213
Joined: 15 Mar 2005, 02:00
Location: JHB
Contact:

Script to quickly resize photos

Post by AlphA »

I send a lot of photos of my son to my family abroad, and usually use "The Gimp" to resize the pics. Decided that was too time consuming when resizing > 10 pics, so I wrote a little script to quickly do it. Thought I'd share.

Code: Select all

#!/bin/bash
mogrify -resize 800 *.JPG
echo "resize done...now decreasing quality and file size"
mogrify -quality 80 *.JPG
echo "all done!"
This will resize to 800X600 and set the quality to 80% of the original. Change the numeric figures to suit your needs. Just incapsulate the code above into a shell script called "resize.sh" (or whatever you like)...do a chmod 755 of the file and run it from the directory in which you have the pics. It will overwrite the original pics, so I suggest copying the pics to a temp diretory and running from there. Also, my camera (Nikon 4600) stores the file extension as uppercase "JPG", some might use "jpg", so change the script accordingly.
Image
DAE_JA_VOO
Registered User
Posts: 12310
Joined: 28 Nov 2005, 02:00
Location: That other place
Contact:

Post by DAE_JA_VOO »

Wow, i wish i could write scripts. Instead, i download them :P

Here's the one i use:

Code: Select all

#!/bin/bash
# Original (NIS) author : Mathieu Vilaplana <mathieu@creationgif.com> 
# Modified by: Radagast <rhosgobel2@gmail.com>
# Date : 02/19/2007
#depends: imagemagick, zenity, rename
# thanks to coffe
#version 0.4
#	- check mime type
#since v 0.4, solve bug with filename spaces
#version 0.6
#     - correct bug in filename with spaces
#     - create a subdirectory to create images
#version 0.7
#     - changed name from NIS to Resize_images for ease of menu selection
#     - changed list to contain solely the max dimension
#     - added more size options
#     - made directory name nicer ("resized_to_xxx")
#     - appended image size to file name for jpgs and gifs ("file.jpg" becomes "file_xxx.jpg")
#     - created user-friendly dialog text
#version 0.75
#     - specified bash for Edgy compatibility, based on 0.8 of NIS (http://www.creationgif.com/debian/nis/); 
#     - added PNG to list of file types that is renamed.

#test if a file has been selected
if [ $# -eq 0 ]; then
	zenity --error --title="error" --text="You must select at least 1 file to process"
	exit 1
fi

#=========================
#       SELECT SIZE DIALOG
# Add or remove (or resort) items from (in) the following list 
#   to customize the sizes available and the order in which they appear.
#   It would be nice to make this list easier to edit (make it a variable?  A file in user's home directory?)
title="Resize images"
dialogtext="Select the maximum dimension length (in pixels) \nto which you want the image(s) resized. \n\n*Script designed to work on jpgs and gifs.*"
maximgsize=`zenity --title "$title" --text="$dialogtext" --list --separator=" " --column="size (px)" "100" "150" "160" "200" "300" "320" "400" "500" "600" "640" "800" "1024" "1280" `

#if $? != 0, user click on cancel button, so exit
if [ "$?" != 0 ] ; then
	exit
fi

#user must select a target size
maximgsize=`echo $maximgsize | sed 's/ max//g'`
if [ ! "$maximgsize" ]; then
	zenity --error --title="error" --text="select a target size"
	exit
fi

#Assign max dimensions to variables for later.
imgsize="${maximgsize}x${maximgsize}" # used for the resize command
niceimgsize="$maximgsize" # used to append to file name
imgsizedir="resized_to_$maximgsize" # name of the directory that will be created

#       END SELECT SIZE DIALOG
#=========================

# Quality is currently hardcoded to be 80 (in the "convert" line, below).
# If desired, another dialog could be rasied here for user selection of quality level.

#Select only images
nb_images=0;
selection="";
while [ $# -gt 0 ]; do
	isimage=`file -bi "$1" | grep image | wc -l` 
	if [ $isimage -eq 1 ]; then
		selection[$nb_images]=$1
		let "nb_images++"
	fi
	shift
done

#create directory if not exist and at least one image to process
if [ ! -d $imgsizedir  ] && [ "$nb_images" -gt "0" ];then
		mkdir $imgsizedir
fi

#iterate through selected images, resize, and rename
n=$nb_images
let "n=n-1"
(for i in `seq 0 $n`;do
	picture=${selection[$i]}
	let "compteur += 1"
	echo "# Processing image $compteur / $nb_images $picture ..."
	convert -quality 90 -resize $imgsize "$picture" $imgsizedir/"$picture"
        # rename the created file - there's got to be an easier way to handle all cases.
        rename s/.JPG$/_$niceimgsize.JPG/ $imgsizedir/"$picture"
        rename s/.jpg$/_$niceimgsize.jpg/ $imgsizedir/"$picture"
        rename s/.JPEG$/_$niceimgsize.JPEG/ $imgsizedir/"$picture"
        rename s/.jpeg$/_$niceimgsize.jpeg/ $imgsizedir/"$picture"
        rename s/.GIF$/_$niceimgsize.GIF/ $imgsizedir/"$picture"
        rename s/.gif$/_$niceimgsize.gif/ $imgsizedir/"$picture"
        rename s/.PNG$/_$niceimgsize.PNG/ $imgsizedir/"$picture"
        rename s/.png$/_$niceimgsize.png/ $imgsizedir/"$picture"
	let "progress = compteur*100/nb_images"
	echo $progress
done
) |
        zenity --progress --auto-close --title="Scaling images"  --text="Processing images ..."  
--percentage=0


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# A copy of the GNU General Public License is available at http://www.gnu.org/copyleft/gpl.html
# or can be obtained by writing Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
That one gives you a list of different sizes to chose from.
That guy that used to mod cases. Now I take photos. True story.
AlphA
Registered User
Posts: 3213
Joined: 15 Mar 2005, 02:00
Location: JHB
Contact:

Post by AlphA »

hehehe - thanks DJV... Its ok to download shell scripts if you read and try to understand them.

My script can be easilly modified to pass parameters from the command-line for size and quality settings.

Code: Select all

#!/bin/bash
#check command line parameters
if (( $# < 2 ))
 then
  echo "$0 requires at least 2 parms - first parm is desired resolution and second parm    is the quality"
 exit 255
else
 if (( $# > 2 ))
  then
  echo "$0 takes no more than 2 parms - first parm is desired resolution and second parm is the quality"
  exit 255;
 fi
fi
#start the resize
mogrify -resize $1 *.JPG
echo "resize done...now decreasing quality and file size"
mogrify -quality $2 *.JPG
echo "all done!"
exit 0
You then run it by passing two parameters. For example

Code: Select all

./resize.sh 800 80
or any resolution and quality you like.
Image
DAE_JA_VOO
Registered User
Posts: 12310
Joined: 28 Nov 2005, 02:00
Location: That other place
Contact:

Post by DAE_JA_VOO »

Oh cool :D
That guy that used to mod cases. Now I take photos. True story.
Post Reply