Sort images based on direction (portrait/landscape)

Sometimes I need to find images that are either portrait or landscape. This can be done manually for a few, but browsing through thousands of images it is more tricky. For these things I typically use a nifty little shell script that I found here: