返回列表 发帖

[问题求助] 求助 vbs调用Windows自带画图工具对图片打开后保存-批量方式

set wshell = wscript.createobject("wscript.shell")
wshell.run "mspaint.exe 20221124123033.jpg"
WScript.Sleep 2000
wshell.SendKeys "^{s}"
WScript.Sleep 2000
wshell.SendKeys "%{F4}"COPY
求助高手,如何将以上用画图工具打开图片并保存的代码,做成遍历文件夹文件,循环执行的形式。

set shell=createobject("shell.application")
foldername="C:\Users"
set nowfolder=shell.namespace(foldername)
Set Img = CreateObject("WIA.ImageFile")
Set IP = CreateObject("WIA.ImageProcess")
Const wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
set re=new regexp
re.pattern=".(png|bmp|wbmp)$"'这一行自己加
IP.Filters.Add IP.FilterInfos("Convert").FilterID
IP.Filters(1).Properties("FormatID").Value = wiaFormatJPEG
filetree nowfolder
sub filetree(fdobject)
set items=fdobject.items
for each item in items
if item.isfolder then
filetree item.getfolder
elseif re.test(item.path) then
'输出到当前文件夹 确保被转换的不是jpg格式
convert item.path
end if
next
end sub
sub convert(filep)
img.loadfile filep
set img=ip.apply(img)
img.savefile re.replace(filep,".jpg")
end subCOPY

TOP

啧 你要转换图片格式你直说啊

TOP

回复 7# likongshun


    你能否找一张图片作为例子,把操作前和操作后的图片打包上传到网盘我看看?
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 7# likongshun

这重编码了吧,图片质量降低了,而且也不一定能减少体积,像灰阶图可能反而增加体积

TOP

能减少文件大小
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "./"
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
For Each strFile in colFiles
    If LCase(objFSO.GetExtensionName(strFile)) = "jpg" Then
        ' 在此处执行操作,例如打印文件名或打开文件等
        set wshell = wscript.createobject("wscript.shell")
        wshell.run("mspaint.exe "+strFile)
        WScript.Sleep 2000
        wshell.SendKeys "^{s}"
        WScript.Sleep 2000
        wshell.SendKeys "%{F4}"
    End If
NextCOPY

TOP

回复 5# hlzj88


    话说保存为jpg压缩理论上来讲和系统没关系 只和压缩算法有关
你好

TOP

这个问题我以前在其他论坛也提过,这样做(另存为同名jpg)的目的可以缩小同尺寸图片占用的空间(xp下),win7下效果不大。win10没用过。

推荐使用 jpeg resizer 这个软件。效率高功能多。
当 部分 jpeg resizer 不能加载的jpg文件,使用上面的代码处理后,可以加载。
目的,学习批处理

TOP

打开并保存对图片没啥意义啊,打开并另存至少还能转换格式

TOP

根据我用画图和sendkeys的十足经验 你这段代码只会极其浪费时间并且开出一堆画图窗口且不会关闭任何一个
vbs画图用wia啊兄弟 况且你打开,然后保存有什么意义?
你好

TOP

回复 1# likongshun


    能否介绍一下画图另存为这个操作的目的是什么?
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表