OM Wiki

Technical Artist Wiki

ユーザ用ツール

サイト用ツール


powershell:fill_background

画像ファイルに背景を挿入する

sample.ps1
Add-Type -AssemblyName System.Drawing
 
Function Select-Folder(){
  $obj = New-Object -ComObject Shell.Application;
  $f = $obj.BrowseForFolder(0, "フォルダを選択してください。", 0, 0);
  return $f.Self.Path;
}
 
$inputFolder = Select-Folder;
$outputFolder = $inputFolder+"\output"
$backgroundImagePath = $inputFolder+"\Background.png"
$count = 0;
 
#ファイルリスト読み込み
$fileList = Get-ChildItem $inputFolder -filter *.png
 
foreach($file in $fileList) {
 
    #背面画像ファイルの読み込み
    $image1 = New-Object System.Drawing.Bitmap($backgroundImagePath)
 
    #前面画像の読み込み
    $image2 = New-Object System.Drawing.Bitmap($file.FullName)
 
    if($file.FullName -ne $backgroundImagePath) {
 
        #処理カウント
        $count++;
        Write-host "[" $count "/" ($fileList.count-1) "]" $file.Name;
 
        #出力画像の作成
        $rect = New-Object System.Drawing.Rectangle(0,0,[int]($image2.Width), [int]($image2.Height));
        $image1s = $image1.Clone($rect,$image1.PixelFormat)
 
        $image2s = New-Object System.Drawing.Bitmap([int]($image2.Width), [int]($image2.Height))
 
		#解像度設定値 
		#72.009 == 72 dpi, Photoshop: 72.009
 
		$imageResolution = 72.009
        $image2s.SetResolution($imageResolution, $imageResolution)
 
        $gra2 = [System.Drawing.Graphics]::FromImage($image2s)
        $gra2.DrawImage($image1s, (New-Object System.Drawing.Rectangle(0 ,0, $image2.Width,$image2.Height)))
        $gra2.DrawImage($image2, (New-Object System.Drawing.Rectangle(0 ,0, $image2.Width,$image2.Height)))
 
 
        #画像出力/解放
        if(!(Test-Path $outputFolder)) {New-Item -Path $outputFolder -Type directory -Force;}
        $image2s.Save($inputFolder+"\output\"+$file.Name, [System.Drawing.imaging.ImageFormat]::Png)
        $gra2.Dispose()
        $image2s.Dispose()
    }
 
    #画像解放
    $image1.Dispose()
    $image2.Dispose()
}
powershell/fill_background.txt · 最終更新: 2021/05/04 15:09 by ochiaimitsuo

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki