Heuristic
Heuristic

Reputation: 5341

How to find images with certain size in Terminal?

I need to list all images in a folder and its sub-folders, with certain size, say all images that are 320x200, I guess I need to do ls -R *.png then pipe the output to some other command that filters images for that size, my command line skill is pool, can anyone help? Thanks a lot!

Upvotes: 3

Views: 3978

Answers (3)

Heinrich Giesen
Heinrich Giesen

Reputation: 521

In MacOSX there are more helpful terminal commands using metaData (similar to Spotlight):

mdfind, mdls etc. (manual pages exist and can be shown with man mdls …). For what you want to do try mdfind, as shown in the following example to find all files in a given folder (and only in this) with a pixel size greater than 900 x 1100:

mdfind -onlyin /Users/hg/Pictures/2014/01/01 "kMDItemPixelHeight>1100 && kMDItemPixelWidth>900"

The (a bit strange looking) query parameter names can be found in the documentation at DataManagement --> File Management --> MDItemReference. Try mdls filename to see some of these parameters.

Upvotes: 2

l'L'l
l'L'l

Reputation: 47169

You can use sips to get pixelHeight and pixelWidth from images. By combining the command with find you'll be able to recursively search images of a specific size.

example:

results=$HOME/Desktop/results.txt

find . -type f -name "*.png" -exec sips -g pixelHeight -g pixelWidth > $results {} \; 

cat $results | grep "\w\{11\}\:\s\(320\)" -B 1 -A 1 | grep "\w\{10\}\:\s\(200\)" -B 1

results.txt:

/Users/Me/Desktop/nsfw.png
  pixelHeight: 320
  pixelWidth: 200

info:

  1. The first command sets up a variable using the path to results.txt
  2. Next, the find command writes a list of all images found with dimensions to results.txt
  3. Finally we check the results.txt for the specific dimensions (320 x 200) using grep.

These commands can be refined however you want and possibly condensed, but should work as is.

Upvotes: 3

David Andreoletti
David Andreoletti

Reputation: 4861

man find and imagemagick's identify are what you need.

Upvotes: 0

Related Questions