【问题标题】:Apple's Automator: compression settings for jpg?Apple 的 Automator:jpg 的压缩设置?
【发布时间】:2012-05-28 22:47:05
【问题描述】:

当我运行 Apple 的 Automator 以简单地将一堆图像剪切成它们的大小时,Automator 也会降低文件 (jpg) 的质量,并且它们会变得模糊。

如何防止这种情况发生?是否有我可以控制的设置?

编辑:

或者有没有其他工具可以做同样的工作但不影响图像质量?

【问题讨论】:

    标签: automator


    【解决方案1】:

    如果您想更好地控制 JPEG 压缩量,正如 kopischke 所说,您必须使用 sips 实用程序,它可以在 shell 脚本中使用。以下是您在 Automator 中的操作方式:

    首先获取文件和压缩设置:

    Ask for Text 操作不应接受任何输入(右键单击它,选择“Ignore Input”)。

    确保第一个 Get Value of Variable 操作不接受任何输入(右键单击它们,选择“Ignore Input”),并且第二个 Get Value of Variable 从第一个获取输入。这将创建一个数组,然后将其传递给 shell 脚本。数组中的第一项是指定给 Automator 脚本的压缩级别。第二个是脚本将执行sips 命令的文件列表。

    运行 Shell 脚本操作顶部的选项中,选择“/bin/bash”作为 Shell,并选择“作为参数”作为传递输入。然后粘贴这段代码:

    itemNumber=0
    compressionLevel=0
    
    for file in "$@"
    do
        if [ "$itemNumber" = "0" ]; then
            compressionLevel=$file
        else
            echo "Processing $file"
            filename="$file"
            sips -s format jpeg -s formatOptions $compressionLevel "$file" --out "${filename%.*}.jpg"
        fi
        ((itemNumber=itemNumber+1))
    done
    ((itemNumber=itemNumber-1))
    osascript -e "tell app \"Automator\" to display dialog \"${itemNumber} Files Converted\" buttons {\"OK\"}"
    

    如果您点击底部的结果,它会告诉您当前正在处理的文件。享受压缩的乐趣!

    【讨论】:

    • 谢谢埃里克!您还有机会分享工作流脚本本身吗?
    【解决方案2】:

    Automator 的“裁剪图像”和“缩放图像”操作没有质量设置——就像 Automator 一样,简单性胜过可配置性。但是,还有另一种方法可以访问 CoreImage 的图像处理工具,而无需借助 Cocoa 编程:可编写脚本的图像处理系统,它使图像处理功能可用于

    1. shell 通过the sips utility。您可以使用此方法来调整最细微的设置,但由于它的处理方式有些晦涩难懂,因此使用第二种方法可能会更好,
    2. AppleScript 通过Image Events,一个由 OS X 提供的可编写脚本的匿名后台应用程序。有 cropscale 命令,以及在保存为 JPEG 时指定压缩级别的选项与

      save <image> as JPEG with compression level (low|medium|high)
      

      使用“运行 AppleScript”操作而不是“裁剪”/“缩放”操作,并将图像事件命令包装在 tell application "Image Events" 块中,您应该已设置好。例如,要将图像缩放到其大小的一半并以最佳质量保存为 JPEG,覆盖原始图像:

      on run {input, parameters}
          set output to {}
          repeat with aPath in input
              tell application "Image Events"
                  set aPicture to open aPath
                  try
                      scale aPicture by factor 0.5
                      set end of output to save aPicture as JPEG with compression level low
                  on error errorMessage
                      log errorMessage
                  end try
                  close aPicture
              end tell
          end repeat
          return output -- next action processes edited files.
      end run
      

      – 对于其他比例,相应地调整因子(1 = 100 %、.5 = 50 %、.25 = 25 % 等);对于作物,将scale aPicture by factor X 替换为crop aPicture to {width, height}。 Mac OS X Automation 有关于scalecrop 用法的很好的教程。

    【讨论】:

    • 感谢您的准确回答,但不幸的是,所有这些事情并没有真正对我说什么。我从未使用过 AppleScript。我也想知道为什么会发生这种情况,因为我的自动化中没有任何内容要求压缩。
    • @melros:Automator 操作是为简单而设计的,而不是可配置性,这意味着您在很大程度上取决于操作开发人员对其实施的决定。
    • 感谢您的详细解答!这实际上给我带来了一条新的道路,我一定会在未来发现更多!
    【解决方案3】:

    Eric 的代码非常棒。可以完成大部分工作。 但如果图像的文件名包含空格,则此工作流程将不起作用。(由于空格会在处理 sip 时破坏 shell 脚本。) 对此有一个简单的解决方案:在此工作流程中添加“Rename Finder Item”。 用“_”或任何你喜欢的东西替换空格。 那么,该走了。

    【讨论】:

      【解决方案4】:

      20 年的评论

      我将脚本更改为快速操作,没有任何提示(用于压缩和确认)。它复制文件并将原始版本重命名为_original。我还包括了 nyam 针对“空间”问题的解决方案。 您可以在此处下载工作流文件:http://mobilejournalism.blog/files/Compress%2080%20percent.workflow.zip(文件已压缩,否则将被识别为文件夹而不是工作流文件)

      希望这对任何寻找这样的解决方案的人有用(就像我在一小时前所做的那样)。

      【讨论】:

        【解决方案5】:

        来自 17 年的评论

        为避免“空格”问题,更改 IFS 比重命名更明智。 备份当前 IFS 并将其更改为仅 \n。并在处理循环后恢复原始 IFS。

        ORG_IFS=$IFS
        IFS=$'\n'
        for file in $@
        do
            ...
        done
        IFS=$ORG_IFS
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-11-02
          • 1970-01-01
          • 2012-09-19
          • 2010-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多