引言:编写批处理程序,实现批量裁剪图片白边并按前缀名+图片名称添加文字水印。
一、下载安装ImageMagick图片处理工具
1.软件官网下载软件
ImageMagick – Convert, Edit, or Compose Digital Images
2.安装软件
2.1双击下载的安装包,勾选【I accept the agreement】,点击【Next】;
2.2在安装界面信息窗口点击【Next】;
2.3在安装路径选择窗口选择软件安装路径后,点击【Next】;
2.4在创建快捷方式窗口点击【Next】;
2.5在选择其他任务界面点击Next;
2.6在准备安装界面点击【Install】;
2.7在软件信息界面点击【Next】;
2.8在软件安装完成界面点击Finish完成软件安装;
二、新建Windows批处理文件
参考博客:
CSDNhttps://mp.csdn.net/mp_blog/creation/editor/132137544
三、写入批处理代码
1.右键新建的批处理文件,点击【编辑】。
2.在打开的记事本中写入以下代码:
@echo off
setlocal enabledelayedexpansion
REM 创建output1和output2文件夹
if not exist output1 mkdir output1
if not exist output2 mkdir output2
REM 裁剪图片并输出到output1文件夹
for %%f in (*.jpg) do (
echo 裁剪图片 %%f
set "filename=%%~nf"
set "outputname=output1\!filename!.jpg"
REM 裁剪图片
magick "%%f" -fuzz 10%% -trim +repage "!outputname!"
)
REM 添加文字水印并输出到output2文件夹,如需修改前缀可替换第4行PYLD为实际需要的内容
for /f "tokens=*" %%f in ('dir /b /a-d output1\*.jpg') do (
echo 添加文字水印 %%f
set "filename=%%~nf"
set "outputname=output2\PYLD!filename!.jpg"
REM 添加文字水印
magick "output1\%%f" -pointsize 72 -font "宋体" -fill red -gravity northwest -annotate +58+26 "PYLD!filename!" -gravity northwest -annotate +58+26 "" "!outputname!"
)
echo 完成
四、运行批处理
1.将图片和批处理放在同一目录下,双击运行批处理,即可实现去除图片白边并为图片添加文字水印功能,处理后的图片存放在output2文件夹。