[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 有偿求批量圆形图片变方形!!!!

各位大佬,
求一个能批量把如下图圆形图片无损变方形,方形无损变圆形的批处理,不要那种简单裁切,要不损失图片内容的变形。谢谢!
[img][/img]

先看看版规,然后最好能给出几个测试原文件。

TOP

啊 ,反过来还好说
这圆形变方形 ,涉及图像复原技术 ,建议直接网上搜索图形修复 ,看看有啥现成的东西

TOP

圆形变方形
读取第一个透明度不为0的像素的颜色为背景色
新建个方形,把圆形粘贴上去

QQ 20147578

TOP

回复 4# czjt1234


    是用PS吗?有没有简单一些的软件或者批处理?

TOP

回复 3# Five66


    应该不涉及修复,PS中的变形就可以做到,但比较麻烦

TOP

1. 把当前目录的矩形图片变圆形图片,"矩形变圆形.ps1.bat"
  1. <#*,:
  2. @echo off&cd /d "%~dp0"&set "batchfile=%~f0"
  3. powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
  4. pause
  5. exit /b
  6. #>
  7. # convert rectangle to ellipse
  8. Add-Type -AssemblyName System.Drawing
  9. Get-ChildItem -Filter *.png | Where-Object { $_ -is [IO.FileInfo] } | ForEach-Object {
  10.   try {
  11.     $_.Name
  12.     $bytes = [IO.File]::ReadAllBytes($_.FullName)
  13.     $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes)
  14.     $bmp = New-Object System.Drawing.Bitmap -ArgumentList ($memstream)
  15.     $width = $bmp.Width
  16.     $height = $bmp.Height
  17.     $a = $width / 2
  18.     $b = $height / 2
  19.     $centerX = $width / 2
  20.     $centerY = $height / 2
  21.     foreach ($x in 0..($width - 1)) {
  22.       foreach ($y in 0..($height - 1)) {
  23.         # // 椭圆公式: (x - centerX)^2 / a^2 + (y - centerY)^2 / b^2 > 1
  24.         $equation = [Math]::Pow($x - $centerX, 2) / [Math]::Pow($a, 2) + [Math]::Pow($y - $centerY, 2) / [Math]::Pow($b, 2)
  25.         if ($equation -gt 1) {
  26.           $color = $bmp.GetPixel($x, $y)
  27.           # NOTE:Both System.Drawing and imagemagick will set RGB channel to 0 when alpha channel is 0.
  28.           $transparent_color = [System.Drawing.Color]::FromArgb(1, $color.R, $color.G, $color.B)
  29.           $bmp.SetPixel($x, $y, $transparent_color)
  30.         }
  31.       }
  32.     }
  33.     $bmp.Save($_.FullName)
  34.   } finally {
  35.     if ($memstream) { $memstream.Close() }
  36.     if ($bmp) { $bmp.Dispose() }
  37.   }
  38. }
复制代码
2.把当前目录的圆形图片变矩形图片,"圆形变矩形.ps1.bat"
  1. <#*,:
  2. @echo off&cd /d "%~dp0"&set "batchfile=%~f0"
  3. powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
  4. pause
  5. exit /b
  6. #>
  7. # convert ellipse to rectangle
  8. Add-Type -AssemblyName System.Drawing
  9. Get-ChildItem -Filter *.png | Where-Object { $_ -is [IO.FileInfo] } | ForEach-Object {
  10.   try {
  11.     $_.Name
  12.     $bytes = [IO.File]::ReadAllBytes($_.FullName)
  13.     $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes)
  14.     $bmp = New-Object System.Drawing.Bitmap -ArgumentList ($memstream)
  15.     $width = $bmp.Width
  16.     $height = $bmp.Height
  17.     $a = $width / 2
  18.     $b = $height / 2
  19.     $centerX = $width / 2
  20.     $centerY = $height / 2
  21.     foreach ($x in 0..($width - 1)) {
  22.       foreach ($y in 0..($height - 1)) {
  23.         $equation = [Math]::Pow($x - $centerX, 2) / [Math]::Pow($a, 2) + [Math]::Pow($y - $centerY, 2) / [Math]::Pow($b, 2)
  24.         if ($equation -gt 1) {
  25.           $color = $bmp.GetPixel($x, $y)
  26.           # NOTE:Both System.Drawing and imagemagick will set RGB channel to 0 when alpha channel is 0.
  27.           $transparent_color = [System.Drawing.Color]::FromArgb(255, $color.R, $color.G, $color.B)
  28.           $bmp.SetPixel($x, $y, $transparent_color)
  29.         }
  30.       }
  31.     }
  32.     $bmp.Save($_.FullName)
  33.   } finally {
  34.     if ($memstream) { $memstream.Close() }
  35.     if ($bmp) { $bmp.Dispose() }
  36.   }
  37. }
复制代码
3

评分人数

微信:flashercs
QQ:49908356

TOP

回复 7# flashercs
感谢大佬出手帮忙!我反馈一下结果,圆变方,实验了三张均没有成功。方变圆,实验两张,有一张成功了。
请大佬麻烦再帮忙看看,非常感谢您!敬礼!!

    [img][/img]

[img][/img]

TOP

回复 6# duoduo200


    方形变圆形 ,图片内容就丢失了 ,要变回方形 ,不用图像复原技术怎么把丢失的弄回来???  
复原的话 ,简单点的还好 ,可以自己弄 ,但是复杂点的 ,要保持效果 ,还得用上深度学习
photoshop能弄是因为人家有对应的复原算法将图片修复
另外photoshop有自动化对象 ,叫photoshop.application ,可用vbs ,jscript ,powershell之类进行自动化操作 ,可以网上搜搜看看能不能使用photoshop的自动化对象用vbs ,jscript ,powershell之类自动修复

TOP

传几张图片到网盘给人家测试

QQ 20147578

TOP

回复 7# flashercs


    大佬,这是测试圆形和方形图片样张,谢谢。

链接: https://pan.baidu.com/s/16hkeqbYC8qWt44RDCucvFg?pwd=8mkx 提取码: 8mkx

TOP

回复 10# czjt1234


    感谢指导,谢谢!

TOP

回复 9# Five66


    感谢您的指导!非常感谢&#128591;

TOP

参考7楼的简单写了一下,不过发现你的原图有灰边,处理灰边System.Drawing可能力有未逮。
填充透明像素为矩形:
  1. <#*,:
  2. @echo off&cd /d "%~dp0"&set "batchfile=%~f0"
  3. powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
  4. pause
  5. exit /b
  6. #>
  7. Add-Type -AssemblyName System.Drawing
  8. Get-ChildItem -Path . -Filter *.png -File | ForEach-Object {
  9.     try {
  10.         Write-Host "处理文件: $($_.Name)" -ForegroundColor Cyan
  11.         $bytes = [IO.File]::ReadAllBytes($_.FullName)
  12.         $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes)
  13.         $bitmap = New-Object System.Drawing.Bitmap -ArgumentList ($memstream)
  14.         $grayTolerance = 10
  15.         $colorCount = @{}
  16.         for ($y = 0; $y -lt $bitmap.Height; $y++) {
  17.             for ($x = 0; $x -lt $bitmap.Width; $x++) {
  18.                 $color = $bitmap.GetPixel($x, $y)
  19.                 if ($color.A -gt 0) {
  20.                     if ($colorCount.ContainsKey($color)) {
  21.                         $colorCount[$color]++
  22.                     } else {
  23.                         $colorCount[$color] = 1
  24.                     }
  25.                 }
  26.             }
  27.         }
  28.         if ($colorCount.Count -eq 0) {
  29.             Write-Warning "警告:$($_.Name) 中无图像,跳过处理"
  30.             continue
  31.         }
  32.         $mostColor = $colorCount.GetEnumerator() | Sort-Object -Property Value -Descending | Select-Object -First 1 -ExpandProperty Key
  33.         $centerX = $bitmap.Width / 2
  34.         $centerY = $bitmap.Height / 2
  35.         $shortestRadius = 0
  36.         for ($y = 0; $y -lt $bitmap.Height; $y++) {
  37.             for ($x = 0; $x -lt $bitmap.Width; $x++) {
  38.                 $color = $bitmap.GetPixel($x, $y)
  39.                 if ($color.A -gt 0 -and $color -ne $mostColor) {
  40.                     $isGray = [Math]::Abs($color.R - $color.G) -le $grayTolerance -and [Math]::Abs($color.G - $color.B) -le $grayTolerance -and [Math]::Abs($color.R - $color.B) -le $grayTolerance
  41.                     if (-not $isGray) {
  42.                         $distance = [Math]::Sqrt([Math]::Pow($x - $centerX, 2) + [Math]::Pow($y - $centerY, 2))
  43.                         if ($distance -gt $shortestRadius) {
  44.                             $shortestRadius = $distance
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.         $newBitmap = New-Object System.Drawing.Bitmap $bitmap.Width, $bitmap.Height
  51.         for ($y = 0; $y -lt $bitmap.Height; $y++) {
  52.             for ($x = 0; $x -lt $bitmap.Width; $x++) {
  53.                 $originalColor = $bitmap.GetPixel($x, $y)
  54.                 $distance = [Math]::Sqrt([Math]::Pow($x - $centerX, 2) + [Math]::Pow($y - $centerY, 2))
  55.                 if ($originalColor.A -eq 0 -or $distance -gt $shortestRadius) {
  56.                     $newColor = [System.Drawing.Color]::FromArgb(255, $mostColor.R, $mostColor.G, $mostColor.B)
  57.                     $newBitmap.SetPixel($x, $y, $newColor)
  58.                 } else {
  59.                     $newBitmap.SetPixel($x, $y, $originalColor)
  60.                 }
  61.             }
  62.         }
  63.         $newFileName = [IO.Path]::ChangeExtension($_.Name, "_filled.png")
  64.         $newBitmap.Save($newFileName, [System.Drawing.Imaging.ImageFormat]::Png)
  65.         Write-Host "保存为: $newFileName" -ForegroundColor Green
  66.     } finally {
  67.         if ($memstream) { $memstream.Close() }
  68.         if ($bitmap) { $bitmap.Dispose() }
  69.         if ($newBitmap) { $newBitmap.Dispose() }
  70.     }
  71. }
  72. Write-Host "处理完成!" -ForegroundColor Green
复制代码

TOP

感觉难点在于处理圆形的灰边,和方形的四角的边框

QQ 20147578

TOP

返回列表