forked from vdcrim/AvsP-macros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Images batch processing.py
134 lines (112 loc) · 4.7 KB
/
Images batch processing.py
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
# -*- coding: utf-8 -*-
"""
Apply the Avisynth script in the current tab to all the selected input
files in a specified directory. Save as an image the current frame of
the result afterwards.
Date: 2012-11-13
Latest version: https://github.com/vdcrim/avsp-macros
Created originally for http://forum.doom9.org/showthread.php?p=1552739#post1552739
Changelog:
- fix previous bad cleanup
- the script is now taken from the current tab
- not restrict the input files to only JPEG
- allow specifying a different output directory
- move all the settings to the prompt
- fix Python 2.6 compatibility
- fix Unicode paths
- strip tags and sliders from the script before evaluating it
Copyright (C) 2012 Diego Fernández Gosende <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
"""
# ------------------------------------------------------------------------------
# run in thread
from os.path import splitext, basename, join
from glob import glob
self = avsp.GetWindow()
# Get defaults
in_dir = avsp.Options.get('in_dir', '')
input_list = avsp.Options.get('input_list', 'bmp;png;tif')
out_dir = avsp.Options.get('out_dir', '')
out_format = avsp.Options.get('out_format', _('Portable Network Graphics') + ' (*.png)')
suffix = avsp.Options.get('suffix', '_new')
quality = avsp.Options.get('quality', 90)
format_dict = dict([(name[0], ext) for ext, name in self.imageFormats.iteritems()])
# Prompt for options
while True:
options = avsp.GetTextEntry(title=_('Images batch processing'),
message=[_('The script in the current tab will be applied to all '
'the input files. \nPlease replace first the input path '
'in your source filter with "input_path", e.g. ImageSource'
'("input_path")'),
[_('Input files directory'),
_('Semicolon-separated list of input file extensions')],
[_('Output directory (blank = input directory)'),
_('Output format')],
[_('Output files suffix'),
_('Quality (JPEG only)'),
_('Save current values as default')]],
default=['', [in_dir, input_list],
[out_dir, sorted(format_dict.keys()) + [out_format]],
[suffix, (quality, 0, 100)]],
types=['sep', ['dir', ''], ['dir', 'list_read_only'],
['', 'spin', 'check']],
width=500)
if not options:
return
in_dir, input_list, out_dir, out_format, suffix, quality, save_options = options
if not in_dir:
avsp.MsgBox(_('Select the input files directory'), _('Error'))
else: break
# Save options
if save_options:
avsp.Options['in_dir'] = in_dir
avsp.Options['input_list'] = input_list
avsp.Options['out_dir'] = out_dir
avsp.Options['out_format'] = out_format
avsp.Options['suffix'] = suffix
avsp.Options['quality'] = quality
# Get input files
paths = []
for ext in input_list.split(';'):
paths.extend(glob(join(in_dir, '*.' + ext)))
if not paths:
avsp.MsgBox(_('No files with the given extensions in the selected input '
'directory'), _('Error'))
return
# Check if the script contains the path wildcard
if self.version > '2.3.1':
script = avsp.GetText(clean=True)
else:
script = self.getCleanText(avsp.GetText())
if not '"input_path"' in script:
avsp.MsgBox(_('Not "input_path" in the script'), _('Error'))
return
# Process and save the input files
if not out_dir:
out_dir = in_dir
ext = format_dict[out_format]
total = len(paths)
progress = avsp.ProgressBox(max=1000, title=_('Batch processing progress'))
for i, path in enumerate(paths):
if not progress.Update(i*1000.0/total, basename(path))[0]:
break
avsp.SetText(script.replace('"input_path"', u'"{0}"'.format(path), 1))
try:
avsp.SaveImage(join(out_dir, splitext(basename(path))[0] + suffix + ext),
quality=quality)
except:
avsp.MsgBox(_('Processing failed at ') + basename(path), _('Error'))
break
# Clean up
progress.Destroy()
avsp.HideVideoWindow()
avsp.SetText(script)