Get pixel dimensions of a PNG on my Mac?
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).
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
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.