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

图片添加水印.bat
默认添加到图片中间了,水印位置共9种选择.
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. pause
  7. exit /b
  8. #>
  9. # 图片文件夹列表
  10. $picDirs = "D:\image\300","D:\image"
  11. # 水印图片路径
  12. $picWatermark = "D:\image\logo.png"
  13. # 水印水平位置: Left,Center,Right
  14. $HorizontalPosition = "Center"
  15. # 水印垂直位置: Top,Center,Bottom
  16. $VerticalPosition = "Center"
  17. function Get-PictureInfo {
  18.   [CmdletBinding(DefaultParameterSetName = "PathSet")]
  19.   param (
  20.     [Parameter(Mandatory = $true,
  21.       Position = 0,
  22.       ParameterSetName = "PathSet",
  23.       ValueFromPipeline = $true,
  24.       ValueFromPipelineByPropertyName = $true,
  25.       HelpMessage = "Path to one or more locations.")]
  26.     [ValidateNotNullOrEmpty()]
  27.     [string[]]
  28.     $Path,
  29.     [Parameter(Mandatory = $false,
  30.       ParameterSetName = "LiteralPathSet",
  31.       ValueFromPipelineByPropertyName = $true,
  32.       HelpMessage = "Literal path to one or more locations.")]
  33.     [ValidateNotNullOrEmpty()]
  34.     [string[]]
  35.     $LiteralPath
  36.   )
  37.   
  38.   begin {
  39.     if ($PSBoundParameters.ContainsKey("Debug")) {
  40.       $DebugPreference = "Continue"
  41.     }
  42.    
  43.   }
  44.   
  45.   process {
  46.     $pathToProcess = if ($PSCmdlet.ParameterSetName -eq 'PathSet') {
  47.       Convert-Path -Path $Path
  48.     } else {
  49.       Convert-Path -LiteralPath $LiteralPath
  50.     }
  51.     foreach ($filePath in $pathToProcess) {
  52.       if (Test-Path -LiteralPath $filePath -PathType Container) {
  53.         continue
  54.       }
  55.       try {
  56.         $bitmap = New-Object "System.Drawing.Bitmap" -ArgumentList $filePath
  57.       } catch {
  58.         $filePath, $Error[0] | Out-String | Write-Host -ForegroundColor Red
  59.         continue
  60.       }
  61.       New-Object -TypeName psobject -Property @{
  62.         FilePath    = $filepath
  63.         Width       = $bitmap.Width
  64.         Height      = $bitmap.Height
  65.         PixelFormat = $bitmap.PixelFormat
  66.       }
  67.       $bitmap.Dispose()
  68.     }
  69.   }
  70. }
  71. function Add-WaterMark {
  72.   param (
  73.     [string]$ImageSource,
  74.     [string]$ImageWatermark,
  75.     [ValidateSet('Left', 'Center', 'Right')]
  76.     [string]$HorizontalPosition = 'Center',
  77.     [ValidateSet('Top', 'Center', 'Bottom')]
  78.     [string]$VerticalPosition = 'Center'
  79.   )
  80.   $tmppic = [System.IO.Path]::GetTempFileName() + [System.IO.Path]::GetExtension($ImageSource)
  81.   try {
  82.     $image = [System.Drawing.Image]::FromFile($ImageSource)
  83.     $watermark = [System.Drawing.Image]::FromFile($ImageWatermark)
  84.     $gfx = [System.Drawing.Graphics]::FromImage($image)
  85.     $waterbrush = New-Object System.Drawing.TextureBrush -ArgumentList $watermark
  86.     switch ($HorizontalPosition) {
  87.       'Left' { [int]$x = 0 }
  88.       'Center' { [int]$x = [math]::Floor(($image.Width - $watermark.Width) / 2) }
  89.       'Right' { [int]$x = $image.Width - $watermark.Width }
  90.     }
  91.     switch ($VerticalPosition) {
  92.       'Top' { [int]$y = 0 }
  93.       'Center' { [int]$y = [math]::Floor(($image.Height - $watermark.Height) / 2) }
  94.       'Bottom' { [int]$y = $image.Height - $watermark.Height }
  95.     }
  96.    
  97.     $waterbrush.TranslateTransform($x, $y)
  98.     $gfx.FillRectangle($waterbrush, (New-Object System.Drawing.Rectangle -ArgumentList @((New-Object System.Drawing.Point -ArgumentList $x, $y), $watermark.Size)))
  99.     $image.Save($tmppic)
  100.   } finally {
  101.     if ($image) {
  102.       $image.Dispose()
  103.     }
  104.     if ($watermark) {
  105.       $watermark.Dispose()
  106.     }
  107.     if ($gfx) {
  108.       $gfx.Dispose()
  109.     }
  110.     if ($waterbrush) {
  111.       $waterbrush.Dispose()
  112.     }
  113.     Move-Item -LiteralPath $tmppic -Destination $ImageSource -Force
  114.   }
  115. }
  116. Add-Type -AssemblyName System.Drawing
  117. foreach ($picDir in $picDirs) {
  118.   $backupdir = "$picDir\备份"
  119.   if (!(Test-Path -LiteralPath $backupdir)) {
  120.     New-Item -Path $backupdir -ItemType Directory
  121.   }
  122.   Get-Item -Path $picDir\* -Include *.png, *.jpg -OutBuffer 10 | Get-PictureInfo | Where-Object { $_.Width -ge 1000 } | `
  123.     Sort-Object -Property Width, Height -Descending | Select-Object -ExpandProperty FilePath -First 2 | ForEach-Object {
  124.     Copy-Item -LiteralPath $_ -Destination $backupdir -Verbose
  125.     '添加水印: ' + $_ | Out-Host
  126.     Add-WaterMark -ImageSource $_ -ImageWatermark $picWatermark -HorizontalPosition $HorizontalPosition -VerticalPosition $VerticalPosition
  127.   }
  128. }
复制代码
微信:flashercs
QQ:49908356

TOP

返回列表