【问题标题】:colour count of an image图像的颜色计数
【发布时间】: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

the result of the above code

我需要帮助找到获得颜色计数的图像。我找到了一种通过 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


    【解决方案1】:

    使用GetPixel() method 获取用于每个像素的颜色,然后计算您遇到的唯一颜色:

    $hashset = [System.Collections.Generic.HashSet[System.Drawing.Color]]::new()
    
    foreach($x in 0..($image.Width - 1)){
      foreach($y in 0..($image.Height - 1)){
        [void]$hashset.Add($image.GetPixel($x, $y))
      }
    }
    
    Write-Host "Image has $($hashset.Count) unique colors"
    

    您可以将此例程添加到现有函数中,如下所示:

    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       
        }   
    }
    

    现在你可以这样做:

    dir C:	est*.png -Recurse | Get-Image | ft Filename, Width, Height, PixelFormat, ColorCount -AutoSize
    

    【讨论】:

    • 您好,感谢您的快速回复。很抱歉提出愚蠢的问题,但我该如何使用该代码?它可以用于整个文件夹吗?我尝试阅读您链接的文档,但它超出了我的理解,我无法理解。对不起
    • 此代码适用于与脚本中的 $image 变量相同类型的单个对象。
    • 嗨所以这段代码有效,但我发现了一个问题。问题:Photoshop 滤镜中的颜色计数差异(电报颜色计数)和新代码。原因:Photoshop 过滤器只计算颜色(没有 alpha),而 PowerShell 脚本计算像素(有 alpha)。有没有办法让它不计算阿尔法?
    猜你喜欢
    • 2011-07-06
    • 2011-10-28
    • 2012-07-07
    • 2013-10-02
    • 2012-10-28
    • 2010-09-12
    • 2019-10-29
    • 2023-04-08
    • 2020-04-27
    相关资源
    最近更新 更多