[AppleScript] Apply an effect to one layer in a series of images

Discuss and share Pixelmator Pro resources such as templates, mockups, and tutorials.
User avatar

2020-10-16 16:47:29

Here's a quick script to answer this question on Twitter:
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			make new gaussian effect at the beginning of effects of layer 2 with properties {radius:10}
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
User avatar

2020-10-16 21:00:38

This is great! I've updated based on the twitter conversation to use the Duo effect preset as noted below. Is there a way to add a step that resizes the image to a set dimension without distorting the image? So, say I wanted everything to be cropped to 1500x500 with a center alignment? Is this possible?
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			apply effects preset layer 2 name "Duo"
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
User avatar

2020-10-16 21:32:53

by Justin Cox 2020-10-16 18:01:46 This is great! I've updated based on the twitter conversation to use the Duo effect preset as noted below. Is there a way to add a step that resizes the image to a set dimension without distorting the image? So, say I wanted everything to be cropped to 1500x500 with a center alignment? Is this possible?
Sure, I think the best option would be to resize the canvas using the resize canvas command. BUT... I just remembered there's currently a bug with that command — when setting the width/height, the first value is used for both the width and height. We've fixed this internally but the update hasn't yet been released. If everything was working correctly, you'd be able to add this line at the end of the currentImage tell block:
resize canvas width 1500 height 500 anchor position middle center
However, this will currently get you a 1500x1500 square image. Luckily, when "relative" resizing is used, the bug doesn't rear its head, so you can just add this line after the one above to take away 1000 pixels from the height of the image:
resize canvas width 0 height -1000 anchor position middle center relative yes
Oh, and if your images are all the same size, you could use just the command above. Say, if they're 2000x1000, you could set the canvas size width to -500 and the height to -500. But if they're different sizes, here's what the full script would look like:
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			apply effects preset layer 2 name "Duo"
			resize canvas width 1500 height 1500 anchor position middle center
			resize canvas width 0 height -1000 anchor position middle center relative yes
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
Hope that helps!
User avatar

2020-10-16 23:12:15

This is great help, though instead of resizing the canvas, I also what to resize the image. So I changed the string to this:
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			apply effects preset layer 2 name "Duo"
			resize image width 2048 height 1080 resolution 72 algorithm lanczos
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
Which is almost perfect, but it stretches the image if the ratios are off. If I do the resize canvas command, it just crops the image to the middle, which isn't what I'm going for. I want to resize the canvas to the dimensions and resize the image to match without stretching the dimensions of the image. Does that make sense?
User avatar

2020-10-17 10:48:10

I think I understand what you mean – you'd like to resize the image to be 1500 pixels wide and at this point, presumably, it can be any amount of pixels tall. So then you'd like to crop away the top and bottom part of the image, leaving it 1500 pixels wide by 500 pixels tall? If so, here's what the resizing part of the script would be:
resize image width 1500 algorithm lanczos
resize canvas width 1500 height 1500 anchor position middle center
resize canvas width 0 height -1000 anchor position middle center relative yes
Because I didn't provide a height to the resize image command, it's resized in proportion to the width. Then the other part does the cropping bit.

Of course, if the resize canvas bug wasn't there, you could just say:
resize image width 1500 algorithm lanczos
resize canvas width 1500 height 500 anchor position middle center
However, the first version should keep working once we fix the bug, so you shouldn't have to go and manually update it. Hope that helps!
User avatar

2020-10-17 10:50:13

Oh, and I can see that you're using a different width/height in your most recent script but the general technique would remain the same. Resize to the width/height you need, then resize the canvas to crop away the parts that don't fit in the new aspect ratio.
User avatar

2020-10-17 12:42:04

Brilliant! That works! I was chaining them wrong. THANK YOU! Will the canvas bug be fixed with a future update of Pixelmator or is an update to Script Editor?
User avatar

2020-10-19 09:28:05

The bug will be fixed with a future update to Pixelmator Pro, but, as I mentioned, your script will keep working even after that update because it's designed to work in both circumstances.
User avatar

2020-10-27 03:14:38

Is Threshold Effects not AppleScriptable? Most Effects seem to be scriptable. If it was scriptable, I would expect something like below to work:

tell application "Pixelmator Pro"
	
	tell the first layer of the front document to make new threshold effect at the beginning of effects with properties {50}
	
end tell

User avatar

2020-10-27 14:58:33

It isn't scriptable at this time – you can see the list of all the scriptable effects in the app dictionary (specifically, the Effects Suite). I'll definitely add this as a feature request but, in the meantime, if you don't necessarily need to modify the properties of the effect, you could create an effect preset containing just the effect, give it a name, and apply it using the apply effects preset command. In theory, if you need variable properties, you could even create a number of presets with names containing a number corresponding to the amount value, basically making the Threshold effect scriptable using this workaround. Also, just out of interest, how are you using this command in your scripts?
User avatar

2020-10-28 20:09:01

I am trying to recreate a process I have used in Photoshop. I have written an AppleScript that converts a full color photo to an image that replicates an etching. The color photo is first converted to RGB grayscale where the threshold setting is adjusted and converted to line art. This process is repeated with various threshold settings which are stored on separate layers. When the desired result is achieved, the image is flattened and converted to a true grayscale image. The image mode is then converted to a bitmap image. As a grayscale bitmap, the image has only 2 colors — either 100% black or 100% white. When the image is placed in Adobe InDesign, the white pixels are transparent. Also during the conversion from grayscale to bitmap, the image can be converted to a true halftone image where you can specify various dither methods or halftone. When converting to a true halftone, you have the ability of setting the line/dot frequency, angle and dot shape. That allows you to create custom color separations.

I was screen printing the image onto T-shirts, so I only needed to save my images using a dither option. But printing true halftone and color separations are valuable features if you want to print grayscale or color photos with silkscreen or conventional methods. I should note that line art and halftone images saved in PNG and Photoshop formats also have the benefit of having white backgrounds print as transparent.

Note that I sent a feature request to Aurelija of your support team, along with additional usage samples.

I’ll try the work around — thank you.

Image