从 SIPS 获取图像宽度

Get image width from SIPS

当使用 SIPS 获取图像的 pixelWidth 时,它会输出一个标题。例如

sips -g pixelWidth $images

returns

"  pixelWidth: 1920"

我试图只获取整数部分,但没有成功:

sips -g pixelWidth $images | grep \d+$

有什么想法吗?

您想使用 grep 的 -E(正则表达式)和 -o(捕获匹配)标志,这对我有用:

sips -g pixelWidth menu_icon.png | grep -Eo "\d+"

注意 如果您的图像文件路径中有任何数字,它们将会显示(因为 sips 也会打印文件路径),因此您可能需要在 grep 出数字之前添加 grep pixelWidth,如下所示:

sips -g pixelWidth menu_icon.png | grep pixelWidth | grep -Eo "\d+"

只是一个易于阅读、可靠的替代方案,因为您已经有了一个可行的答案,您可以像这样使用 awk

sips -g pixelWidth image.png | awk '/pixelWidth:/{print }'

也就是说... "on lines that contain the word 'pixelWidth:', please print the second field."