Shell – How to find all square images in a directory

filesimage manipulationimagesshell-script

The images are stored in JPEG and PNG files. I want to get a list of those of them that are square.

Best Answer

You can do this using the command convert from ImageMagick and Awk:

convert *.png *.jp* -format '%w %h %f\n' info: | awk '$1==$2 { $1=$2=""; print substr($0, 3) }'

The command above will output the list of images that have exactly the same number of pixels horizontally and vertically. If instead what you want to find is images that only visually approximate a square, you can do this:

# Find all images in which one side is no more than 5% larger than the other.
convert *.png *.jp* -format '%w %h %f\n' info: | awk '($1>$2?$1:$2)/($1>$2?$2:$1)<=1.05 { $1=$2=""; print substr($0, 3) }'

Note that neither command will work correctly if your images' filenames contain newline characters.

Related Question