在Automator中将图像调整为特定像素宽度和高度

时间:2013-02-07 17:14:55

标签: macos automator

我一直在寻找大约一个小时试图找到一种方法来做到这一点。

Automator中是否有任何方法可以将图像调整为指定的宽度和高度,而不仅仅是最长的尺寸?我需要我的输出图像是特定的像素宽度和高度,即使它是不成比例的。

我有Photoshop,所以有没有办法让Automator这样做?

由于

2 个答案:

答案 0 :(得分:15)

使用标准Automator步骤无法满足您的需求。

无论如何,您可以使用this Python脚本:

import sys
import CoreGraphics

RESIZE_WIDTH = 100
RESIZE_HEIGHT = 100

def resizeimage(source_image_file, target_image_file, target_width, target_height):
    source_image = CoreGraphics.CGImageImport(CoreGraphics.CGDataProviderCreateWithFilename(source_image_file))

    source_width = source_image.getWidth()
    source_height = source_image.getHeight()

    source_image_rect = CoreGraphics.CGRectMake(0, 0, source_width, source_height)
    new_image = source_image.createWithImageInRect(source_image_rect)

    colors_space = CoreGraphics.CGColorSpaceCreateDeviceRGB()
    colors = CoreGraphics.CGFloatArray(5)
    context = CoreGraphics.CGBitmapContextCreateWithColor(target_width, target_height, colors_space, colors)
    context.setInterpolationQuality(CoreGraphics.kCGInterpolationHigh)
    new_image_rect = CoreGraphics.CGRectMake(0, 0, target_width, target_height)
    context.drawImage(new_image_rect, new_image)
    context.writeToFile(target_image_file, CoreGraphics.kCGImageFormatJPEG)



def main(argv):
    for image_file_name in argv:
        resizeimage(image_file_name, image_file_name, RESIZE_WIDTH, RESIZE_HEIGHT)

if __name__ == "__main__":
    main(sys.argv[1:])

RESIZE_WIDTHRESIZE_HEIGHT设置为您需要的任何值。

将其添加为Run Shell Script步骤:

enter image description here

并将Shell设置为/use/bin/python,将Pass input设置为as arguments

编辑:使用sips有一种相对简单的方法(epatel建议)来实现这一目标。

您可以使用以下命令创建Run Shell Script步骤:

sips --resampleHeightWidth 100 100 "$@"

注意:Double Quotes需要包围$ @,否则您将收到错误。

enter image description here

答案 1 :(得分:2)

当然,使用“裁剪图像”操作并根据需要设置“裁剪前缩放”,“宽度”和“高度”选项。您可能还会发现“Pad Images”操作很有用。

相关问题