Alistair Colling
Alistair Colling

Reputation: 1563

How to recursively list the dimensions of all images in a folder recursively using Shell script?

I think the title should explain clearly what I'm trying to do. I'm very new to shell scripts and have managed to put some code together but it isn't right at all- every file on my computer is being listed

I would also ideally like to check all image types ( at least jpg, png and gif)

find "$1" -name ".jpg" | while read imagePath ; do
imageFile=$(basename "$imagePath")
dimensions=$(sips -g pixelHeight -g pixelWidth * )
echo "$videoFile: $dimensions" done

Can anyone help? Thankyou!

Upvotes: 2

Views: 3111

Answers (3)

pe7er
pe7er

Reputation: 444

If you have ImageMagick installed, you could combine its "identify" command with "find".

The following oneliner will give you a recursive list of all jpg images in the underlying directories:

find . -name "*.jpg" -exec identify {} \;

Upvotes: 0

orangenarwhals
orangenarwhals

Reputation: 405

I couldn't find sips so based on the answer above, this one-liner works for me:

$ identify -format "%f: %wx%h\n" *.jpg *.gif *.png | cut -d: -f2 | sort -u | sed 's/x/\t/g'

This outputs a width, height tab-delimited list of filesizes in the current directory (I'm trying to see how many different sizes of images I have in my directory)

 909    503
 924    503

notes

Example output, first part:

jobs8.png: 909x503
jobs.png: 909x503
lobstr.png: 924x503

Second part, cut out filename:

 909x503
 909x503
 924x503

Remove duplicates:

 909x503
 924x503

Make pretty with tabs:

 909    503
 924    503

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207425

This should work nicely for you:

#!/bin/bash

find . \( -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | \
   while read -d $'\0' -r image; do
      read w h < <(sips -g pixelWidth -g pixelHeight "$image" | \
         awk '/Width:/{w=$2} /Height:/{h=$2} END{print w " " h}')
      echo $image $w $h 
   done

Sample Output

./start2.png 640 480
./step2.gif 1 3918
./step2.jpg 1 3918
./step3.gif 2551 1
./survey.gif 2651 4018
./t.jpg 1 463
./tiger.png 258 296

Save the above in a file called pics, then go to the Terminal and type the following one time to make it executable:

chmod +x pics

Then, when you want to run it, just type:

./pics

Though, personally, I would prefer ImageMagick over SIPS, since you can do something similar in one line:

identify -format "%f: %wx%h\n" *.jpg *.gif *.png

Sample Output:

7T6Dj.jpg: 1920x1080
a.jpg: 400x463
b.jpg: 400x463
back.jpg: 906x603
background.jpg: 906x603

If you think of installing ImageMagick on a Mac - use homebrew and it is easy.

Updated Answer

If you only want to output each image size once, ignoring second and subsequent images with identical dimensions, you can add some awk at the end like this:

#!/bin/bash

# Find all types of imagery GIF, JPG, PNG
find . \( -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif" \) -print0 | \
   while read -d $'\0' -r image; do
      read w h < <(sips -g pixelWidth -g pixelHeight "$image" | \
         awk '/Width:/{w=$2} /Height:/{h=$2} END{print w " " h}')
      echo $image $w $h 
   done | awk '{w=$(NF-1); h=$(NF); if(!seen[w SUBSEP h]++)print $0}'

Upvotes: 7

Related Questions