Adam recently blogged about sorting images by attributes such as aspect ratio. While this promises to be an awesome project, it’s a bit complicated for just picking out wallpaper. I’d been thinking of doing something like this a month or so ago, so the weekend after my birthday (as well as that of this blog), I was sitting bored and decided to script up something. Here’s what I came up with — a bash one-liner to just delete anything except 4:3 and 5:4 images (well, split into two so as not to break the page layout):
identify * | \ sed -r ‘s/^([^\[]*)(\[[0-9]*\] | )(JPEG|PNG|BMP|GIF|SVG) ([0-9]*)x([0-9]*).*$/[\1 ]P 3 k \4 \5 \/ p/g’ | \ dc | egrep -v ‘ (1\.333|1\.250)$’ | sed ‘s/^\(.*\) [^[:space:]]*$/\“\1\”/’ | xargs rm -f
(It depends upon ImageMagick for the identify tool.)
And this one-liner will do the same thing for 16:9 and 16:10 images:
identify * | \ sed -r ‘s/^([^\[]*)(\[[0-9]*\] | )(JPEG|PNG|BMP|GIF|SVG) ([0-9]*)x([0-9]*).*$/[\1 ]P 3 k \4 \5 \/ p/g’ | \ dc | egrep -v ‘ (1\.777|1\.778|1\.600)$’ | sed ‘s/^\(.*\) [^[:space:]]*$/\“\1\”/’ | xargs rm -f
You can probably figure out what to change for other aspect ratios.
(Obviously you’d want to copy all your images to a temporary directory and then run this command within that directory, unless you want everything else to be gone permanently.)
Unfortunately. it’ll nuke all images that are close to the needed aspect ratio but don’t quite match. libnautilus-pr0n is likely to be more useful for those kinds of situations.
Or if you like perl
[by greenfly]
Or, if you like perl:
identify * | perl -e '($w,$h) = split /:/, shift; while(<>){ if(/^(.*?)\[\d+\] [A-Z]+ (\d+)x(\d+)/){ ($f,$w2,$h2) = ($1,$2,$3); print "$f\n" if(($w2 / $h2) == ($w / $h));}}' "4:3"This one-liner has the benefit of allowing you to specify a specific res as an argument. A possibly more fun way would be to store the data in a hash of arrays, each array’s key being floating-point ratio, so you could quickly and easily lookup more than one resolution.
Comment by Anonymous — February 17, 2005 @ 3:38 pm