Resizing images to specific pixel width and height in Automator
Asked Answered
F

2

5

I've been searching for about an hour now to try and find a way to do this.

Is there any way in Automator to resize images to a specified width and height, instead of just the longest size? I need my output images to be a specific pixel width and height, even if it is disproportionate.

I have photoshop, so is there a way to get Automator to do this?

Thanks

Fiduciary answered 7/2, 2013 at 17:14 Comment(1)
How about: maclife.com/article/howtos/how_batchresize_images_automatorReisfield
W
15

There is no way to do what you need with a standard Automator step.

Anyway, you can use this Python script:

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:])

Set RESIZE_WIDTH and RESIZE_HEIGHT to whichever value you need.

Add it as a Run Shell Script step:

enter image description here

and set Shell to /use/bin/python and Pass input to as arguments.

EDIT: there's a rather simpler way (suggested by epatel) to achieve this, using sips.

You can create a Run Shell Script step with this command:

sips --resampleHeightWidth 100 100 "$@"

Note: Double Quotes need to surround $@ or you will get an error.

enter image description here

Wretched answered 8/2, 2013 at 8:14 Comment(3)
Could have used sips too, a command line tool available on OSX.Lambaste
Thanks for this answer...really saved hours of my time.Dedans
Actually, it is possible directly in Automator. See my answer.Handclap
H
4

Sure, use the "Crop Images" action and set the "Scale before crop", "Width" and "Height" options as desired. You might also find the "Pad Images" action helpful.

Handclap answered 6/11, 2014 at 11:37 Comment(1)
This exactly answers the question to my eyes and was the answer I was looking for! Thanks!Hyperextension

© 2022 - 2024 — McMap. All rights reserved.