Picture modification with PowerShell

What can we do if we need to resize, crop more than 2000 picture? Later it needs to repeat this task regularly, as a scheduled task. The answer is automatizing with PowerShell on Windows OS. I don’t like add-ons or 3rd party things importing into a corporate environment. I decided to write my code without any 3rd party things.

Firs I ask for help from .NET, because the PowerShell easily use. I loaded the .NET Framework System.Drawing class.

$a = [reflection.assembly]::LoadWithPartialName("System.Drawing")

After this I can modify any pictures. I got the following modify rules:

  • The final picture width and height must be 96×96 pixel
  • Portrait images must to insert on a white square
  • Landscape images must to cut to square

 

Here is my picture modification function:

Function ReDraw-Picture{
	Param (
		$Sourcepic,
		$DriveLetter
	)
	
	Write-Host 'ReDraw-Picture function started'
	$SizeLimit= 96
	$Source= $DriveLetter + $Sourcepic.Name
	$Destination= $Global:TempDir +"\"+ $Sourcepic.Name
	$OldBitmap = new-object System.Drawing.Bitmap $Source
	$text1 = $Sourcepic.Name
	$text = "  ReDrawing $text1, save as $Destination"
	Write-Host $text
	if ($OldBitmap.Height -gt $SizeLimit) { 
	 	if ($OldBitmap.Width -lt $OldBitmap.Height) {
	        $newH=$SizeLimit
        	$newW=[int]($OldBitmap.Width*$SizeLimit/$OldBitmap.Height)
			$offset = [int] (($newH - $newW)/2)
			$NewBitmap = new-object System.Drawing.Bitmap $SizeLimit,$SizeLimit
			$g=[System.Drawing.Graphics]::FromImage($NewBitmap)
			$g.clear([System.Drawing.Color]::White )
        	$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic 
        	$g.DrawImage($OldBitmap, $offset, 0, $newW, $newH)   
			} 
			else{
				$newH = $SizeLimit
				$newW = [int]($OldBitmap.Width*$SizeLimit/$OldBitmap.Height)
				$offset = [int] (($newW - $newH)/2)
				$NewBitmap = new-object System.Drawing.Bitmap $SizeLimit,$SizeLimit
				$g=[System.Drawing.Graphics]::FromImage($NewBitmap)
        		$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic 
        		$g.DrawImage($OldBitmap, -$offset, 0, $newW, $newH)  
			}
		$NewBitmap.Save($Destination, ([system.drawing.imaging.imageformat]::jpeg))
 		$NewBitmap.Dispose()                                     
        $OldBitmap.Dispose()
		}
	Write-Host 'ReDraw-Picture function finished'
	}


 

The parameters of the ReDraw-Picture function are the picture name, and the picture location. It will save the modified picture into a TempDir directory, not overwrite the original one.

Advertisements

1 thought on “Picture modification with PowerShell”

  1. This always amazes me just how Blog owners for example yourself can find some time along with the commitment to keep on Crafting terrific blog posts your site https://letitknow.wordpress.com/2012/02/23/picture-modification-with-powershell/ is excellent and one of my must read weblogs, I had been more than pleased with the blog post Picture modification with PowerShell Let IT know I simply want to say thanks and also congratulations Kind regards horny

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s