使用PowerShell获取图像的调色板

时间:2017-10-06 21:26:12

标签: windows powershell colors color-palette

我正在尝试获取图片的Color Palette。我尝试了各种方法,现在我在PowerShell中使用了以下代码,但是我无法得到正确的结果:

$filename = "C:\Users\schoo\Desktop\bb.jpg"
$BitMap = [System.Drawing.Bitmap]::FromFile((Get-Item $filename).fullname) 

Foreach($y in (1..($BitMap.Height-1))){ 

  Foreach($x in (1..($BitMap.Width-1))){ 

         $Pixel = $BitMap.GetPixel($X,$Y)         
         $BackGround = $Pixel.Name
         }

$R = $Pixel | select -ExpandProperty R
$G = $Pixel | select -ExpandProperty G
$B = $Pixel | select -ExpandProperty B
$A = $Pixel | select -ExpandProperty A

$allClr = "$R" + "." + "$G" + "." + "$B" + "." + "$A"
$allClr  
}

结果带给我超过一千个RGB代码: wrong result

1 个答案:

答案 0 :(得分:1)

我认为“调色板”是指图像中出现的不同颜色。

仅选择集合的不同子集的简单(且非常快)方法是使用哈希表。

$filename =  'C:\Users\schoo\Desktop\bb.jpg'
$BitMap = [System.Drawing.Bitmap]::FromFile((Resolve-Path $filename).ProviderPath)

# A hashtable to keep track of the colors we've encountered
$table = @{}
foreach($h in 1..$BitMap.Height){
  foreach($w in 1..$BitMap.Width) {
    # Assign a value to the current Color key
    $table[$BitMap.GetPixel($w - 1,$h - 1)] = $true
  }
}

# The hashtable keys is out palette
$palette = $table.Keys