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).
-
Doesn't that just show up when you view the file properties?– beatgammitApr 20, 2011 at 2:39
-
1I right click, then click "Get info", and don't see it.– William JockuschApr 20, 2011 at 2:40
6 Answers
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:
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
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
-
-
Nice. But sips doesn't directly have a single-line syntax to return
width x height
?– smciJul 16, 2022 at 21:24
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.
-
3The 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? Apr 20, 2011 at 5:24
-
3This feature depends on Spotlight to index the image's location. Are the picture in a non-indexed location? Apr 20, 2011 at 6:45
This is an old question, but for current readers, there is now a readily available solution. In Yosemite, you can turn on the "Show Preview Column" in Finder. Finder > View > Show View Options
Here's a screenshot.
-
sadly the question was posted in 2011 and yosemite was released in 2014 Sep 28, 2015 at 16:51
-
1yeah, it was the number 1 search result when i looked for answer to this problem on Google, so i thought others would benefit from my answer– SergiSep 28, 2015 at 19:56
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:
-
I find it really bizarre that this is the only option that worked for me. Why do I have to open a browser to view picture dimensions ?!? This is ridiculous. Thanks for the answer nevertheless– MichaelMay 7, 2015 at 22:22
Here is how to use the Finder add a separate 'Dimensions' and 'Resolution' column to the list view of a folder.
- Open your Pictures folder. /Users/~/Pictures
- Create a new folder within the opened Pictures folder. Do not drag one in from elsewhere.
- In this new folder, in list view, right/option-click the column header row.
- Check both of the options available at the bottom of the contextual menu, 'Dimensions' and 'Resolution'.
Now the nice part.
- Drag this new folder onto the desktop (or wherever) and open.
- Note that the modified header row retains the headers 'Dimensions' and 'Resolution'.
- Add images files to the folder and in list view read the dimensions and resolutions info for all the image files at once and not one at a time with the various info panel methods.
Sometimes Resolution is not calculated or available and a dash will appear in the column. See attachment.
This still works as of macOS Sierra 10.12.6 I do not know if this folder setting sticks when the folder is moved to another user account, network folder, etc.
Screenshot: Folder in list view with Dimension and Resolution columns.
Open the image in preview then go to tools>Show Inspector(⌘S). This will bring up a window that displays the image dimensions with pixels as the unit.