Vbscript 和 imagemagick 错误调整大小
Vbscript and imagemagick error resize
我尝试先将每个子文件夹重新压缩 jpg 并将其放入新的子文件夹
Dim strFolderPath,objFSO
strDirectory= "D:\images\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder, objSubFolder,folderCur
Set objFolder = objFSO.GetFolder(strDirectory)
Dim imageMagick
Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")
If objFolder.SubFolders.Count > 0 Then
For Each objSubFolder in objFolder.SubFolders
'Now check if the folder contains any files.
If objSubFolder.Files.Count > 0 Then
folderCur = objSubFolder & "\_small320\"
For each JpgFile in objSubFolder.Files
WScript.Echo "Checking Folder: " & folderCur & "\" & JpgFile.name & vbcrlf & " File: " & JpgFile
if not objFSO.folderexists(folderCur) then objFSO.createfolder folderCur
'imageMagick.Exec("convert " & JpgFile & " -resize 320x210 " & JpgFile.name)
imageMagick.Convert JpgFile, "-resize", "320x210", folderCur & "\preview.jpg"
exit for
next
End If
Next
End If
但是我在 imageMagick.Convert JpgFile, "-resize", "320x210", folderCur & "\preview.jpg"
上出错
语法有什么问题?
也出现这个错误
但文件夹中存在所有 dll
未测试(手头没有 ImageMagick),但可能
imageMagick.Convert JpgFile.Path, "-resize", "320x210", folderCur & "preview.jpg"
包含文件的完整路径并删除了输出文件中的附加反斜杠(包含在文件夹名称中)。
已编辑 现在已经过测试并且可以正常工作。此代码处理放置脚本的文件夹的子文件夹。
Option Explicit
Const OUTPUT_FOLDER_NAME = "_small320"
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim strScriptFolder
strScriptFolder = fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path
Dim rootFolder
Set rootFolder = fso.GetFolder( strScriptFolder )
Dim imageMagick
Set imageMagick = WScript.CreateObject("ImageMagickObject.MagickImage.1")
Dim inputFolder, inputFile, strOutputFolder
For Each inputFolder In rootFolder.SubFolders
If inputFolder.Name <> OUTPUT_FOLDER_NAME Then
strOutputFolder = fso.BuildPath(inputFolder.Path, OUTPUT_FOLDER_NAME)
If Not fso.FolderExists(strOutputFolder) Then
fso.CreateFolder strOutputFolder
End If
For Each inputFile In inputFolder.Files
Select Case LCase(fso.GetExtensionName(inputFile.Path))
Case "jpg", "jpeg", "png"
WScript.Echo "[ convert ] " & inputFile.Path
imageMagick.Convert inputFile.Path, _
"-resize", _
"320x210", _
fso.BuildPath(strOutputFolder, inputFile.Name)
Case Else
WScript.Echo "[ skip ] " & inputFile.Path
End Select
Next
End If
Next
问题中的原始错误 (Unsupported argument type
) 是在传递 File
对象而不是包含文件路径的字符串作为参数时产生的。
Unable to load module
是一个 ImageMagick
错误,不是编程错误。可能是 path
变量错误导致找不到模块或者是安装错误。之前的代码已经过全新 ImageMagick-6.9.1-6-Q16-x64-dll.exe
安装测试(并且有效)。
我尝试先将每个子文件夹重新压缩 jpg 并将其放入新的子文件夹
Dim strFolderPath,objFSO
strDirectory= "D:\images\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder, objSubFolder,folderCur
Set objFolder = objFSO.GetFolder(strDirectory)
Dim imageMagick
Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")
If objFolder.SubFolders.Count > 0 Then
For Each objSubFolder in objFolder.SubFolders
'Now check if the folder contains any files.
If objSubFolder.Files.Count > 0 Then
folderCur = objSubFolder & "\_small320\"
For each JpgFile in objSubFolder.Files
WScript.Echo "Checking Folder: " & folderCur & "\" & JpgFile.name & vbcrlf & " File: " & JpgFile
if not objFSO.folderexists(folderCur) then objFSO.createfolder folderCur
'imageMagick.Exec("convert " & JpgFile & " -resize 320x210 " & JpgFile.name)
imageMagick.Convert JpgFile, "-resize", "320x210", folderCur & "\preview.jpg"
exit for
next
End If
Next
End If
但是我在 imageMagick.Convert JpgFile, "-resize", "320x210", folderCur & "\preview.jpg"
语法有什么问题?
也出现这个错误
但文件夹中存在所有 dll
未测试(手头没有 ImageMagick),但可能
imageMagick.Convert JpgFile.Path, "-resize", "320x210", folderCur & "preview.jpg"
包含文件的完整路径并删除了输出文件中的附加反斜杠(包含在文件夹名称中)。
已编辑 现在已经过测试并且可以正常工作。此代码处理放置脚本的文件夹的子文件夹。
Option Explicit
Const OUTPUT_FOLDER_NAME = "_small320"
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim strScriptFolder
strScriptFolder = fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path
Dim rootFolder
Set rootFolder = fso.GetFolder( strScriptFolder )
Dim imageMagick
Set imageMagick = WScript.CreateObject("ImageMagickObject.MagickImage.1")
Dim inputFolder, inputFile, strOutputFolder
For Each inputFolder In rootFolder.SubFolders
If inputFolder.Name <> OUTPUT_FOLDER_NAME Then
strOutputFolder = fso.BuildPath(inputFolder.Path, OUTPUT_FOLDER_NAME)
If Not fso.FolderExists(strOutputFolder) Then
fso.CreateFolder strOutputFolder
End If
For Each inputFile In inputFolder.Files
Select Case LCase(fso.GetExtensionName(inputFile.Path))
Case "jpg", "jpeg", "png"
WScript.Echo "[ convert ] " & inputFile.Path
imageMagick.Convert inputFile.Path, _
"-resize", _
"320x210", _
fso.BuildPath(strOutputFolder, inputFile.Name)
Case Else
WScript.Echo "[ skip ] " & inputFile.Path
End Select
Next
End If
Next
问题中的原始错误 (Unsupported argument type
) 是在传递 File
对象而不是包含文件路径的字符串作为参数时产生的。
Unable to load module
是一个 ImageMagick
错误,不是编程错误。可能是 path
变量错误导致找不到模块或者是安装错误。之前的代码已经过全新 ImageMagick-6.9.1-6-Q16-x64-dll.exe
安装测试(并且有效)。