11

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

我怎样才能防止这种情况?是否有我可以控制的设置?

编辑:

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

4

5 回答 5

12

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

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

将文件和压缩设置传递给 Automator 中的 shell 脚本

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

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

在Run Shell Script操作顶部的选项中,选择“/bin/bash”作为 Shell,并选择“作为参数”作为 Pass Input。然后粘贴这段代码:

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\"}"

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

于 2012-10-24T19:29:20.357 回答
6

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

  1. 通过实用程序sips外壳。您可以使用它来调整最细微的设置,但由于处理起来有点神秘,您可能会更好地使用第二种方式,
  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 Xcrop aPicture to {width, height}。Mac OS X Automation 有很好的关于scalecrop用法的教程。

于 2012-05-22T14:16:56.657 回答
2

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

于 2013-12-24T00:23:43.600 回答
2

20 年的评论

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

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

于 2020-07-04T20:06:55.013 回答
0

17 年的评论

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

ORG_IFS=$IFS
IFS=$'\n'
for file in $@
do
    ...
done
IFS=$ORG_IFS
于 2017-02-11T04:36:52.043 回答