本帖最后由 flashercs 于 2019-3-28 06:55 编辑
用powershell找色也行吧.设 RGB中至少两个原色不同的点为彩色点,当图片底部的1/3区域中的彩色点数量达到10%时 ,该图片被认为是彩图.
下面代码并未真正重命名图片,只是找出符合条件的图片; 保存为 "彩图".bat,设置变量PicturePath为要处理的图片或所在目录的路径列表,以空格分隔开- @echo off
- set PicturePath="E:\test\dir1" "E:\test\dir2" "E:\test\dir3" "E:\test\dir4\00053.jpg"
- for /f "tokens=1 delims=:" %%A in ('findstr /n "#######*" %0') do more +%%A %0 >"%~dpn0.ps1"
- powershell.exe -ExecutionPolicy Bypass -File "%~dpn0.ps1" %PicturePath%
- pause
- exit /b
- ################################################################
- [boolean]$isRecursive = $false # 是否处理子目录
- function testColor {
- param (
- [string]$filepath
- )
- $bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $filepath
- # $bitmap = [System.Drawing.Bitmap]::new($filepath)
- $height = $bitmap.Height
- $width = $bitmap.Width
- # 彩色点数目下限设置为图片底部1/3的点数的10%
- $minPoints = [System.Math]::Floor(0.1 * $width * $height / 3)
- $count = 0
- $flag = $false
- :outer
- for ($y = [System.Math]::Ceiling($height * 2 / 3) - 1; $y -lt $height; $y++) {
- for ($x = 0; $x -lt $width; $x++) {
- $color = $bitmap.GetPixel($x, $y)
- if (!($color.R -eq $color.G -and $color.R -eq $color.B)) {
- if (++$count -ge $minPoints) {
- $flag = $true
- break outer
- }
- }
- }
- }
- # Write-Host "`$y = $y;`$x = $x" -ForegroundColor Magenta
- $bitmap.Dispose()
- return $flag
- }
- function genFile {
- param (
- [string]$filepath
- )
- Write-Host $filepath -ForegroundColor Green
- if (testColor $filepath) {
- Rename-Item -LiteralPath $filepath -NewName ([System.IO.Path]::GetFileNameWithoutExtension($filepath) + 'C' + [System.IO.Path]::GetExtension($filepath)) -WhatIf
- }
- }
- function genFolder {
- param (
- [string]$folderPath,
- [switch]$recurse
- )
- if ($recurse) {
- Get-ChildItem -LiteralPath $folderPath -Filter '*.jpg' -File -Recurse|ForEach-Object {genFile $_.FullName}
- }
- else {
- Get-ChildItem -LiteralPath $folderPath -Filter '*.jpg' -File|ForEach-Object {genFile $_.FullName}
- }
- }
- Add-Type -AssemblyName System.Drawing|Out-Null
- Write-Host "`$args.Count=$($args.Count)"
- foreach ($item in $args) {
- if ((Get-Item -LiteralPath $item).Attributes -band [System.IO.FileAttributes]::Directory) {
- genFolder $item $isRecursive
- }
- else {
- genFile $item
- }
- }
复制代码
|