使用 PowerShell 获取图像的调色板
Get Color Palette of Image using PowerShell
我正在尝试获取图像的 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
代码:
我假设 "color palette" 是指图像中出现的 不同 颜色。
仅select集合的一个不同子集的一种简单(且相当快速)的方法是使用哈希表。
$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
我正在尝试获取图像的 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
代码:
我假设 "color palette" 是指图像中出现的 不同 颜色。
仅select集合的一个不同子集的一种简单(且相当快速)的方法是使用哈希表。
$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