Automate Image Processing using Graphics Mill and PowerShell

Today I encountered a task - I needed to resize and apply auto level on a batch of images. In the past, I would just open Gimp or Photoshop and process those images one by one (fortunately I never had to do it on more than a couple of dozens of images). However this time I had way too many files and I thought to myself - we are doing a top-notch imaging software, why not to use it to make my life easier?

I heard that it is possible to use .NET components with PowerShell which is created for the tasks like that, but I did not have any experience with it before. I used to write some .bat files and even creates some WSH scripts in the past, so I prepared to the worst, but after reading a couple of tutorials, I was really amazed how easy to do it! So I could not resist to a temptation to share my experience here in my blog.

As you can see the code is very clear and straghtforward. The PowerShell syntax is very easy to understand (comparing to a quite complicated C#). Just insert a single line to connect Graphics Mill to the script and you are ready to use it!

If you would like to play with it, just create a plain text file:

resize.ps1

param([String]$inputDir, [String]$outputDir)

If (!$inputDir) {
	echo "inputDir is not specified."
	Exit
}

If (!$outputDir) {
	echo "outputDir is not specified."
	Exit
}

[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Aurigma\Graphics Mill 7 SDK\.Net 3.5\Binaries_x64\Aurigma.GraphicsMill.dll")

Get-ChildItem $inputDir -Filter *.jpg | `
Foreach-Object{
	write-host $_.FullName
	
	Try
	{
		$bitmap = new-object Aurigma.GraphicsMill.Bitmap $_.FullName
		$bitmap.Transforms.Resize(640, 640, [Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode]::High, [Aurigma.GraphicsMill.Transforms.ResizeMode]::Fit)
		$bitmap.ColorAdjustment.AutoLevels()
		$bitmap.Save($outputDir + "\" + $_.Name)	
		
		write-host "Success" -foreground "green"			
	}
	Catch
	{
		write-host "Error" -foreground "red"	
	}
	Finally
	{
		If ($bitmap)
		{
			$bitmap.Dispose()	
		}
	}
}

Then run a PowerShell and execute it like this:

PS C:\> .\resize.ps1 <inputDir> <ouputDir>