在我的 Mac 上获取 PNG 的像素尺寸?

Wil*_*sch 21 png images macos

.PNG我的 Mac 上有一个随机文件。实际上我有大约一百个。获取像素尺寸的最简单方法是什么?(即 100 像素宽和 50 像素高,或其他)。

Dan*_*eck 37

在终端中,您可以使用以下命令:

$ 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
Run Code Online (Sandbox Code Playgroud)

要仅提取值,请使用例如

$ sips -g pixelHeight Pictures/238337225.png | tail -n1 | cut -d" " -f4
900
Run Code Online (Sandbox Code Playgroud)

将其嵌入 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)

结果:

在此处输入图片说明


或者,您可以阅读 Spotlight 元数据:

mdls Pictures/238337225.png | grep kMDItemPixel
kMDItemPixelCount              = 1026000
kMDItemPixelHeight             = 900
kMDItemPixelWidth              = 1140
Run Code Online (Sandbox Code Playgroud)

要获取目录中所有文件的名称和尺寸:

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

或者,使用findsips

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


更灵活,包装在一个 shell 脚本中:

$ 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\"}"
Run Code Online (Sandbox Code Playgroud)

之后的结果chmod +x dim/sh

$ ./dim.sh Pictures/flying_cars.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


您可以轻松扩展脚本以同时显示多个文件的尺寸,或例如某个目录中的所有 png 文件。输出作为 Finder 对话框,因此您可以将其嵌入到 Automator 服务中:

打开Automator并选择创建接收图像文件作为任何应用程序输入的服务

添加接收输入作为参数运行 Shell 脚本操作并输入以下内容:

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
Run Code Online (Sandbox Code Playgroud)

另存为显示图像尺寸。在 Finder 中选择几个图像文件,然后选择Finder » Services » Show Image DimensionsRight-click在其中一个文件上选择[Services »] Show Image Dimensions

在此处输入图片说明

在此处输入图片说明


Ste*_*ngs 8

在 Finder 窗口中查找文件,然后:

  • 突出显示文件并按⌘ Cmd+ ⌥ Option+ I,或

  • 按住 Control 键单击文件并按住,⌥ Option以便您可以选择“显示检查器”。

这将打开一个类似于“获取信息”窗口的检查器,但每次选择文件时都会更新。

现在展开检查器上的“更多信息”部分。您将能够看到 PNG 的尺寸和颜色深度以及其他数据。选择一个新文件以在检查器中查看其尺寸。

在 Finder 窗口中显示突出显示的文件的检查器窗口

  • 奇怪的是,当我打开那个窗口时,我在“更多信息”下看到的是 Title, Headline 和 Last Opened 。. . 但没有尺寸、颜色空间、颜色配置文件或 alpha 通道。我的操作系统是 10.6.7,我的 Finder 是 10.6.8 版;这可能是问题吗? (3认同)
  • 此功能依赖于 Spotlight 来索引图像的位置。图片是否位于未编入索引的位置? (3认同)