Created
October 17, 2015 03:38
-
-
Save Birchwell/28b7c9f5bb64d9595d75 to your computer and use it in GitHub Desktop.
Sort images by dimension, and check the images to delete.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#expecting 2 args: | |
# function display or delete | |
# a directory containing images | |
ZENITY_X=0 | |
ZENITY_Y=0 | |
ZENITY_W=500 | |
ZENITY_H=400 | |
#set options according to function in $1, if isn't recognised, exit | |
#MAX_INPUT is max number of files in list taken from sort by (width+height) | |
#MAX_OUTPUT is max number of files to display or delete | |
#SORTING is command line options for sort; data lines: w+h w h file | |
case $1 in | |
display) MAX_OUTPUT=500; MAX_INPUT=500; SORTING="-n -r" ;; | |
delete) MAX_OUTPUT=500; MAX_INPUT=500; SORTING="-n" ;; | |
*) exit ;; | |
esac | |
WIN_TITLE="ZenityImageSorter_$1" | |
#exit if $2 is null or can't enter directory $2 | |
if [[ -z $2 ]] || ! cd "$2" 2>/dev/null; then exit; fi | |
function IDandSort { | |
while | |
read w h f | |
do | |
#skip file if identify didn't identify it | |
if [[ $w ]] && [[ $h ]]; then | |
printf "%05d %05d %05d %s\n" $((w+h)) $w $h "$f" | |
fi | |
done < <(identify -format "%w %h %f\n" *) | sort $SORTING | head -n $MAX_INPUT | |
} | |
function selectImages { | |
#run a loop in background to resize/move window when it's up | |
{ while | |
! wmctrl -l -G|grep "$WIN_TITLE" >/dev/null | |
do | |
sleep 0.05 | |
done | |
wmctrl -r "$WIN_TITLE" -e 0,$ZENITY_X,$ZENITY_Y,$ZENITY_W,$ZENITY_H | |
} & | |
while | |
read s w h file | |
do | |
printf "%s\n%s\n%s\n" $w $h "$file" | |
done < <(IDandSort) | \ | |
zenity --list --multiple --title="$WIN_TITLE" --print-column=3 \ | |
--column "width" --column "height" --column="file name" 2>/dev/null | \ | |
tr "|" "\n" | |
} | |
readarray -n $MAX_OUTPUT -t images <<< "`selectImages`" | |
#end if no files returned | |
[[ -z ${images[0]} ]] && exit | |
#run command on each returned file | |
case $1 in | |
display) | |
for file in "${images[@]}"; do | |
display "$file" & | |
done ;; | |
delete) | |
rm -f "${images[@]}" ;; | |
esac | |
#don't exit till all child processes have finished | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment