Sort images based on direction (portrait/landscape)

I have lots and lots of photos on my computer (and servers!). Sometimes I have a pile of photos of which I want to find only the ones that are in portrait or landscape mode. This can be done manually for a few images, but browsing through thousands of them is more tricky. Then I often tend to use a nifty little shell script that I found here. It effectively sorts all images into two folders automagically.