BarryWalsh
BarryWalsh

Reputation: 1340

Batch resizing images in OSX Automator white background instead of black

I'm using Automator to resize a bunch of images. It works great apart from the way it adds a black background to PNG images. This is a real issue if the image was a black logo with a transparent background. Is there any way to change the background colour?

I'm using the Crop Images action.

Cheers!

Upvotes: 1

Views: 1801

Answers (1)

Jon
Jon

Reputation: 1479

Automator is very easy to use but for more finite control, you can use AppleScript (even as part of Automator) with Image Events to do some basic image editing including resizing using the pad command with specific dimensions and background color. Here's a basic script that you can save as an application in Script Editor and then, to use it, either double-click the app icon in the Finder or, better, drop the image files to process on the application icon:

property _width : 400
property _height : 200
property _color : {65528, 65535, 65525} --white

on run
    open (choose file with multiple selections allowed)
end run

on open the_items
    set output_folder to (((path to desktop) as string) & "Output")
    try
        get output_folder as alias
    on error
        tell application "Finder" to make new folder at desktop with properties {name:"Output"}
    end try
    repeat with this_item in the_items
        set this_item to (this_item as alias)
        set _info to info for this_item
        set _extension to _info's name extension
        if (_extension is in {"jpg", "jpeg", "tif", "tiff", "png"}) then
            set _name to name of _info
            set _name to (text 1 thru -((count _extension) + 1) of _name)
            set output_path to (output_folder & ":" & _name & "jpg")
            my resize_image(this_item, output_path, _width, _height)
        end if
    end repeat
end open

on resize_image(original_path, output_path, _width, _height)
    tell application "Image Events"
        launch
        set _image to (open file (original_path as string))
        pad _image to dimensions {_width, _height} with pad color _color -- use crop to dimensions to decrease the size
        save _image as JPEG in file output_path with icon
        close _image
    end tell
end resize_image

This will create a folder on your Desktop named Output (if one does not already exist) and save the images as JPGs there using the dimensions at the top of the script (you can modify the dimensions, color, output location, etc., this is just an example).

Upvotes: 2

Related Questions