首页 电脑 电脑学堂 查看内容

Photoshop批量生成缩略图

2009-5-20 12:56 680 0

摘要: 在网上找到一段关于Photoshop的Vbs脚本,本想改成批量生成缩略图的脚本,但总是提示创建Photoshop.JPEGSaveOptions对象失败,没办法只好编写两个文件:一个jsx文件(其实就...
关键词: nbsp doc Directory JPEGSaveOptions rsHeight Function limitHeight limitWidth objFile 文件

在网上找到一段关于Photoshop的Vbs脚本,本想改成批量生成缩略图的脚本,但总是提示创建Photoshop.JPEGSaveOptions对象失败,没办法只好编写两个文件:一个jsx文件(其实就是js文件)、一个Vbs文件。 Vbs文件的代码: Directory = "F:\12\Dvbbs8.2.0_Ac\dvbbs80\UploadFile\2008-1" '原始图像的文件夹jsxFile = "C:\Documents and Settings\new\桌面\jsx.jsx" 'jsx文件的路径 Const psDoNotSaveChanges     = 2Const psDisplayNoDialogs     = 3Const ImageFilter = "PSD;BMP;JPG;JPEG;GIF;PNG" Set app = CreateObject( "Photoshop.Application" ) limitWidth = 200 '最大寬度limitHeight = 160 '最大高度ImgResolution = 72 '解析度 app.bringToFront()app.preferences.rulerUnits = 1 'psPixelsapp.DisplayDialogs = psDisplayNoDialogs Call Convert2JpegByFolder(Directory) Function ReSizeImg(doc)If doc.width <= limitWidth And doc.height <= limitHeight Then Exit Function rsWidth = doc.widthrsHeight = doc.heightScale = 1.0 if doc.width > limitWidth Then        Scale = limitWidth / (doc.width + 0.0)        rsWidth = doc.width * Scale        rsHeight = doc.height * ScaleEnd If if rsHeight > limitHeight Then        Scale = limitHeight / (doc.height + 0.0)        rsWidth = doc.width * Scale        rsHeight = doc.height * ScaleEnd If doc.resizeImage rsWidth, rsHeight, ImgResolution, 3End Function Function Convert2JpegByFolder(Directory)Set fso = CreateObject("Scripting.FileSystemObject")If Not fso.FolderExists(Directory) Then             MsgBox "Photo Directory NOT Exists."        Exit FunctionEnd IfSet objFiles = fso.GetFolder(Directory).FilesFor Each objFile In objFiles        strExt = UCase(fso.GetExtensionName(objFile.Path))        If InStr(ImageFilter, strExt)>0 Then                Set doc = app.Open(objFile.Path)                Set app.ActiveDocument = doc                ReSizeImg(doc)                Call app.DoJavaScriptFile(jsxFile)                Call doc.Close(psDoNotSaveChanges)                Set doc = Nothing       End IfNextSet app = NothingEnd Function jsx文件的代码: //存放缩略图的文件夹var outputFolderPath = "F:/12/Dvbbs8.2.0_Ac/dvbbs80/UploadFile/test"; var theDoc = activeDocument;var docName = theDoc.name; saveFile = new File(outputFolderPath + "/" + docName + ".jpg"); jpegSaveOptions = new JPEGSaveOptions(); jpegSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;jpegSaveOptions.quality = 8; theDoc.saveAs(saveFile, jpegSaveOptions, true, Extension.LOWERCASE); ------------------------------------------------------------------------------------------------------------- 这里附上那段创建Photoshop.JPEGSaveOptions对象失败的脚本,大家在自己电脑上试试会不会成功? 代码如下: Directory = "F:\12\Dvbbs8.2.0_Ac\dvbbs80\UploadFile\2008-1" '原始图像的文件夹NewDirectory = "F:\12\Dvbbs8.2.0_Ac\dvbbs80\UploadFile\test\" '保存缩小图的文件夹 Const psDoNotSaveChanges     = 2Const PsExtensionType_psLowercase = 2Const ImageFilter = "PSD;BMP;JPG;JPEG;GIF;PNG"Const psDisplayNoDialogs     = 3Const psStandardBaseline     = 1 Set app = CreateObject( "Photoshop.Application" ) limitWidth = 200 '最大寬度limitHeight = 160 '最大高度ImgResolution = 72 '解析度 app.bringToFront()Set jpgOpt = CreateObject("Photoshop.JPEGSaveOptions")With jpgOpt        .FormatOptions = psStandardBaseline        .Quality = 8 '0~12End Withapp.preferences.rulerUnits = 1 'psPixelsapp.DisplayDialogs = psDisplayNoDialogs Call Convert2JpegByFolder(Directory) Function ReSizeImg(doc)If doc.width <= limitWidth And doc.height <= limitHeight Then Exit Function rsWidth = doc.widthrsHeight = doc.heightScale = 1.0 if doc.width > limitWidth Then        Scale = limitWidth / (doc.width + 0.0)        rsWidth = doc.width * Scale        rsHeight = doc.height * ScaleEnd If if rsHeight > limitHeight Then        Scale = limitHeight / (doc.height + 0.0)        rsWidth = doc.width * Scale        rsHeight = doc.height * ScaleEnd If doc.resizeImage rsWidth, rsHeight, ImgResolution, 3End Function Function Convert2JpegByFolder(Directory)Set fso = CreateObject("Scripting.FileSystemObject")If Not fso.FolderExists(Directory) Then             MsgBox "Photo Directory NOT Exists."        Exit FunctionEnd IfSet objFiles = fso.GetFolder(Directory).FilesFor Each objFile In objFiles        strExt = UCase(fso.GetExtensionName(objFile.Path))        If InStr(ImageFilter, strExt)>0 Then                Set doc = app.Open(objFile.Path)                Set app.ActiveDocument = doc                ReSizeImg(doc)                doc.SaveAs NewDirectory & objFile.Name & ".jpg", jpgOpt, True, PsExtensionType_psLowercase                Call doc.Close(psDoNotSaveChanges)                Set doc = Nothing       End IfNextSet app = NothingEnd Function
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部