Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing images to specific pixel width and height in Automator

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

like image 501
Glenn Flanagan Avatar asked Feb 07 '13 17:02

Glenn Flanagan


People also ask

How do I resize an image in Automator?

Now anytime you want to resize an image, all you need to do is right-click on it, hover over Quick Actions, and select Image Resize if that's what you name this action or whatever name you named it.


1 Answers

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

like image 168
Riccardo Marotti Avatar answered Sep 22 '22 04:09

Riccardo Marotti