#!/bin/sh ##This file was cteated by Chris Wilson (blixtra@gmail.com) ## ##It is a public domain work ## ## ##Imagemagick, Zenity and Gtk are required to run ## ##exit if nothing is selected. if test $# == 0 then zenity --error --title="error" --text="Nothing is selected." exit 1 fi ##return 0 if file is image. isImage () { image=`file -bi "$1" | sed 's:/.*::'` if test "$image" = "image" then return 0 fi return 1 } ##if image file is in list of selected files break loop and continue. Otherwise, exit. counter=0 for i in "$@" do ((counter += 1)) if isImage "$i" then break elif test "$counter" = "$#" then zenity --error --title="error" --text="No images are selected." exit 1 fi done ##exits if "cancel" was entered in one of the dialogs. wasCanceled () { if test "$1" != 0 then exit 1 fi } ##resize dialog resizeTo=`zenity --list --title "Resize Image" --text="What size should the photos be?" --height=260 \ --column="Size" "Thumbnail (160px)" "X-Small (400px)" "Small (640px)" "Medium (800px)" "Large (1200px)" "X-Large (1600px)" "Original Size"` wasCanceled "$?" ##gets digits from return string of last dialog (Original Size option returns empty string). resizeTo=`echo $resizeTo | sed "s/[^[:digit:]]//g"` ##quality dialog quality=`zenity --list --title "Image Quality" --text="What quality should the photos be?" --height=260 \ --column="Quality" "Poor (55%)" "Low (65%)" "Good (72%)" "Very Good (80%)" "Best (85%)"` wasCanceled "$?" ##gets digits from return string of last dialog. quality=`echo $quality | sed "s/[^[:digit:]]//g"` ##if subfolder doesn't exist creat it. subFolder="converted" mkdir -p $subFolder ##the workhorse processImage () { ##we only need the filename. imgFile=`echo "$1" | sed 's:.*/::g'` ##don't resize if "Original Size" was selected. if test -z $2 then ##if file exists just ignore. if test -e "$subFolder/orig-${quality}-$imgFile" then return else convert -quality $quality "$1" "$subFolder/orig-${quality}-$imgFile" fi else ##if file exists just ignore. if test -e "$subFolder/${resizeTo}px${quality}-$imgFile" then return else convert -quality $quality -resize "${resizeTo}x${resizeTo}" "$1" "$subFolder/${resizeTo}px${quality}-$imgFile" fi fi return } progress=0 counter=0 (for i in "$@" ##echoed strings starting with "#" are used as text in the the progress window ##echoed integers are used as the percentage for the progress bar do ((counter+=1)) ((progress = $counter*100/$#)) echo $progress if test "$progress" = 100 then echo "#Finished! Your resized image(s) are in the\nfolder named \"${subFolder}\" inside this folder." else echo "#Processing image ${i} ($counter/$#)" fi ##only process images; ignore rest. if isImage "$i" then processImage "$i" "$resizeTo" fi ##pipe into progress window to update. done) | zenity --progress --title="Resizing images" --text="Processing image ..." --percentage=${progress} exit 0