Fast Picture Sorter for Ubuntu (Update:Video)

Did you ever feel the need to go through a huge pile of pictures and sort them in different folders?

This script opens the pictures and you have the choice to move them directly to a folder by only pressing one key.

How does it work?

You get presented a picture, a list of destinations enumerated from 1 to max 9 and then simply enter a digit from 1 to max 9 to move it to that folder. This way of sorting is much much quicker and easier than using the mouse, opening a picture, closing the picture an the moving the file to a folder using drag-and-drop.

Instructions:

When you execute the script, the folders in the current directory are shown.
You first have to enter the folder names you want to use in the script for sorting your pictures.
Enter one folder name and press Enter
If the folder doesn’t already exist, it is created.
To finish entering folder names, simply press Enter.
If your selection is correct, press y and then Enter.
Now, a picture is being shown and you get presented your choices in the terminal window.
Press the number according to the folder name you want your picture moved to.
After your selection, the next picture is shown and so on.

If you press “u”, the last step is undone, but the current picture is skipped. This is a bug I cannot correct. If anyone has an idea, please post it here!

If you are finished, or simply bored, enter q. The script is being terminated. A log file named “quit.log” is created where the last moved picture is entered.

Give it a go, it is really fast!

Requirements:
Open a terminal window and enter

sudo apt-get install feh wmctrl

feh is the really fast and great picture viewer, wmctrl is the window manager that manages the focus.

Open gedit or a text editor and paste the following code:

#!/bin/bash

#---Configuration Variables-------------------------------

timeSleep=0.5				#change a bit higher (1) if you have a slower computer. Variable defines time the computer waits until the picture is loaded and the focus shifts back to your terminal window, so that you don't always have to click on the terminal window first and then enter your choice. I wouldn't recommend any lower value than 0.5. Default 0.5
pictureProgram="feh -g 800x600"		#change this command to whatever your favourite viewing program is. Please remember to chose a really fast picture viewer, as you are opening and closing many many pictures. Alternatives would be "eog", but this is slow. Change the timeSleep accordingly. Default "feh -g 800x600"

#---------------------------------------------------------

echo -ne "\033]0;pictures_sorter\007"		#rename terminal window title, for wmctrl to know what window to shift focus to

echo -e "\n-------------------------------------"
echo -e "Here are the folders in this direcory"		#lists folders in current directory
ls -d */
echo -e "--------------------------------------\n"

echo -e "Please enter the names of the folders"
echo -e "you want to store your files into. If"
echo -e "they dont exist, they will be created"
echo -e "Finish by hitting Enter\n"

k=1
folderNameVar=1
while :
	do
	echo -e "$k. Folder Name"
	read folderNameVar				#reads folder Name Variable
	if [ -z $folderNameVar ]			#tests if variable is empty
		then break				#stops the loop
	fi
	folderNameArray[k]=$folderNameVar		#converts the folder Name Variable into array
	if [ -d ${folderNameArray[k]} ]			#checks if folder exists
		then sleep 0
		else mkdir ${folderNameArray[k]}	#creates folder
	fi
	k=$(($k+1))					#takes next array variable
done

echo -e "\nYour folder selection:"
	for (( r=1;r<k;r++)) do				#lists array with the names just entered
		echo -e "${folderNameArray[r]}"
	done
echo -e "\nIs this correct? [y] n"
read correctEntry
	if [ "$correctEntry" = "n" ]			#stops the script if there is an error in the folder selection
		then
		echo -e "\n__Please start the script again__"
		echo -e "Press any key to stop the script now"
		read
		exit 0					#stops the script
	fi

picCounter=0

for i in *.jpg; do

		$pictureProgram ${i} &							#opens picture with defined picture viewer
		pid=$!									#gets pid of the picture program
		sleep $timeSleep							#time to wait until focus back to terminal
		wmctrl -a pictures_sorter						#switches the focus back to the terminal window
		echo -e "Picture ${i}\n"
		for (( r=1;r<k;r++)) do							#loop to display choices that are available
			echo -e "Press $r for ${folderNameArray[r]}"
		done
		echo -e "Press q for quitting \nPress u to undo the last move"	#displays remaining choices
		read -n1 actionDone							#reads your choice. -n1 allows for one character to be entered. You don't have to press Enter at the end of every choice
			if [ "${actionDone}" = "q" ]
				then echo "${i}" > quit.log				#writes last file name to this file
				kill $pid &> /dev/null
				break							#stops loop
			fi
			if [ "${actionDone}" = "u" ]					#undo command
				then mv ${controlFolder}/${controlPicture} ./
				picCounter=$((${picCounter}+1))
				echo "\nMoved Picture ${controlPicture} back to root out of folder ${controlFolder}"
				kill $pid &> /dev/null
				continue						#if this wasn't there, the script would continue with the next if and write that the choice variable is no integer. Continues with the next i in the while loop
			fi
			if [ "$actionDone" -lt "$k" ]
				then mv ${i} ${folderNameArray[actionDone]}		#moves file to the folder. Finds the name in the array specified above
				picCounter=$((${picCounter}+1))
				echo -e "\nMoved the picture to the ${folderNameArray[actionDone]} folder"
			fi
			kill $pid &> /dev/null						#closes the picture program

			controlFolder=${folderNameArray[actionDone]}			#Sort of backup variable for the Folder name, required for the UNDO command
			controlPicture=${i}						#Same as controlFolder, but remembers the file name

done

echo -e "\nYou processed ${picCounter} pictures"

Save this file with the name “sort_pictures” in the directory with the pictures.

Open a terminal and cd to that directory and enter in the terminal:

chmod 777 sort_pictures

To execute the script, simply type:

./sort_pictures

I created a how-to Video.
Watch it in HD and full screen to view everything

3 Responses to Fast Picture Sorter for Ubuntu (Update:Video)

  1. Eddie says:

    Excellent job. Perfect for what I was looking for. One thing would be to have an option to use the dirs that already exist.

  2. thoughtsdaily says:

    @ Eddie
    Thanks. The script can use existing folders. That’s why it lists the contents of your directory at the beginning. Simply type the folder name exactly as it appears (with capital letters etc.) and the program will use that one without creating a new folder. Greetings

    • Eddie says:

      Just my laziness in not wanting to type the dirs each time. Here is what I mean. If I just hit enter at the first prompt, it will just use the dirs in the current dir. If I type any dir names instead of a blank , the script behaves the same as you origionally had it. It’s the code I added to your script, inserted at line 38:

      # if no dirs are entered, take the ones in current dir #ERC
      if [ $k -lt 2 ]; #ERC
      then #ERC
      dir_list=`ls -d */` #ERC
      dir_list=`echo $dir_list | sed -e “s/\///g”` #ERC
      echo $dir_list #ERC
      a=1 #ERC
      for b in $dir_list #ERC
      do #ERC
      folderNameArray[$a]=$b #ERC
      a=$(($a+1)) #ERC #takes next array variable
      done #ERC
      k=$a
      fi

      Feel free to use it, modify it or discard it. Again, thank you very much for an excellent script. Danke schön.

Leave a comment