本帖最后由 WHY 于 2019-12-10 00:07 编辑
添加文字水印,添加位置:图片右下角- function Add-WaterMark([string]$strFile){
- $Text = (Get-Date).ToString('yyyy年MM月dd日 HH:mm:ss'); #水印文字:当前日期字符串
- [int]$FontSize = 20; #字体大小
- [int]$Margin = 1; #字边距
- [float]$Alpha = 0.4; #透明度:[0.1~1.0]
- $Color = [Drawing.Color]::FromArgb([int](256*$Alpha),255,255,255); #水印颜色:白色
- $FontFamily = 'Arial'; #字体名称:Arial
- $FontStyle = [Drawing.FontStyle]::Italic; #字体风格:斜体
-
- $image = [System.Drawing.Image]::FromFile($strFile);
- $graph = [System.Drawing.Graphics]::FromImage($image);
- $font = New-Object System.Drawing.Font($FontFamily, $FontSize, $FontStyle);
- $textSize = $graph.MeasureString($Text, $font).ToPointF();
- $pointF = New-Object System.Drawing.PointF;
- $pointF.X = $image.Width - $Margin - $textSize.X;
- $pointF.Y = $image.Height - $Margin - $textSize.Y;
-
- $graph.DrawString($Text, $font, [System.Drawing.SolidBrush]$Color, $pointF);
- $image.Save($strFile + '.tmp');
- $graph.Dispose();
- $image.Dispose();
-
- mv -Literal ($strFile + '.tmp') -Dest $strFile -Force;
- }
-
- function Get-ImageInfo([string]$strFile){
- $image = [System.Drawing.Image]::FromFile($strFile);
- [int]$width = $image.Width;
- [int]$height = $image.Height;
- $image.Dispose();
- return @($width, $height);
- }
-
- $ImgFolder = 'D:\Picture'; #存放源图片的文件夹
- $BakFolder = 'D:\Backup'; #备份文件夹
- [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing');
-
- $PSObj = dir -Literal $ImgFolder -Recurse | ?{$_ -is [IO.FileInfo] -and $_.Extension -match '^\.(jpg|png)$'} | forEach{
- $arr = Get-ImageInfo $_.FullName; #图片宽度和高度
- if( $arr[0] -gt 1000 ){
- New-Object PSObject -Property @{ fp = $_.FullName; sz = $arr[0] * $arr[1] };
- }
- }
-
- $PSObj | sort sz -Desc | group {[IO.Path]::GetDirectoryName($_.fp)} | forEach{
- $newFolder = $BakFolder + $_.Name.SubString($ImgFolder.Length);
- if( ![IO.Directory]::Exists($newFolder) ){ $null = md $newFolder; }
- $_.Group | select -first 5 | forEach {
- copy -Literal $_.fp -Dest $newFolder -Force;
- Add-WaterMark $_.fp; #添加水印
- }
- }
复制代码
|