【发布时间】:2022-10-24 19:29:52
【问题描述】:
我正在尝试为旧游戏“Recoil”制作高档纹理模型。
为此,我需要找到高度、宽度、像素格式和颜色计数1000 张图像,这样我就可以将这些信息提供给 excel 并找到要放大的最佳纹理。
我已经能够得到高度、宽度和像素格式通过 PowerShell 脚本,然后我可以将其复制到 excel,因为该脚本提供了一个表格。该脚本适用于整个文件夹。
Function Get-Image{
Param(
[Parameter(ValueFromPipeline=$true)]
[System.IO.FileINfo]$file
)
begin{
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") |Out-Null
}
process{
if( $file.Exists){
$img=[System.Drawing.Image]::FromFile($file)
$image=$img.Clone()
$img.Dispose()
$image | Add-Member `
-MemberType NoteProperty `
-Name Filename `
-Value $file.FUllname `
-PassThru
}else{
Write-Host "File not found: $file" -fore yellow
}
}
end{}
}
dir C:\test\*.png | Get-Image
dir C:\test\*.png -Recurse | Get-Image | select filename, Width, Height, PixelFormat | ft -auto
我需要帮助找到获得颜色计数的图像。我找到了一种通过 Photoshop 过滤器手动完成的方法,但这并不是处理所有图像的可行方法。 photoshop filter example
如果我能得到颜色计数以与代码类似的方式提供它是最好的。
编辑:我需要一种方法来获得颜色计数的文件夹中的所有图像.
图像本身很小(最大的是 512x512)。我只需要颜色的数量,不需要RGB的分解。
ps-我实际上对编程和脚本一无所知(即使是 Reddit 帮助我解决的上述脚本)
希望我能够清楚地解释我的查询。 感谢您的时间和考虑。
编辑 2所以这段代码有效,但我发现了一个问题。有没有办法让它不计算阿尔法?问题:Photoshop 过滤器(电报颜色计数)和新代码中的颜色计数差异。原因:Photoshop 过滤器仅计算颜色(不带 alpha),而 PowerShell 脚本计算像素(带 alpha)。 Format32bppArgb - 有问题 Format24bppRgb - 它计算得很好。 以下是当前代码
Function Get-Image{
Param(
[Parameter(ValueFromPipeline=$true)]
[System.IO.FileINfo]$file
)
begin{
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") |Out-Null
}
process {
if ($file.Exists) {
# Load image
$img = [System.Drawing.Image]::FromFile($file)
$image = $img.Clone()
$img.Dispose()
# Count colors
$colorSet = [System.Collections.Generic.HashSet[System.Drawing.Color]]::new()
foreach ($x in 0..($image.Width - 1)) {
foreach ($y in 0..($image.Height - 1)) {
[void]$colorSet.Add($image.GetPixel($x, $y))
}
}
# Add file name and color count properties to image object
$fileNameProp = @{ MemberType = 'NoteProperty'; Name = 'Filename'; Value = $file.FullName; PassThru = $true}
$colorCountProp = @{ MemberType = 'NoteProperty'; Name = 'ColorCount'; Value = $colorSet.Count; PassThru = $true}
$image | Add-Member @fileNameProp | Add-Member @colorCountProp
}else{
Write-Host "File not found: $file" -fore yellow
}
}
end{}
}
dir D:\Games\Setups\RECOIL_fixed_edition_v0.5\SourceFile\zbd\Dataset_D\Dataset_D\ammoarcgun\*.png | Get-Image
dir D:\Games\Setups\RECOIL_fixed_edition_v0.5\SourceFile\zbd\Dataset_D\Dataset_D\ammoarcgun\*.png -Recurse | Get-Image | select filename, Width, Height, PixelFormat, ColorCount | ft -auto
【问题讨论】:
标签: image powershell colors metadata