How do I automate rescaling an image to fit in a square, leaving result square with transparency?

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

2020-06-29 14:15:54

I need to convert a large number of images to be 512x512. Each source image will be scaled proportionally to fit in 512x512, but I want the result to be 512x512 so I need to fill the unused space with transparent pixels. I don't see an obvious way to do that with the Automator actions provided. Can someone tell me what I have to do? While I'm a software engineer I find AppleScript totally inscrutable.
User avatar

2020-06-29 14:44:35

I found my answer using Pixelmator non-Pro actions. Posting it here for others to use.

1: Get Selected Finder Items
2: Copy Finder Items. To=my chosen output folder
3: Pixelmator: Change Type of Images. To Type=PNG.
4: Rename Finder Items: Make Sequential
5: Pixelmator: Fit Images. Width=512, Height=512, Scale proportionally=ON.
6: Pixelmator: Crop Images. Width=512, Height=512, Units=pixels, Anchor=center, Relative=OFF, Background Color=OFF.

The PNG conversion ensures the format supports transparency. The Crop step leaves the BG color off so transparency is used to fill to square.

I didn't see a Pro action for cropping, which is why I went with non-Pro for this workflow.
User avatar

2020-06-29 15:16:13

Yep, that should do it! And yes, there currently isn't a Crop Images action for Pixelmator Pro — we'll see what we can do about that.

P.S. This would also be possible using AppleScript, though it would take a little longer to write the script itself. I'm kind of intrigued by the challenge though, so I'll see if I can whip something up...
User avatar

2020-06-30 12:05:25

Alright, here's that script! First, a screenshot:
Image
And the downloadable script itself:

http://upload-cdn.pixelmator.com/ConvertTo512-10630.zip

Obviously, this took a lot longer than using Automator but it does come with some benefits – it's more customizable and more easily shareable, which is pretty cool. And, of course, it can be adapted to more complex workflows (or even used in Automator itself).

P.S. Our devs also found AppleScript to be quite a confusing thing at first but, as a non-programmer with a teeny bit of programming knowledge, I have to say I'm a little hooked on AppleScript. :sweat_smile:

P.P.S. I'm totally not an expert so certain things could probably be optimized (and I wrote out some extra code + used some extra variables to make the script easier to read) but, as far as I've tested, everything seems to work just fine!
User avatar

2021-03-22 00:40:28

Excellent!!! Thanks for script. How to keep the image title(from process folder to export folder) with this code?
User avatar

2021-03-31 09:21:00

Hi Stefanos, apologies for missing your comment! Here's the script that would do that:
tell application "Pixelmator Pro"
	activate
	set sourceFolder to (choose folder with prompt "Choose the folder you'd like to process:")
	set exportFolder to (choose folder with prompt "Choose where you'd like to export the processed images:")
	
	tell application "Finder"
		set filesToProcess to every file in folder sourceFolder
	end tell
	
	set imageWidth to 512
	set imageHeight to 512
	
	repeat with currentFile in filesToProcess
		set currentDocument to open (currentFile as alias)
		tell currentDocument
			resize canvas width imageWidth height imageHeight
			if (width of layer 1) > (height of layer 1) then
				set the width of layer 1 to imageWidth
				set y to (imageHeight - (height of layer 1)) / 2
				set the position of layer 1 to {0, y}
			else if (height of layer 1) > (width of layer 1) then
				set the height of layer 1 to 512
				set x to (imageWidth - (width of layer 1)) / 2
				set the position of layer 1 to {x, 0}
			else if (width of layer 1 = height of layer 1) then
				set the height of layer 1 to 512
				set the position of layer 1 to {0, 0}
			end if
		end tell
		export currentDocument to ((exportFolder as string) & name of currentDocument & ".png") as PNG
		close currentDocument without saving
	end repeat
end tell

tell application "Finder"
	activate
	open folder exportFolder
end tell
User avatar

2021-10-29 22:01:11

Helo!!
User avatar

2021-11-03 15:57:41

Hi Stefanos! Could you explain what exactly your scripts should do – i.e. what they take as input and what they produce as output? Because I could try and guess, but I could be wrong and it would generally be much easier to get a good idea of your workflow with that information. :sweat_smile:

Thanks in advance!
User avatar

2021-11-03 18:56:23

by Andrius 2021-11-03 13:57:41 Hi Stefanos! Could you explain what exactly your scripts should do – i.e. what they take as input and what they produce as output? Because I could try and guess, but I could be wrong and it would generally be much easier to get a good idea of your workflow with that information. :sweat_smile:

Thanks in advance!
It is ok pixelmator support send me , Thanks very much!!
User avatar

2021-11-03 19:28:45

What I ended up doing was using Automator (still a thing) with this workflow:

Create a workflow that receives image files.
1) (Pixelmator Pro) Scale images to 1000x1000 (scaled proportionally).
2) (Pixelmator Pro) Change type of images (to PNG).
3) (Pixelmator) Crop images (center anchor, to 1000x1000).

Step 3 was unfortunate because there is no Pixelmator Pro action for cropping in Automator.