标题: 30元求助,挑选一部分图片加水印并备份原图 [打印本页]
作者: 464700366 时间: 2019-12-9 01:18 标题: 30元求助,挑选一部分图片加水印并备份原图
有多个文件夹,每个文件夹都有一些图片,我需要给其中一小部分图片加上水印并按目录备份这些加水印的原图
图片格式是jpg和png的,需要吧每个文件夹里的图片按大小排列,挑选出来文件最大的5张图 并且要确保选出来的图宽度必须大于1000像素,【目的是像素太低的图加水印影响美观】
如果符合要求的不够五张就有几张算几张
然后在当前文件夹新建个[备份]文件夹,吧这些符合要求的图按目录复制到备份文件夹,方便日后还原,
接下来就是给符合要求的图加上水印并覆盖原图。
q2089 06626
作者: flashercs 时间: 2019-12-9 08:29
本帖最后由 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
- }
- }
复制代码
作者: zaqmlp 时间: 2019-12-9 10:34
本帖最后由 zaqmlp 于 2019-12-9 18:27 编辑
http://bcn.bathome.net/tool/ImageMagick,6.9.2-6/convert.exe 下载该命令并跟bat和多个文件夹放一起- /*&cls
- @echo off
- mode con lines=3000
- set info=互助互利,支付宝扫码头像,感谢打赏
- rem 有问题,可加QQ956535081及时沟通
- title %info%
- set "rootpath=%~dp0"
- set "rootpath=%rootpath:~,-1%"
- cd /d "%rootpath%"
-
- rem 备份文件夹
- set "newfolder=.\备份"
-
- rem 数量
- set count=5
-
- rem 最小宽度
- set width=1000
-
- rem 水印文件
- set "logofile=.\xxx.png"
-
- rem 水印位置,1为左上角,2为右上角,3为左下角,4为右下角
- set direction=2
-
- if not exist "convert.exe" (echo;"convert.exe" not found&goto end)
- if not exist "%logofile%" (echo;"%logofile%" not found&goto end)
- set gravity=northwest
- if "%direction%" equ "2" set gravity=northeast
- if "%direction%" equ "3" set gravity=southwest
- if "%direction%" equ "4" set gravity=southeast
-
- for /f "tokens=1* delims=|" %%a in ('dir /a-d/b/s *.jpg *.png 2^>nul^|cscript -nologo -e:jscript "%~f0" %count% %width% "%rootpath%"') do (
- echo;".%%a%%b"
- if not exist "%newfolder%%%a" md "%newfolder%%%a"
- copy /y ".%%a%%b" "%newfolder%%%a"
- "convert.exe" ".%%a%%b" "%logofile%" -gravity %gravity% -geometry +5+5 -composite ".%%a%%b"
- )
- :end
- echo;%info%
- pause
- exit
- */
-
- var sa=new ActiveXObject('Shell.Application');
- var fso=new ActiveXObject('Scripting.FileSystemObject');
- var w=0,objFolder=sa.NameSpace(0);
- for(var i=0; i<350; i++){if(objFolder.GetDetailsOf(null, i) == '尺寸'){w=i;break;}}
- if(w==0){WSH.echo('failed to identify');WSH.Quit();}
-
- var files={};
- while(!WSH.StdIn.AtEndOfStream){
- var line=WSH.StdIn.ReadLine();
- var file=fso.GetFile(line);
- var fpath=file.ParentFolder.Path;
- if(files[fpath]==undefined){
- files[fpath]=[];
- }
- var fwidth=getwidth(file);var fsize=file.Size;
- if(Number(fwidth) >= Number(WSH.Arguments(1))){
- files[fpath].push(file.Name+'|'+fsize.toString());
- }
- }
-
- for(var it in files){
- if(files[it].length >0){
- files[it].sort(function(a,b){
- return Number(b.split('|')[1]) - Number(a.split('|')[1]);
- });
- var n=0;
- for(var i=0;i<files[it].length;i++){
- WSH.echo(it.replace(WSH.Arguments(2), '')+'\\|'+files[it][i].split('|')[0]);
- n++;
- if(n >= Number(WSH.Arguments(0))){break;}
- }
- }
- }
-
- function getwidth(f){
- var fw=0;
- var objFolder=sa.Namespace(f.ParentFolder.Path);
- var objItem=objFolder.ParseName(f.Name);
- fw=objFolder.GetDetailsOf(objItem, w).match(/\d+/)[0];
- return fw;
- }
复制代码
作者: WHY 时间: 2019-12-9 15:37
本帖最后由 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; #添加水印
- }
- }
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |