1. 程式人生 > >利用PHOTOSHOP CS 和 指令碼實現批量自動處理圖片

利用PHOTOSHOP CS 和 指令碼實現批量自動處理圖片

OptionExplicit

''Batch converter  for Photoshop CS2(Not tested for CS1)
'
author: cangwu.lee # gmail.com

'saving parameter
Const psSaveChanges          =1
Const psDoNotSaveChanges     =2
Const psPromptToSaveChanges  =3

'Jpeg Option
Const psStandardBaseline     =1
Const psOptimizedBaseline    =2
Const psProgressive          
=3

'saveAs PsExtensionType
Const PsExtensionType_psLowercase  =2
Const PsExtensionType_psUppercase  =3

'PsDialogModes
Const psDisplayAllDialogs    =1
Const psDisplayErrorDialogs  =2
Const psDisplayNoDialogs     =3

const imageFilter =";PSD;BMP;JPG;JPEG;GIF;PNG;"'image file type to process

dim directory, jsxFile, jpeg_quality
directory    
="c: emp"'change  to point to your photos folder
jsxFile      ="C:/Program Files/Adobe/Adobe Photoshop CS2/Presets/Scripts/exif_info.jsx"
jpeg_quality 
=8'between 1~12
'
將這3個變數改成你的實際需要。

call convert2jpegByFolder()




'
Function convert2jpegByFolder()
    
Dim i
    
Dim fs, objFiles, objFile, ext, idone, ierror
    
Set fs =CreateObject("Scripting.FileSystemObject")
    
IfNot fs.fileExists(jsxFile) Then
        
MsgBox"The .jsx file NOT exists."
        
ExitFunction
    
EndIf
    
IfNot fs.folderexists(directory) Then
        
MsgBox"Photo directory NOT exists."
        
ExitFunction
    
EndIf

    
Dim appRef '  As Photoshop.Application
Dim docRef ' As Photoshop.Document
    
    
Set appRef =GetObject("""Photoshop.Application")
    
If Err.Number <>0Then
        
Set appRef =CreateObject("Photoshop.Application")
    
EndIf
    
If appRef IsNothingThen
        
MsgBox"Photoshop Appliaction object exception."
        
ExitFunction
    
EndIf

    
Dim jpgOpt'Photoshop.JPEGSaveOptions
Set jpgOpt =CreateObject("Photoshop.JPEGSaveOptions")
    
With jpgOpt
        .FormatOptions 
= psStandardBaseline
        .Quality 
= jpeg_quality '0~12
EndWith

    appRef.Preferences.TypeUnits 
=1'for PsTypeUnits --> 1 (psPixels)
    appRef.DisplayDialogs = psDisplayNoDialogs

    
Set objFiles = fs.GetFolder(directory).Files
    
If Err.Number <>0ThenMsgBox Err.Description: ExitFunction
    
OnErrorGoTo0
    
    
MsgBox"All file in the directory conuters:"& objFiles.Count &chr(13&chr(10&"Click 'OK' to continue."
    idone 
=0
    
ForEach objFile In objFiles
        ext 
=";"&UCase(fs.GetExtensionName(objFile.Path)) &";"
        
Ifinstr(1, imageFilter, ext, 1)>0Then
        
            
'set docRef = appRef.Documents.Add(1024, 768)
Set docRef = appRef.Open(objFile.Path)
            
If docRef IsNothingThen
                
MsgBox"Create/open image document failed."
            
Else
                
'setting current picture document
Set appRef.ActiveDocument = docRef
                
                
'begin to process
OnErrorResumeNext
                
Call appRef.DoJavaScriptFile(jsxFile)
                
If Err.Number =0Then
                    
For i =0To30000
                        
'waiting for some times
Next
                    
Call docRef.SaveAs(objFile.Path &".jpg", jpgOpt, True, PsExtensionType_psLowercase)
                
Else
                    ierror 
= ierror +1
                
EndIf
                
OnErrorGoTo0
                
'close document
Call docRef.Close(psDoNotSaveChanges)
                idone 
= idone +1''counter
Set docRef =Nothing' free document object
EndIf
        
EndIf
        
    
Next
    
MsgBox"Image files:"& idone &chr(13&chr(10&"File error:"& ierror


    
Set appRef =Nothing

End Function