Thursday, January 16, 2014

Mega Free Wallpapers Updates

Mega Free Wallpapers Updates

Bash script to sort wallpapers and pictures by their ratios and move them to subfolders

Posted: 29 Oct 2013 04:06 AM PDT

If you have a bunch of pictures which you want to sort and move into sub-folders according to their ratios (such as portrait, landscape, 16-10 wallpapers, 16-9 wallpapers etc), then this bash script I just wrote and successfully used to sort over a thousand pictures in just a couple of minutes may be the thing you need. It runs from the command line, and can be easily modified to suit other ratios. It creates the sub-folders first, and then reads the dimensions of each image and moves it to the appropriate sub-directory. It is written on a Linux PC and requires ImageMagick (installed by default on most distros).

You can download the script via this link (make it executable before you run it) or you can copy & paste the code below.

CODE STARTS HERE    #!/bin/bash    # wallpaper and other JPEG pictures sorting script by Mega Free Wallpapers  # -    # you MUST navigate to the image folder first before you run this script    mkdir -p portraits landscapes 4-3 16-9 16-10 #folder names according to ratio    for f in *.jpg; do    W=`identify -format "%w" "$f"`  H=`identify -format "%h" "$f"`  ratio=$(echo "scale=6; $W / $H" | bc) #scale= is the number of decimal places    # The mathematical ratios to 6 decimal places    sqr=1.333333 #4-3 ratio eg. 1600x1200  pst=1.777777 #16-9 (PlayStation) ratio eg. 1920x1080  wide=1.600000 #16-10 widescreen ratio eg. 1920x1200    # Analyse each picture and if it matches one of the above ratios  # move it to that folder, otherwise move to general landscape or portrait folder        if [[ $ratio = $sqr ]]; then      mv "$f" 4-3/"$f"      elif [[ $ratio = $pst ]]; then      mv "$f" 16-9/"$f"      elif [[ $ratio = $wide ]]; then      mv "$f" 16-10/"$f"      elif [[ "$H" -lt "$W" ]]; then #these lines must go after the numerical ratios      mv "$f" landscapes/"$f"  else       mv "$f" portraits/"$f"  fi  done    exit 0    CODE ENDS HERE  

I was using a Python script called PyImgSort 0.1.6 : Python Package Index, however that one can be a bit complicated to edit if you want to change the ratios being sorted. The one I’ve written makes it nice and simple by keeping everything in the one file. The quotation marks around the $f variable mean that the script should work okay on file names with spaces in them. Hopefully you might find it helpful if you have a large number of pictures to sort.

No comments:

Post a Comment