【问题标题】:Resizing Images using bash in Automator and retain image ratio在 Automator 中使用 bash 调整图像大小并保留图像比例
【发布时间】:2013-06-10 22:07:04
【问题描述】:

我正在尝试编写一个自动化应用程序,它将获取图像文件并将它们调整为指定的宽度,但保持原始图像文件的高度/宽度比。

我一直在尝试在 bash 中使用 sip,但我不确定我哪里出错了。我在谷歌上找不到任何可以参考 BASH 或 sips 的东西。

我正在尝试获取传递图像的高度和宽度,计算出比例,然后使用目标宽度和目标高度调整图像大小(根据目标宽度和比例计算)

这是我当前的 shell 脚本,我将图像作为Pass input: as arguments. 传递给

 height=`sips --getProperty pixelHeight $@`
 width=`sips --getProperty pixelWidth $@`
 ratio=height/width
 targetWidth=262
 targetHeight=targetWidth*ratio

 sips --resampleHeightWidth targetHeight targetWidth $@

我什至不确定这是否是正确的做法,所以任何建议都会有所帮助。

【问题讨论】:

  • 我觉得我完全是个白痴。我没有意识到有一个 resampleWidth 函数可以有效地满足我的需要。 sips --resampleWidth 262 $@ 对不起大家。

标签: macos bash automator sips


【解决方案1】:

您可以使用--resampleWidth-Z

sips --resampleWidth 262 "$@" # make smaller or larger so that the width is 262 px
sips -Z 262 "$@" # make smaller or larger so that the longer sides are 262 px

如果您想防止放大较小的图像,请参阅this answer,或使用 ImageMagick。我认为 sips 通常会使图像在没有额外锐化的情况下看起来过于模糊,但 ImageMagick 还允许选择不同的重采样过滤器:

mogrify -filter lanczos2 -resize '262>' "$@"

mogrifyconvert 的一个版本,它可以修改图像。 lanczos2(2-lobed Lanczos)使图像比lanczos(3-lobed Lanczos,在某些时候成为缩小的默认过滤器)稍微不那么清晰。 262> 调整宽于 262 像素的图像大小,使其宽度为 262 像素。

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 2013-08-20
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2017-08-24
    相关资源
    最近更新 更多