您的位置:首页 > 其它

diy toy: image auto-handler

2015-11-01 20:37 246 查看
备忘之:)

config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
<zoom width="1296" height="100">
</zoom>
<rotation angle="270">
</rotation>
<save>
<path>D:\0122</path>
</save>
<author prefix="xxoo">
</author>
<monitor total="10">
</monitor>
</config>


image-auto-handler.vbs

'Whatfor:  rename pictures with Exif property [artist]
'Author:   lichmama nextgodhand@163.com
'Runtime:  win-NT series(x86/x64), which installed WIA component.
'LICENSE:
'    Copyright ;copy; 2014-2015 lichmama nextgodhand@163.com
'    All rights reserved.
'    FOR NON-COMMERCIAL USE: you can use, copy, modify and distribute this code,
'    but please keep the information of LICENSE & Author.

'*ChangeLog:
'    1.add zoom and rotation
'    2.backup the folder [*.NEW] to config path
'    reedited: 2015/04/16
'    3.change the naming style, which supports the [alpha] property
'    3.*not keeping the folder [*.NEW]
'    reedited: 2015/10/27
'    4.add monitor function, which supports the [total] property
'    reedited: 2015/10/31, happy halloween:)
'    5.change the config file to config.xml [original, config.ini]
'    5.*add verifying for configurations
'    reedited: 2015/11/1

Set fso = CreateObject("scripting.filesystemobject")
Set imgfile = CreateObject("wia.imagefile")
Set sdp = CreateObject("scripting.dictionary")
Set regex = CreateObject("vbscript.regexp")
Set monitor = CreateObject("scripting.dictionary")
set config = new Config_Class

'================script starts!================
Call sdp.Add("PNG", "PNG FILE")
Call sdp.Add("GIF", "GIF FILE")
Call sdp.Add("BMP", "BMP FILE")
Call sdp.Add("JPG", "JPG FILE")

If WScript.Arguments.Count = 0 Then
Call Quit_Job(True)
Else
path = WScript.Arguments(0)
If Not fso.FolderExists(path) Then
Call Quit_Job(True)
End If
End If

If Not ReadConfig() Then
Call Wscript.Echo("[配置文件错误]:运行前请保证配置文件config.ini存在,并且正确!")
Call Quit_Job(False)
End If

If Not fso.FolderExists(path & ".NEW") Then
Call fso.CreateFolder(path & ".NEW")
End If

For Each picture In fso.GetFolder(path).Files
extension = UCase(fso.GetExtensionName(picture))
If sdp.Exists(extension) Then
Call imgfile.LoadFile(picture)
For index = 1 To imgfile.Properties.Count
name = imgfile.Properties(index).Name
If name = "Artist" Then
regex.Pattern ="\d*"
value = imgfile.Properties(index).Value
value = regex.Execute(value)(0)
index = CInt(value)
regex.Pattern = "^(0*)"
value = regex.Replace(value, config.author_prefix)
Call Zoom_Rotate()
Call imgfile.SaveFile(path & ".NEW\" & value & "." & extension)
monitor.Item(index) = path & ".NEW\" & value & "." & extension
Exit For
End If
Next
End If
Next
Call Monitor_Total()

'move the folder [*.NEW] to config path
regex.Pattern = "([^\\]+)$"
dest = fso.GetAbsolutePathName(path)
dest = regex.Execute(dest)(0).SubMatches(0)
For i=0 To config.save_path.Count-1
Call fso.CopyFolder(path & ".NEW", config.save_path.Item(i) & "\" & dest)
Next
Call fso.DeleteFolder(path & ".NEW")

MsgBox "批处理执行完毕!",vbInformation,"Image Auto-Handler v0.1"
Call Quit_Job(False)
'================script ends here==============
Function ReadConfig()
On Error Resume Next
xmlsource = Replace(WScript.ScriptFullName, WScript.ScriptName, "config.xml")
If fso.FileExists(xmlsource) Then
Call config.init_config(xmlsource)
For i = 1 To config.monitor_total
Call monitor.Add(i, "")
Next
For i = 0 To config.save_path.Count-1
If Not fso.FolderExists(config.save_path.Item(i)) Then
WScript.Echo "[配置文件错误]:保存路径不存在!"
Call Quit_Job(False)
End If
Next
ReadConfig = True
Else
ReadConfig = False
End If
If Err.Number <> 0 Then
ReadConfig = False
End If
End Function

Sub Monitor_Total()
regex.Pattern = "(" & config.author_prefix & ")\d+"
If monitor.Item(1) = "" Then
For i = 2 To monitor.Count
If monitor.Item(i) <> "" Then
clone = regex.Replace(monitor.Item(i), "$1" & 1)
monitor.Item(1) = clone
Call fso.CopyFile(monitor.Item(i), monitor.Item(1))
Exit For
End If
Next
End If

For i = 2 To monitor.Count
If monitor.Item(i) = "" Then
clone = regex.Replace(monitor.Item(i-1), "$1" & i)
monitor.Item(i) = clone
Call fso.CopyFile(monitor.Item(i-1), monitor.Item(i))
End If
Next
End Sub

Sub Zoom_Rotate()
Set imgproc = CreateObject("wia.imageprocess")
'zoom
Call imgproc.Filters.Add(imgproc.FilterInfos("Scale").FilterID)
imgproc.Filters(1).Properties("MaximumWidth") = config.zoom_width
imgproc.Filters(1).Properties("MaximumHeight") = config.zoom_width
'rotate
Call imgproc.Filters.Add(imgproc.FilterInfos("RotateFlip").FilterID)
imgproc.Filters(2).Properties("RotationAngle") = 0
'apply
Set imgfile = imgproc.Apply(imgfile)
End Sub

Sub Quit_Job(force)
If force = True Then
MsgBox "[使用方法]: 将文件夹拖放到脚本上,开始重命名图片。", vbInformation, "Image Auto-Handler v0.1"
End If
Set fso = Nothing
Set imgfile = Nothing
Set sdp = Nothing
Set regex = Nothing
Set monitor = Nothing
Call WScript.Quit()
End Sub

Class Config_Class
Private my_zoom_width
Private my_zoom_height
Private my_rotation_angle
Private my_save_path
Private my_author_prefix
Private my_monitor_total

Public Sub init_config(xmlsource)
Set xml = CreateObject("msxml2.domdocument")
Call xml.load(xmlsource)

Set zoom = xml.getElementsByTagName("zoom").item(0)
my_zoom_width = zoom.attributes.getNamedItem("width").text
my_zoom_height = CInt(zoom.attributes.getNamedItem("height").text)

Set rotation = xml.getElementsByTagName("rotation").item(0)
my_rotation_angle = rotation.attributes.getNamedItem("angle").text

Set save = xml.getElementsByTagName("save").item(0)
Set my_save_path = CreateObject("scripting.dictionary")
For i = 0 To save.childNodes.length - 1
Call my_save_path.Add(i, save.childNodes.item(i).text)
Next

Set author = xml.getElementsByTagName("author").item(0)
my_author_prefix = author.attributes.getNamedItem("prefix").text

Set xml_monitor = xml.getElementsByTagName("monitor").item(0)
my_monitor_total = xml_monitor.attributes.getNamedItem("total").text
End Sub

Public Property Get zoom_width
zoom_width = my_zoom_width
End Property

Public Property Get zoom_height
zoom_height = my_zoom_height
End Property

Public Property Get rotation_angle
rotation_angle = my_rotation_angle
End Property

Public Property Get save_path
Set save_path = my_save_path
End Property

Public Property Get author_prefix
author_prefix = my_author_prefix
End Property

Public Property Get monitor_total
monitor_total = my_monitor_total
End Property
End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: