Powershell – Resize Image Files

Powershell 로 이미지 파일을 Resize 해 보자.

Parameter는 입력 파일 경로, 출력 파일 경로 그리고 Width, Height 사이즈만 있으면 된다.

System.Drawing을 사용하였는데 기본적으로 Load 되어 있는 Assembly가 아니므로 Load 해 줘야 한다.

아래는 여러가지 방법중 하나 이다. .Net 을 사용한 Image Resize 코드들을 찾아 보면 여러 가지 방법이 있으니 다른 방법도 Powershell로 다시 써보면 재미 있을 것이다.

Resize-Image

Function Resize-Image
{
	param(
		[String]$InputFile,
		[String]$OutputFile,
		[int]$Width,
		[int]$Height
	)

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

    $OriginImage = [System.Drawing.Bitmap]::FromFile($InputFile)

    $ResizedImage = New-Object System.Drawing.Bitmap @($Width, $Height)

    $graphics = [System.Drawing.Graphics]::FromImage($ResizedImage)

    $graphics.DrawImage($OriginImage, 0, 0, $Width, $Height)

    $graphics.Dispose()

    $ResizedImage.Save($OutputFile)
}

이 Function을 이용하여 원하는 Directory 내에 있는 File들을 모두 Resize 하기 위해서는 다음과 같이 응용한다.

Reset-ImageFilesSize.ps1

param($InputDirectory, $OutputDirectory, $Width, $Height)

New-Item -ItemType Directory -Path $OutputDirectory -Force

$InputFiles = ls $InputDirectory | ? {$_.Extension -eq ".jpg"} |
	%{Resize-Image $_.FullName (Join-Path $OutputDirectory $_.Name) $Width $Height}

Extension을 .jpg로 고정 했는데 Parameter로 받아도 좋을 것이다.

Verb 를 Resize로하고 싶었는데 Get-Verb 범위에서 벗어나 Reset으로 하였다.

답글 남기기

이메일 주소는 공개되지 않습니다.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.