|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 hoebec 于 2021-1-18 19:22 编辑
最近发现ImageMagick的自动脚本有时也挺方便的,发一些自己做的bat研究一下
因为是bat,所以都只支持单个文件和单层目录,没有递归(不会)
首先是基本的jpg转换和png转换:
- @echo off
- title Image-JPG
- Setlocal Enabledelayedexpansion
- Set str=%1
- :str
- if not defined str Set /p "str=Image file/folder:"
- if not defined str goto str
- dir /ad %str% >nul 2>nul&&goto dir||goto file
- exit
- :dir
- if not exist !str!\ exit
- echo convert image folder
- cd /d %str%
- Set qua=90
- Set /p qua=compression ratio(default/90):
- echo convert image files to jpg%qua% directory
- if not exist %str%-jpg%qua% mkdir %str%-jpg%qua%
- %~dp0\mogrify -path %str%-jpg%qua% -format jpg -quality %qua% *.*
- Set /p exitinfo=done..
- exit
- :file
- if not exist !str! exit
- echo convert image file
- Set qua=90
- Set /p qua=compression ratio(default/90):
- echo convert %str% to jpg%qua% file
- for %%a in (!str!) do (
- %~dp0\convert !str! -format jpg -quality %qua% "%%~dpna-jpg%qua%.jpg")
- Set /p exitinfo=done..
- exit
复制代码
- @echo off
- title Image-PNG
- Setlocal Enabledelayedexpansion
- Set str=%1
- :str
- if not defined str Set /p "str=Image file/folder:"
- if not defined str goto str
- dir /ad %str% >nul 2>nul&&goto dir||goto file
- exit
- :dir
- if not exist !str!\ exit
- echo convert image folder
- cd /d %str%
- Set qua=75
- Set /p qua=compression level(default/75):
- echo convert image files to png%qua% directory
- if not exist %str%-png%qua% mkdir %str%-png%qua%
- %~dp0\mogrify -path %str%-png%qua% -format png -quality %qua% *.*
- Set /p exitinfo=done..
- exit
- :file
- if not exist !str! exit
- echo convert image file
- Set qua=75
- Set /p qua=compression level(default/75):
- echo convert %str% to png%qua% file
- for %%a in (!str!) do (
- %~dp0\convert !str! -format png -quality %qua% "%%~dpna-png%qua%.png")
- Set /p exitinfo=done..
- exit
复制代码
使用方法:首先必须安装ImageMagick,官网下载https://www.imagemagick.org/script/download.php#windows.当然不用安装版的也可以,我自己用的是portable版(把exe文件复制到bat所在目录)
本贴的脚本都基于ImageMagick-7.0.10-57-portable-Q16-x64.zip版本制作
以上两个脚本需要用到convert.exe.双击运行bat,把需要转换的图片文件/文件夹拖到cmd窗口,ENTER,输入压缩质量数值,jpg是1-100,png是00-95(第一位数字取值0-9,第二位数字取值0-5),然后等待完成
会创建新文件/文件夹,不会修改原文件
图片缩放:
- @echo off
- title Image-Resize
- Setlocal Enabledelayedexpansion
- Set str=%1
- :str
- if not defined str Set /p "str=Image file/folder:"
- if not defined str goto str
- if not exist %str%\ goto file
- :dir
- echo resize image folder
- cd /d %str%
- Set ext=jpg
- Set /p ext=new file type(default/jpg):
- Set qua=90
- Set /p qua=quality(default/90):
- Set rs=x1000
- Set /p rs=image resize setting(1000/x1000,default/x1000):
- if not exist %str%-%rs%-%ext%%qua% mkdir %str%-%rs%-%ext%%qua%
- echo convert image files to %rs%-%ext%%qua% directory
- %~dp0\mogrify -path %str%-%rs%-%ext%%qua% -resize %rs% -format %ext% -quality %qua% *.*
- Set /p exitinfo=done..
- exit
- :file
- if not exist %str% exit
- echo resize image file
- Set ext=jpg
- Set /p ext=new file type(default/jpg):
- Set qua=90
- Set /p qua=quality(default/90):
- Set rs=x1000
- Set /p rs=image resize setting(1000/x1000,default/x1000):
- for %%a in (!str!) do (
- echo convert %%~nxa to %rs%-%ext%%qua% file
- %~dp0\convert !str! -resize %rs% -format %ext% -quality %qua% "%%~dpna-%rs%-%ext%%qua%.!ext!")
- Set /p exitinfo=done..
- exit
复制代码
ImageMagick的缩放参数比较多,基本的就是1000和x1000这样的格式
图片旋转:
- @echo off
- title Image-Rotate
- Setlocal Enabledelayedexpansion
- Set str=%1
- :str
- if not defined str Set /p "str=Image file/folder:"
- if not defined str goto str
- if not exist %str%\ goto file
- :dir
- echo rotate image folder
- cd /d %str%
- Set ext=jpg
- Set /p ext=new file type(default/jpg):
- Set qua=90
- Set /p qua=quality(default/90):
- Set bg=none
- Set /p bg=background color(default/none):
- Set rt=90
- Set /p rt=image rotation degree(90/-90,default/90):
- if not exist %str%-r%rt%-%ext%%qua% mkdir %str%-r%rt%-%ext%%qua%
- echo convert image files to r%rt%-%ext%%qua% directory
- %~dp0\mogrify -path %str%-r%rt%-%ext%%qua% -background %bg% -rotate %rt% -format %ext% -quality %qua% *.*
- Set /p exitinfo=done..
- exit
- :file
- if not exist %str% exit
- echo rotate image file
- Set ext=jpg
- Set /p ext=new file type(default/jpg):
- Set qua=90
- Set /p qua=quality(default/90):
- Set bg=none
- Set /p bg=background color(default/none):
- Set rt=90
- Set /p rt=image rotation degree(90/-90,default/90):
- for %%a in (!str!) do (
- echo convert %%~nxa to r%rt%-%ext%%qua% file
- %~dp0\convert !str! -background %bg% -rotate %rt% -format %ext% -quality %qua% "%%~dpna-r%rt%-%ext%%qua%.!ext!")
- Set /p exitinfo=done..
- exit
复制代码
90,-90是旋转角度,这两种角度也不需要考虑背景色
批量加水印:
- ' 2>nul&cls&@echo off
- ' 2>nul&title Watermark
- ' 2>nul&Setlocal Enabledelayedexpansion
- ' 2>nul&Set /p "Input=Image file/folder:"
- ' 2>nul&Set /p "Water=Watermark txt/image:"
- ' 2>nul&start "" Wscript.exe %0 %Input% %Water% //Nologo //e:vbs &exit
- Set objFSO = CreateObject("scripting.filesystemobject")
- Set WshShell = CreateObject("Wscript.Shell")
- Set RE = CreateObject("vbscript.RegExp")
- Input = wscript.arguments(0)
- Water = wscript.arguments(1)
- Font="msyh.ttf"
- Size=15
- Color="red"
- BgColor="none"
- Width=300
- Opacity=50
- Ext="png"
- Pos="-gravity southeast -geometry +25+25"
- If LCase(objFSO.GetExtensionName(Water))="txt" Then
- Font = Inputbox("Font Path:",,Font)
- Size = Inputbox("Font Size:",,Size)
- Color = Inputbox("Font Color:",,Color)
- BgColor = Inputbox("Bg Color:",,BgColor)
- Width = Inputbox("Watermark Width:",,Width)
- End If
- Opacity = Inputbox("Opacity:",,Opacity)
- Ext = Inputbox("Output Ext:",,Ext)
- Pos = Inputbox("Watermark Position:",,Pos)
- If Font<>EMPTY Then FontPara="-font """&Font&""""
- If Opacity<>EMPTY Then Compos="-compose dissolve -define compose:args="&Opacity
- If objFSO.FileExists(Input) Then Output=objFSO.GetParentFolderName(Input)&"\"&objFSO.GetBaseName(Input)&"-watermarked."&Ext
- If objFSO.FolderExists(Input) Then
- Output=Input&"-watermarked"
- If not objFSO.FolderExists(Output) Then objFSO.createFolder(Output)
- End If
- If LCase(objFSO.GetExtensionName(Water))="txt" Then
- WatermarkImage=objFSO.GetSpecialFolder(2)&"\watermark.png"
- WaterText=objFSO.OpenTextFile(Water,1,false,true).ReadAll
- RE.Global = True
- RE.MultiLine = True
- RE.IgnoreCase = True
- RE.Pattern = chr(34)
- WaterText=RE.Replace(WaterText,chr(34)&chr(34))
- RE.Pattern = chr(37)
- WaterText=RE.Replace(WaterText,chr(37)&chr(37))
- WshShell.run "convert -background """&Bgcolor&""" -fill """&Color&""" -pointsize "&Size&" -size "&Width&" "&FontPara&" caption:"""&WaterText&""" +antialias -flatten -trim "&WatermarkImage&"",0,true
- Else
- WatermarkImage=Water
- End If
- WaterSpace=objFSO.GetSpecialFolder(2)&"\colorspace"
- If objFSO.FileExists(Input) Then
- WatermarkImage=chr(34)&WatermarkImage&chr(34)
- Input=chr(34)&Input&chr(34)
- Output=chr(34)&Output&chr(34)
- WshShell.Run "cmd /c identify -format %[colorspace] "&Input&">"&WaterSpace&"",0,true
- CS="-colorspace "&objFSO.OpenTextFile(WaterSpace,1,false,false).ReadLine
- WshShell.Run "composite "&CS&" "&Pos&" "&Compos&" "&WatermarkImage&" "&Input&" "&Output&"",0,true
- objFSO.DeleteFile(WaterSpace)
- CS=EMPTY
- End If
- If objFSO.FolderExists(Input) Then
- WatermarkImage=chr(34)&WatermarkImage&chr(34)
- For Each i in objFSO.GetFolder(Input).files
- Inputfile=i.path
- Outputfile=Output&"\"&objFSO.GetBaseName(i)&"."&Ext
- Inputfile=chr(34)&Inputfile&chr(34)
- Outputfile=chr(34)&Outputfile&chr(34)
- WshShell.Run "cmd /c identify -format %[colorspace] "&Inputfile&">"&WaterSpace&"",0,true
- CS="-colorspace "&objFSO.OpenTextFile(WaterSpace,1,false,false).ReadLine
- WshShell.Run "composite "&CS&" "&Pos&" "&Compos&" "&WatermarkImage&" "&Inputfile&" "&Outputfile&"",0,true
- objFSO.DeleteFile(WaterSpace)
- CS=EMPTY
- Next
- End If
- Wscript.Echo "Done"
复制代码
首先把需要加水印的图片文件/文件夹拖到cmd窗口,ENTER然后输入水印对应的txt/图片,如果输入的是图片,下一步就是在vbs里面依次选择(不)透明度,输出格式,水印位置(默认是右下角,距离边缘25像素).如果输入的是txt,就会读取txt的所有文本(按unicode编码读取),转换成一张水印图片,会增加文字的样式选择.
也一样是创建新文件,不修改原文件
转换截图文件:
- @echo off
- title ClipBoard-Image
- Setlocal Enabledelayedexpansion
- Set hour=%time:~0,2%
- for /l %%i in (0,1,9) do (if %time:~0,2%==%%i Set hour=0%%i)
- Set scrtime=%date:~0,4%%date:~5,2%%date:~8,2%_%hour%%time:~3,2%%time:~6,2%
- Set scrfolder=%HOMEPATH%\Pictures\ScreenShot
- if not exist %scrfolder% mkdir %scrfolder%
- %~dp0\convert clipboard:myimage %scrfolder%\%scrtime%.png
复制代码
这个的用法是按下printscreen键后,双击运行脚本,就会在"我的图片\ScreenShot"目录下生成以时间数字命名的png截图
图片裁剪:
有两个选项,一个按边缘裁剪,另一个是水平方向平分为两张图片,这个脚本不支持带空格的文件路径
几何拼合图片:
- @echo off
- title Image-geometry
- Setlocal Enabledelayedexpansion
- Set str=%1
- :str
- if not defined str Set /p "str=Image file/folder:"
- if not defined str goto str
- if not exist %str%\ exit
- cd /d %str%
- Set ext=jpg
- Set /p ext=new file type(default/jpg):
- Set qua=90
- Set /p qua=quality(default/90):
- Set bg=none
- Set /p bg=background color(default/none):
- Set lati=1
- Set /p lati=horizontal image number(default/1):
- Set longi=1
- Set /p longi=vertical image number(default/1):
- echo geometry image files to %ext%%qua%-%lati%-%longi%.!ext!
- for %%a in (!str!) do (
- %~dp0\montage *.* -background %bg% -geometry +0+0 -tile %lati%x%longi% -format %ext% -quality %qua% "%%~dpna-%ext%%qua%-%lati%-%longi%.!ext!")
- Set /p exitinfo=done..
- exit
复制代码
按横向与竖向的设定数目拼合
定向去色:
- @echo off
- title Image-transparent
- Setlocal Enabledelayedexpansion
- Set str=%1
- :str
- if not defined str Set /p "str=Image file/folder:"
- if not defined str goto str
- dir /ad %str% >nul 2>nul&&goto dir||goto file
- exit
- :dir
- if not exist !str!\ exit
- echo convert image folder
- cd /d %str%
- Set color=white
- Set /p color=discard color(default/white):
- echo convert image files to transparent-%color% directory
- if not exist %str%-transparent-%color% mkdir %str%-transparent-%color%
- %~dp0\mogrify -path %str%-transparent-%color% -transparent %color% *.*
- Set /p exitinfo=done..
- exit
- :file
- if not exist !str! exit
- echo convert image file
- Set color=white
- Set /p color=discard color(default/white):
- for %%a in (!str!) do (
- echo convert %%~nxa to transparent-%color% file
- %~dp0\convert !str! -transparent %color% "%%~dpna-transparent-%color%.png")
- Set /p exitinfo=done..
- exit
复制代码
使某种颜色的区域变透明(假装可以批量去水印/抠图) |
评分
-
查看全部评分
|