見出し画像

PowerShell 画像ファイルからサイズ、形式、カラー情報を取り出す

PowerShellで画像ファイルから画像サイズ、ファイル形式、カラー情報を取り込むサンプル


#画像ファイルから画像サイズ、ファイル形式、カラー情報を取り込むサンプル

add-type -AssemblyName System.Drawing

# ファイルが保存されているフォルダ
$filePath = 'C:\temp'

# 拡張子で絞り込む
$include = @("*.jpg","*.JPG","*.jpeg","*.png","*.PNG","*.bmp","*.BMP","*.gif","*.GIF")

# ファイル一覧を取得
$files = Get-ChildItem -Path $filePath -Include $include -Recurse -File
 
foreach($file in Get-ChildItem $files){
	try{
		$img=[System.Drawing.Image]::FromFile($file.fullname)
		$resolution = "$($img.Width)" + "x" + "$($img.Height)"

		if ( ($img.RawFormat ) -eq [Drawing.Imaging.ImageFormat]::Jpeg) {
			$resolution += " JPG"
		} elseif ( ($img.RawFormat) -eq [Drawing.Imaging.ImageFormat]::Png) {
			$resolution += " PNG"
		} elseif ( ($img.RawFormat) -eq [Drawing.Imaging.ImageFormat]::Bmp) {
			$resolution += " BMP"
		} elseif ( ($img.RawFormat) -eq [Drawing.Imaging.ImageFormat]::Gif) {
			$resolution += " GIF"
		} else {
			$resolution += " ファイル形式不明"
		}

		if ( ($img.PixelFormat) -eq "Format1bppIndexed") {
			$resolution += " モノクロ2階調"
		} elseif ( ($img.PixelFormat) -eq "Format8bppIndexed") {
			$resolution += " 256色"
		} elseif ( ($img.PixelFormat) -eq "Format24bppRgb") {
			$resolution += " 1677万色24ビットカラー"
		} elseif ( ($img.PixelFormat) -eq "Format16bppGrayScale") {
			$resolution += " 65,536階調モノクロ16ビット"
		} elseif ( ($img.PixelFormat) -eq "Format32bppArgb") {
			$resolution += " 1677万色24ビットαブレンド"
		} else {
			$resolution += " $($img.PixelFormat)"
		}
		
		$img.Dispose() #オブジェクトを廃棄
		write-host $file " : " $resolution
	}
   catch{
		# 例外処理
	}
}


実行するとc:\tempの中の画像ファイルのサイズ、形式、カラー情報が表示されます。



#PowerShell #コマンドレット #プログラミング初心者 #プログラミング学習 #画像ファイル #Windows #System .Drawing


この記事が気に入ったらサポートをしてみませんか?