Sunday, April 28, 2013

Thunar Custom Actions: Pictures Batch Resize with Zenity Progress Bar and Convert

This is e better version of my old picture resize script. It uses Zenity to ask for the resizing percentage and to show a progress bar while the script is working.

Screenshots

Resize Script Enter Percentage

Resize Script Progress Bar
Resize Script Work Completed

Bash Script


picresize.sh
 1 #!/bin/bash
 2 
 3 
 4 # paths, constants, etc.
 5 PICPATH="$1"
 6 TOTALPICS="$((($# - 1)))"
 7 STEP="$(((100 / $TOTALPICS)))"
 8 PROGR=0
 9 MINSIZE=1
10 MAXSIZE=500
11 
12 # get the new size percentage, check it and set output directory path
13 SIZE=""
14 while [ -z "$SIZE" ] || [[ $SIZE<$MINSIZE ]] || [[ $SIZE>$MAXSIZE ]]; do
15     SIZE="$(zenity --entry --title="Picture Resize" --text="Enter percentage...")"
16 
17     # exit if cancel pressed
18     [[ $? != 0 ]] && exit 0
19 
20     # check range
21     if [[ $SIZE<$MINSIZE ]] || [[ $SIZE>$MAXSIZE ]]; then
22         zenity --error --text="$SIZE% is outside the accepted range: 1% - 500%!"
23     fi
24 done
25 
26 OUTPATH="resize_$SIZE%"
27 
28 # check if output directory exists
29 [ -d "$PICPATH/$OUTPATH" ] || mkdir "$PICPATH/$OUTPATH"
30 shift
31 
32 # convert pics, show progress bar
33 while (( $# )); do
34     convert "$PICPATH/$1" -resize "$SIZE%" "$PICPATH/$OUTPATH/resized_$SIZE%-$1"
35     ((PROGR+=$STEP))
36     echo $PROGR
37     shift
38 done | zenity --progress --title="Picture Resize" --text="working..." --percentage=0 -
39                                  -auto-close
40 
41 # check return value and print message depending on it
42 [[ $? == 0 ]] && zenity --info --title="Picture Resize" --text="All done!" || zenity -
43        -error --text="Canceled!"

Add Thunar Custom Action

Please refer to this article for adding a custom action to Thunar.
Here is a screenshot of my custom action for this resizing script.

Note that the bash script needs two "thunar arguments" :
  1. Path to the pictures to be resized -> %d
  2. Pictures list -> %N


1 comment:

  1. That is really good and informative post, will be sharing it with my friends, may be they will get benefit out of it. Thank you for sharing it with us

    ReplyDelete