ペイントで色を置換する作業が手間なので、効率化を考える。
Officeでの「色の変更」や「透明色を指定」は使用しない。
. { # [テスト環境]# PowerShell 5.1, Windows 11 (2025年1月頃)# [スクリプトファイル(.ps1)]# <ファイル名># "testConvertColor.ps1"# <エンコード># UTF-8 (BOM付き)# [ショートカット(.lnk)]# <リンク先(T)># PowerShell.exe -NoLogo -NoExit -NoProfile -ExecutionPolicy RemoteSigned -File ".\testConvertColor.ps1"# <作業フォルダー(S)># ""$Host.UI.RawUI.WindowTitle ="画像の色を変更"$ErrorActionPreference=[System.Management.Automation.ActionPreference]::Stop Set-StrictMode -Version Latest Add-Type -AssemblyName PresentationFramework Add-Type -AssemblyName System.Drawing # 画像ファイルのパス$filePath="$env:USERPROFILE\Desktop\test.tif"# 画像ファイルのパスを変更& { $wnd=New-Object -TypeName System.Windows.Window -Property @{ Width =600 Height =100 Title ="画像ファイルのパス" Content =New-Object -TypeName System.Windows.Controls.TextBox -Property @{ FontFamily =[System.Windows.Media.FontFamily]::new("Cascadia Mono") FontSize =16 Text =$filePath } } $wnd.Content.add_TextChanged( [System.Windows.Controls.TextChangedEventHandler] { param([object]$s,[System.Windows.Controls.TextChangedEventArgs]$e) $script:filePath=$s.Text <# # スコープの確認 @("Global", "Local", "Script").ForEach({ $s = $_; Get-Variable -Scope $s | Select-Object -Property @{ Name = "Scope"; Expr = { $s } }, * }) | Sort-Object -Property Name, Scope | Where-Object Name -Match 'filePath' | Out-GridView #> } ) $null=$wnd.ShowDialog() } $fileOrig=[System.IO.File]::Open( $filePath,[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::ReadWrite -bor[System.IO.FileShare]::Delete ) $bmpOrig=[System.Drawing.Bitmap]::new($fileOrig) $bmpNew=[System.Drawing.Bitmap]::new( $bmpOrig.Width,$bmpOrig.Height,[System.Drawing.Imaging.PixelFormat]::Format32bppArgb ) # 解像度を設定$bmpNew.SetResolution($bmpOrig.HorizontalResolution,$bmpOrig.VerticalResolution) # 指定した色を透明にする# * HorizontalResolution と VerticalResolution が 96 になる# * PixelFormat が Format32bppArgb になる$bmpOrig.MakeTransparent([System.Drawing.Color]::FromArgb(255,255,255)) $imgAttr=[System.Drawing.Imaging.ImageAttributes]::new() $imgAttr.SetColorMatrix( [System.Drawing.Imaging.ColorMatrix]::new( [float[][]]@( [float[]]@( 1,0,0,0,0),[float[]]@( 0,1,0,0,0),[float[]]@( 0,0,1,0,0),[float[]]@( 0,0,0,1,0),[float[]]@(0.75,0,0,0,1) ) ),[System.Drawing.Imaging.ColorMatrixFlag]::Default,[System.Drawing.Imaging.ColorAdjustType]::Bitmap ) $g=[System.Drawing.Graphics]::FromImage($bmpNew) $g.DrawImage( $bmpOrig,[System.Drawing.Rectangle]::new(0,0,$bmpOrig.Width,$bmpOrig.Height),0,0,$bmpOrig.Width,$bmpOrig.Height,[System.Drawing.GraphicsUnit]::Pixel,$imgAttr ) $imgAttr.Dispose() $g.Dispose() $bmpOrig.Dispose() $fileOrig.Dispose() $bmpNew.Save( "$env:USERPROFILE\Desktop\testNew.png",[System.Drawing.Imaging.ImageFormat]::Png ) $bmpNew.Dispose() }