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

添加文字水印,添加位置:图片右下角
  1. function Add-WaterMark([string]$strFile){
  2.     $Text = (Get-Date).ToString('yyyy年MM月dd日 HH:mm:ss');                #水印文字:当前日期字符串
  3.     [int]$FontSize = 20;                                                   #字体大小
  4.     [int]$Margin = 1;                                                      #字边距
  5.     [float]$Alpha = 0.4;                                                   #透明度:[0.1~1.0]
  6.     $Color = [Drawing.Color]::FromArgb([int](256*$Alpha),255,255,255);     #水印颜色:白色
  7.     $FontFamily = 'Arial';                                                 #字体名称:Arial
  8.     $FontStyle = [Drawing.FontStyle]::Italic;                              #字体风格:斜体
  9.     $image = [System.Drawing.Image]::FromFile($strFile);
  10.     $graph = [System.Drawing.Graphics]::FromImage($image);
  11.     $font  = New-Object System.Drawing.Font($FontFamily, $FontSize, $FontStyle);
  12.     $textSize = $graph.MeasureString($Text, $font).ToPointF();
  13.     $pointF = New-Object System.Drawing.PointF;
  14.     $pointF.X = $image.Width - $Margin - $textSize.X;
  15.     $pointF.Y = $image.Height - $Margin - $textSize.Y;
  16.     $graph.DrawString($Text, $font, [System.Drawing.SolidBrush]$Color, $pointF);
  17.     $image.Save($strFile + '.tmp');
  18.     $graph.Dispose();
  19.     $image.Dispose();
  20.     mv -Literal ($strFile + '.tmp') -Dest $strFile -Force;
  21. }
  22. function Get-ImageInfo([string]$strFile){
  23.     $image = [System.Drawing.Image]::FromFile($strFile);
  24.     [int]$width = $image.Width;
  25.     [int]$height = $image.Height;
  26.     $image.Dispose();
  27.     return @($width, $height);
  28. }
  29. $ImgFolder = 'D:\Picture';                #存放源图片的文件夹
  30. $BakFolder = 'D:\Backup';                 #备份文件夹
  31. [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing');
  32. $PSObj = dir -Literal $ImgFolder -Recurse | ?{$_ -is [IO.FileInfo] -and $_.Extension -match '^\.(jpg|png)$'} | forEach{
  33.     $arr = Get-ImageInfo $_.FullName;     #图片宽度和高度
  34.     if( $arr[0] -gt 1000 ){
  35.         New-Object PSObject -Property @{ fp = $_.FullName; sz = $arr[0] * $arr[1] };
  36.     }
  37. }
  38. $PSObj | sort sz -Desc | group {[IO.Path]::GetDirectoryName($_.fp)} | forEach{
  39.     $newFolder = $BakFolder + $_.Name.SubString($ImgFolder.Length);
  40.     if( ![IO.Directory]::Exists($newFolder) ){ $null = md $newFolder; }
  41.     $_.Group | select -first 5 | forEach {
  42.         copy -Literal $_.fp -Dest $newFolder -Force;
  43.         Add-WaterMark $_.fp;              #添加水印
  44.     }
  45. }
复制代码

TOP

返回列表