RalleG RalleG - 11 months ago 50
Bash Question

How to remove pictures below a certain resolution from folder

I have a big folder of more or less random picture files that I use to display desktop backgrounds with Wallch. Since I noticed that some of the pictures have too low resolution to look good I'd like to get rid of anything below 1000 or so pixels in height.

Since I've started using Ubuntu recently I suspect this could be accomplished using a few lines of code on the command line, but I'm very inexperienced using that and would like to avoid anything too disastrous, hence asking for help before experimenting on my own.

The pictures are approximately 4 gigs total, 3000 or so pictures, mostly ending in .png or .jpg, but sometimes in .JPG and some of them are in subfolders at most 3 levels deep.

Any suggestions?

Answer Source

You could use imagemagick and its identify command. http://www.imagemagick.org/script/identify.php

For example something like a bash function which takes a filename as its argument, tweak minX and minY

function rmImgBySize
  xres=`identify -format "%[fx:w]" "$1"`
  yres=`identify -format "%[fx:h]" "$1"`
  if [ $xres -lt $minX ] || [ $yres -lt $minY ]
    rm "$1"

export -f rmImgBySize

find /path/to/basedir/with/pictures/ -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -exec bash -c 'rmImgBySize "$0"' "{}" \;

The -iname tells that name matchins should be caseInsensitive -o tells "or"

man identify
man find