回复 35# wh123wh123
removeRedWatermark.ps1- Add-Type -AssemblyName System.Drawing
-
- function removeRedWatermark-Image {
- param([string]$imagePath)
- $image = [System.Drawing.Bitmap]::FromFile($imagePath)
- $rect = New-Object System.Drawing.Rectangle(0, 0, $image.Width, $image.Height)
- $bitmapData = $image.LockBits($rect, [System.Drawing.Imaging.ImageLockMode]::ReadWrite, $image.PixelFormat)
- $ptr = $bitmapData.Scan0
- $bytes = $bitmapData.Stride * $image.Height
- $rgbValues = New-Object byte[] $bytes
- [System.Runtime.InteropServices.Marshal]::Copy($ptr, $rgbValues, 0, $bytes)
-
- for ($i = 0; $i -lt $rgbValues.Length; $i += 3) {
- $b = $rgbValues[$i]
- $g = $rgbValues[$i + 1]
- $r = $rgbValues[$i + 2]
-
- if ($r -gt $g -and $r -gt $b) {
- $total = $r + $g + $b
- if ($total -gt 400) {
- $rgbValues[$i] = 255
- $rgbValues[$i + 1] = 255
- $rgbValues[$i + 2] = 255
- } else {
- $rgbValues[$i] = 0
- $rgbValues[$i + 1] = 0
- $rgbValues[$i + 2] = 0
- }
- }
- }
- [System.Runtime.InteropServices.Marshal]::Copy($rgbValues, 0, $ptr, $bytes)
- $image.UnlockBits($bitmapData)
- $newPath = [System.IO.Path]::GetDirectoryName($imagePath) + "\processed_" + [System.IO.Path]::GetFileName($imagePath)
- $image.Save($newPath)
- $image.Dispose()
- }
-
- $(dir *.jpg).foreach{
- Write-Host "processing: $($_.Name)"
- removeRedWatermark-Image $_.FullName
- }
复制代码
|