-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale.bat
310 lines (243 loc) · 8.72 KB
/
scale.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
@if (@X)==(@Y) @end /* JScript comment
@echo off
cscript //E:JScript //nologo "%~f0" %*
::pause
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms630819(v=vs.85).aspx
var imageFile = new ActiveXObject("WIA.ImageFile");
var imageProcess = new ActiveXObject("WIA.ImageProcess");
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var ARGS=WScript.Arguments;
/******
Scale filter description:
Scales image to the specified Maximum Width and Maximum Height preserving
Aspect Ratio if necessary.
MaximumWidth - Set the MaximumWidth property to the width (in pixels)
that you wish to scale the image to.
MaximumHeight - Set the MaximumHeight property to the height (in pixels)
that you wish to scale the image to.
PreserveAspectRatio - Set the PreserveAspectRatio property to True
[the default] if you wish to maintain the current aspect
ration of the image, otherwise False and the image will
be stretched to the MaximumWidth and MaximumHeight
FrameIndex - Set the FrameIndex property to the index of a frame if
you wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
******/
//defaults
var maxWidth=0;
var maxHeight=0;
var pRatio=true;
var frameIndex=0;
var source="";
var target="";
var force=false;
var height=0;
var width=0;
var percentage=false;
////////////////////////////
//// //
/**/ var QUALITY=100; //
//// //
////////////////////////////
function existsFile(path){
if (fileSystem.FileExists(path))
return true;
}
function existsFolder(path){
if (fileSystem.FolderExists(path))
return true;
}
function deleteFile(path){
fileSystem.DeleteFile(path);
}
function loadImage(image,imageFile){
try{
image.LoadFile(imageFile);
}catch(err){
WScript.Echo("Probably "+imageFile+" is not a valid image file");
WScript.Echo(err.message);
WScript.Quit(30);
}
height=image.Height;
width=image.Width;
}
function ID2Format(id){
var ids={};
ids["{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"]="BPM";
ids["{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"]="PNG";
ids["{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"]="GIF";
ids["{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"]="JPG";
ids["{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"]="TIFF";
return ids[id];
}
function format2ID(format){
formats={};
formats["BMP"]="{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
formats["PNG"]="{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
formats["GIF"]="{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
formats["JPG"]="{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
formats["TIFF"]="{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
return formats[format];
}
function convert(image,format){
var ic=imageProcess.Filters.Count;
var filterFormat=format2ID(format);
if(filterFormat==null){
WScript.Echo("not supported target format "+format);
WScript.Quit(90);
}
imageProcess.Filters.Add(imageProcess.FilterInfos("Convert").FilterID);
imageProcess.Filters(ic+1).Properties("FormatID").Value = filterFormat;
imageProcess.Filters(ic+1).Properties("Quality").Value = QUALITY;
}
function scale(){
if(maxHeight<=0){
WScript.Echo("MaximumHeight ("+maxHeight+") should be bigger than 0");
WScript.Quit(80);
}
if(maxWidth<=0){
WScript.Echo("MaximumHeight ("+maxWidth+") should be bigger than 0");
WScript.Quit(81);
}
var ic=imageProcess.Filters.Count;
//var filterFormat=format2ID(format);
imageProcess.Filters.Add(imageProcess.FilterInfos("Scale").FilterID);
imageProcess.Filters(ic+1).Properties("MaximumWidth").Value = maxWidth;
imageProcess.Filters(ic+1).Properties("MaximumHeight").Value = maxHeight;
//WScript.Echo(pRatio+"::"+maxWidth+"::"+maxHeight+">>"+width+"++"+height);
imageProcess.Filters(ic+1).Properties("PreserveAspectRatio").Value = pRatio;
imageProcess.Filters(ic+1).Properties("FrameIndex").Value = frameIndex;
}
function fromPerc(){
maxWidth=Math.round((width*maxWidth)/100);
maxHeight=Math.round((height*maxHeight)/100);
if(maxHeight==0)
maxHeight=1;
if(maxWidth==0)
maxWidth=1;
}
function printHelp(){
WScript.Echo( WScript.ScriptName + " - resizes an image");
WScript.Echo(" ");
WScript.Echo(WScript.ScriptName + "-source source.file -target file.format [-max-height height] [-max-width width] [-percentage yes|no] [-keep-ratio yes|no] [-frame-index -0.5..1] ");
WScript.Echo("-source - the image that will flipped or rotated.");
WScript.Echo("-target - the file where the transformations will be saved in.If the file extension format is different than the source it will be converted to the pointed one.Supported formats are BMp,JPG,GIF,TIFF,PNG");
WScript.Echo("-percentage - whether the rescale will be calculated in pixels or in percentages.If yes percentages will be used.Default is no.");
WScript.Echo("-force - If yes and the target file already exists , it will be overwritten");
WScript.Echo("-max-height - max height of the image");
WScript.Echo("-max-width - max width of the image");
WScript.Echo("-keep-ratio - if dimensions ratio will be preserved.Default is yes");
WScript.Echo("-frame-index - Have no idea what this is used for , but it is pressented in the rotation filter capabilities.Images with this and without looks the same.Accepted values are from -0.5 to 1");
}
function parseArguments(){
if (WScript.Arguments.Length<4 || ARGS.Item(1).toLowerCase() == "-help" || ARGS.Item(1).toLowerCase() == "-h" ) {
printHelp();
WScript.Quit(0);
}
if (WScript.Arguments.Length % 2 == 1 ) {
WScript.Echo("Illegal arguments ");
printHelp();
WScript.Quit(1);
}
//ARGS
for(var arg = 0 ; arg<ARGS.Length-1;arg=arg+2) {
if (ARGS.Item(arg) == "-source") {
source = ARGS.Item(arg +1);
}
if (ARGS.Item(arg) == "-target") {
target = ARGS.Item(arg +1);
}
if (ARGS.Item(arg).toLowerCase() == "-force" && (ARGS.Item(arg +1).toLowerCase() == "yes" || ARGS.Item(arg +1).toLowerCase() == "true") ) {
force=true;
}
if (ARGS.Item(arg).toLowerCase() == "-percentage" && (ARGS.Item(arg +1).toLowerCase() == "yes" || ARGS.Item(arg +1).toLowerCase() == "true") ) {
percentage=true;
}
if (ARGS.Item(arg).toLowerCase() == "-keep-ratio" && (ARGS.Item(arg +1).toLowerCase() == "no" || ARGS.Item(arg +1).toLowerCase() == "false") ) {
pRatio=false;
}
if (ARGS.Item(arg).toLowerCase() == "-max-width") {
try {
maxWidth=parseInt(ARGS.Item(arg +1));
} catch (err){
WScript.Echo("Wrong argument:");
WScript.Echo(err.message);
WScript.Quit(10);
}
}
if (ARGS.Item(arg).toLowerCase() == "-max-height") {
try {
maxHeight=parseInt(ARGS.Item(arg +1));
} catch (err){
WScript.Echo("Wrong argument:");
WScript.Echo(err.message);
WScript.Quit(15);
}
}
if (ARGS.Item(arg).toLowerCase() == "-frame-index") {
try {
frameIndex=parseFloat(ARGS.Item(arg +1));
if(frameIndex<-0.5 || frameIndex > 1){
WScript.Echo("Wrong argument - frame index should be between -0.5 and 1");
WScript.Quit(25);
}
} catch (err){
WScript.Echo("Wrong argument:");
WScript.Echo(err.message);
WScript.Quit(20);
}
}
}
if (target==""){
WScript.Echo("Target file not passed");
WScript.Quit(5);
}
if(source==""){
WScript.Echo("Source file not passed");
WScript.Quit(6);
}
}
parseArguments();
if(!existsFile(source)){
WScript.Echo("Source image: " + source +" does not exists");
WScript.Quit(40);
}
if(existsFile(target) && !force){
WScript.Echo("Target image: " + target +" already exists");
WScript.Quit(45);
}
if(existsFolder(target)){
WScript.Echo("There's existing folder with the target file (" + target +") name");
WScript.Quit(46);
}
if(existsFile(target) && force){
deleteFile(target);
}
var targetFormat=target.split(".")[target.split(".").length-1].toUpperCase();
loadImage(imageFile,source);
var sourceFormat=ID2Format(imageFile.FormatID);
if(maxWidth==0 && !percentage){
maxWidth=width;
}
if(maxHeight==0 && !percentage){
maxHeight=height;
}
if(maxWidth==0 && percentage){
maxWidth=100;
}
if(maxHeight==0 && percentage){
maxHeight=100;
}
if(percentage){
fromPerc();
}
///
scale();
///
if (sourceFormat !== targetFormat ){
convert(resImg,targetFormat);
}
var resImg=imageProcess.Apply(imageFile);
resImg.SaveFile(target);