本帖最后由 flashercs 于 2019-12-9 11:38 编辑
图片添加水印.bat
默认添加到图片中间了,水印位置共9种选择.- <#*,:&cls
- @echo off
- pushd "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- # 图片文件夹列表
- $picDirs = "D:\image\300","D:\image"
- # 水印图片路径
- $picWatermark = "D:\image\logo.png"
- # 水印水平位置: Left,Center,Right
- $HorizontalPosition = "Center"
- # 水印垂直位置: Top,Center,Bottom
- $VerticalPosition = "Center"
- function Get-PictureInfo {
- [CmdletBinding(DefaultParameterSetName = "PathSet")]
- param (
- [Parameter(Mandatory = $true,
- Position = 0,
- ParameterSetName = "PathSet",
- ValueFromPipeline = $true,
- ValueFromPipelineByPropertyName = $true,
- HelpMessage = "Path to one or more locations.")]
- [ValidateNotNullOrEmpty()]
- [string[]]
- $Path,
- [Parameter(Mandatory = $false,
- ParameterSetName = "LiteralPathSet",
- ValueFromPipelineByPropertyName = $true,
- HelpMessage = "Literal path to one or more locations.")]
- [ValidateNotNullOrEmpty()]
- [string[]]
- $LiteralPath
- )
-
- begin {
- if ($PSBoundParameters.ContainsKey("Debug")) {
- $DebugPreference = "Continue"
- }
-
- }
-
- process {
- $pathToProcess = if ($PSCmdlet.ParameterSetName -eq 'PathSet') {
- Convert-Path -Path $Path
- } else {
- Convert-Path -LiteralPath $LiteralPath
- }
- foreach ($filePath in $pathToProcess) {
- if (Test-Path -LiteralPath $filePath -PathType Container) {
- continue
- }
- try {
- $bitmap = New-Object "System.Drawing.Bitmap" -ArgumentList $filePath
- } catch {
- $filePath, $Error[0] | Out-String | Write-Host -ForegroundColor Red
- continue
- }
- New-Object -TypeName psobject -Property @{
- FilePath = $filepath
- Width = $bitmap.Width
- Height = $bitmap.Height
- PixelFormat = $bitmap.PixelFormat
- }
- $bitmap.Dispose()
- }
- }
- }
- function Add-WaterMark {
- param (
- [string]$ImageSource,
- [string]$ImageWatermark,
- [ValidateSet('Left', 'Center', 'Right')]
- [string]$HorizontalPosition = 'Center',
- [ValidateSet('Top', 'Center', 'Bottom')]
- [string]$VerticalPosition = 'Center'
- )
- $tmppic = [System.IO.Path]::GetTempFileName() + [System.IO.Path]::GetExtension($ImageSource)
- try {
- $image = [System.Drawing.Image]::FromFile($ImageSource)
- $watermark = [System.Drawing.Image]::FromFile($ImageWatermark)
- $gfx = [System.Drawing.Graphics]::FromImage($image)
- $waterbrush = New-Object System.Drawing.TextureBrush -ArgumentList $watermark
- switch ($HorizontalPosition) {
- 'Left' { [int]$x = 0 }
- 'Center' { [int]$x = [math]::Floor(($image.Width - $watermark.Width) / 2) }
- 'Right' { [int]$x = $image.Width - $watermark.Width }
- }
- switch ($VerticalPosition) {
- 'Top' { [int]$y = 0 }
- 'Center' { [int]$y = [math]::Floor(($image.Height - $watermark.Height) / 2) }
- 'Bottom' { [int]$y = $image.Height - $watermark.Height }
- }
-
- $waterbrush.TranslateTransform($x, $y)
- $gfx.FillRectangle($waterbrush, (New-Object System.Drawing.Rectangle -ArgumentList @((New-Object System.Drawing.Point -ArgumentList $x, $y), $watermark.Size)))
- $image.Save($tmppic)
- } finally {
- if ($image) {
- $image.Dispose()
- }
- if ($watermark) {
- $watermark.Dispose()
- }
- if ($gfx) {
- $gfx.Dispose()
- }
- if ($waterbrush) {
- $waterbrush.Dispose()
- }
- Move-Item -LiteralPath $tmppic -Destination $ImageSource -Force
- }
- }
- Add-Type -AssemblyName System.Drawing
-
- foreach ($picDir in $picDirs) {
- $backupdir = "$picDir\备份"
- if (!(Test-Path -LiteralPath $backupdir)) {
- New-Item -Path $backupdir -ItemType Directory
- }
- Get-Item -Path $picDir\* -Include *.png, *.jpg -OutBuffer 10 | Get-PictureInfo | Where-Object { $_.Width -ge 1000 } | `
- Sort-Object -Property Width, Height -Descending | Select-Object -ExpandProperty FilePath -First 2 | ForEach-Object {
- Copy-Item -LiteralPath $_ -Destination $backupdir -Verbose
- '添加水印: ' + $_ | Out-Host
- Add-WaterMark -ImageSource $_ -ImageWatermark $picWatermark -HorizontalPosition $HorizontalPosition -VerticalPosition $VerticalPosition
- }
- }
复制代码
|