Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a random .PNG file on my Mac. Actually I have about a hundred of them. What is the easiest way to get the pixel dimensions? (I.e, 100 pixels wide and 50 high, or whatever).

share|improve this question
    
Doesn't that just show up when you view the file properties? –  tjameson Apr 20 '11 at 2:39
1  
I right click, then click "Get info", and don't see it. –  William Jockusch Apr 20 '11 at 2:40

4 Answers 4

up vote 12 down vote accepted

In Terminal, you can use the following:

$ sips -g pixelWidth Pictures/238337225.png 
/Users/danielbeck/Pictures/238337225.png
  pixelWidth: 1140
$ sips -g pixelHeight Pictures/238337225.png 
/Users/danielbeck/Pictures/238337225.png
  pixelHeight: 900

To extract the value only, use e.g.

$ sips -g pixelHeight Pictures/238337225.png | tail -n1 | cut -d" " -f4
900

To embed that in AppleScript:

set h to do shell script "sips -g pixelHeight /Users/danielbeck/Pictures/238337225.png | tail -n1 | cut -d' ' -f4"
set w to do shell script "sips -g pixelWidth /Users/danielbeck/Pictures/238337225.png | tail -n1 | cut -d' ' -f4"
display alert "Height: " & (h as text) & "
Width: " & (w as text)

Result:

enter image description here


Alternatively, you can read the Spotlight metadata:

mdls Pictures/238337225.png | grep kMDItemPixel
kMDItemPixelCount              = 1026000
kMDItemPixelHeight             = 900
kMDItemPixelWidth              = 1140

To get the names and dimensions of all files in a directory:

$ mdls Pictures/* | grep "\(kMDItemDisplayName\|mMDItemPixel\)"
[...]
kMDItemDisplayName             = "url.png"
kMDItemPixelCount              = 16384
kMDItemPixelHeight             = 128
kMDItemPixelWidth              = 128
[...]

Or alternatively, using find and sips:

find /Users/danielbeck/Pictures -type f -name "*.png" -exec sips -g pixelWidth {} \; -exec sips -g pixelHeight {} \;


More more flexibility, wrap in a shell script:

$ cat dim.sh 
#!/usr/bin/env bash

filename=$1

if [ ! -f "$filename" ] ; then
    echo "$filename not found!";
    exit 1
fi

h=$( mdls "$filename" | grep kMDItemPixelHeight | tail -n1 | cut -d= -f2 )
w=$( mdls "$filename" | grep kMDItemPixelWidth | tail -n1 | cut -d= -f2 )

osascript -e "tell application \"Finder\" to {activate, display alert \"$filename\\nWidth:$w\\nHeight:$h\"}"

Result after chmod +x dim/sh:

$ ./dim.sh Pictures/flying_cars.png

enter image description here


You could easily extend the script to display dimensions for multiple files at once, or e.g. all png files in a certain directory. Output is as Finder dialog, so you can embed it into an Automator service:

Open Automator and select to create a Service that receives image files as input in any application.

Add a Run Shell Script action that receives input as arguments and enter the following:

dlg=
for f in "$@"
do
    h=$( mdls "$f" | grep kMDItemPixelHeight | tail -n1 | cut -d= -f2 )
    w=$( mdls "$f" | grep kMDItemPixelWidth | tail -n1 | cut -d= -f2 )
    dlg="$dlg$f\nW:$w H:$h\n"
done
osascript -e "tell application \"Finder\" to {activate, display alert \"$dlg\"}"
exit 0

Save as Show Image Dimensions. Select a few image files in Finder and select Finder » Services » Show Image Dimensions or Right-click on one of the files and [Services »] Show Image Dimensions

enter image description here

enter image description here

share|improve this answer
    
Wow +1 ... can't get much better detail than that! Need to write a bash script and you basically did it all for me already! Awesome! –  jsherk Jul 28 '12 at 4:31

Find the file in a Finder window, and either:

  • Highlight the file and press ⌘ Cmd + ⌥ Option + I, or

  • Control-click the file and hold ⌥ Option so you can select "Show Inspector".

This will open an inspector which is similar to the Get Info window, but updates each time you select a file.

Now expand the "More info" section on the inspector. You will be able to see the PNG's dimensions and color depth, among other data. Select a new file to see its dimensions in the inspector.

An inspector window showing the highlighted file in a Finder window

share|improve this answer
    
The weird thing is that when I open that window, what I see under "more info" is Title, Headline, and Last Opened . . . but no dimensions, color space, color profile, or alpha channel. My OS is 10.6.7 and my Finder is version 10.6.8; could that be the problem? –  William Jockusch Apr 20 '11 at 5:24
1  
This feature depends on Spotlight to index the image's location. Are the picture in a non-indexed location? –  Stephen Jennings Apr 20 '11 at 6:45

Not sure if you want to do it manually but this is a great tool that will give you the dimensions of any image: http://www.pascal.com/software/freeruler/

share|improve this answer

The easiest way; open the images in Safari.

Select all the images. Then use Ctrl+Click context menu and choose Open With > Safari.

Optionally you can merge all the open Safari menus together into separate tabs using the Safari menu Window > Merge All Windows.

The top of the Safari window displays the image dimensions: enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.