forked from vdcrim/AvsP-macros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuto-crop.py
224 lines (197 loc) · 8.32 KB
/
Auto-crop.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
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
# -*- coding: utf-8 -*-
"""
Crop borders from the script in the current tab
Date: 2012-10-24
Latest version: https://github.com/vdcrim/avsp-macros
Changelog:
- fix Python 2.6 compatibility
- fix number of samples
- speed up a bit the autocrop detection if the original AviSynth clip is
keep on memory (%YUV or %CLR are set to appear on the status bar)
- check the chroma subsampling also for the new AviSynth 2.6 colorspaces
(AvsPmod 2.4.0+)
- fix clips with less frames than samples
Copyright (C) 2012 Diego Fernández Gosende <dfgosende@gmail.com>
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>.
"""
# PREFERENCES
# Set to False to not ask for settings
show_prompt = True
# Values used with show_prompt = False
samples = 10 # number of frames analyzed. Start and end of the video are skipped to avoid credits
tol = 70 # tolerance (0-255)
overcrop = True # Up or down crop values because of chroma subsampling
insert = True # insert Crop() at the end of the script
refresh = True # update and show if hidden the video preview
# ------------------------------------------------------------------------------
# run in thread
from collections import defaultdict
import wx
def autocrop(samples=10, tol=70, overcrop=True, insert=True, refresh=True,
show_prompt=False):
"""Crop borders from the script in the current tab"""
# Get options
if show_prompt:
samples = avsp.Options.get('samples', samples)
tol = avsp.Options.get('tol', tol)
overcrop = avsp.Options.get('overcrop', overcrop)
insert = avsp.Options.get('insert', insert)
refresh = avsp.Options.get('refresh', refresh)
options = avsp.GetTextEntry(
message=[[_('Samples'), _('Tolerance'), _('Overcrop')],
[_('Apply to script'), _('Update preview')]],
default=[[(samples, 1, 100), (tol, 0, 255), overcrop],
[insert, refresh]],
title=_('Auto-crop'),
types=[['spin', 'spin', 'check'], ['check', 'check']],
width=200)
if not options:
return
samples, tol, overcrop, insert, refresh = options
avsp.Options['samples'] = samples
avsp.Options['tol'] = tol
avsp.Options['overcrop'] = overcrop
avsp.Options['insert'] = insert
avsp.Options['refresh'] = refresh
# Get crop values for a number of frames
frames = avsp.GetVideoFramecount()
avs = avsp.GetWindow().currentScript
if not frames or avs.AVI.IsErrorClip():
avsp.MsgBox(_('Error loading the script'), _('Error'))
return
samples = min(samples, frames)
if samples <= 2:
frames = range(samples)
else:
def float_range(start=0, end=10, step=1):
'''Range with float step'''
while start < end:
yield int(round(start))
start += step
frames = float_range(frames/10, 9*frames/10 - 1, 8.0*frames/(10*samples))
progress = avsp.ProgressBox(samples, _('Analyzing frames...'), _('Auto-crop'))
crop_values = []
for i, frame in enumerate(frames):
if not progress.Update(i)[0]:
progress.Destroy()
return
crop_values.append(autocrop_frame(frame, tol))
progress.Destroy()
# Get final crop values
final_crop_values = []
for seq in zip(*crop_values):
value = get_crop_value(seq)
value = check_subsampling(value, avs.AVI.Colorspace, not len(final_crop_values) % 2, overcrop)
final_crop_values.append(value)
if insert:
txt = '' if avsp.GetText().endswith('\n') else '\n'
avsp.InsertText(txt + 'Crop({0}, {1}, -{2}, -{3})'.format(*final_crop_values))
if refresh:
avsp.ShowVideoFrame(forceRefresh=True)
return final_crop_values
def autocrop_frame(frame, tol=70):
"""Return crop values for a specific frame"""
avs_clip = avsp.GetWindow().currentScript.AVI
width, height = avs_clip.vi.width, avs_clip.vi.height
version = avsp.GetWindow().version
if version > '2.3.1' or avs_clip.clipRaw is not None: # Get pixel color from the original clip
avs_clip._GetFrame(frame)
get_pixel_color = avs_clip.GetPixelRGB if avs_clip.IsRGB else avs_clip.GetPixelYUV
else: # Get pixel color from the video preview (slower)
bmp = wx.EmptyBitmap(width, height)
mdc = wx.MemoryDC()
mdc.SelectObject(bmp)
avs_clip.DrawFrame(frame, mdc.GetHDC())
img = bmp.ConvertToImage()
get_pixel_color = lambda x, y : (img.GetRed(x, y), img.GetGreen(x, y), img.GetBlue(x, y))
w, h = width - 1, height - 1
top_left0, top_left1, top_left2 = get_pixel_color(0, 0)
bottom_right0, bottom_right1, bottom_right2 = get_pixel_color(w, h)
top = bottom = left = right = 0
# top & bottom
top_done = bottom_done = False
for i in range(height):
for j in range(width):
if not top_done:
color0, color1, color2 = get_pixel_color(j, i)
if (abs(color0 - top_left0) > tol or
abs(color1 - top_left1) > tol or
abs(color2 - top_left2) > tol):
top = i
top_done = True
if not bottom_done:
color0, color1, color2 = get_pixel_color(j, h - i)
if (abs(color0 - bottom_right0) > tol or
abs(color1 - bottom_right1) > tol or
abs(color2 - bottom_right2) > tol):
bottom = i
bottom_done = True
if top_done and bottom_done: break
else: continue
break
# left & right
left_done = right_done = False
for j in range(width):
for i in range(height):
if not left_done:
color0, color1, color2 = get_pixel_color(j, i)
if (abs(color0 - top_left0) > tol or
abs(color1 - top_left1) > tol or
abs(color2 - top_left2) > tol):
left = j
left_done = True
if not right_done:
color0, color1, color2 = get_pixel_color(w - j, i)
if (abs(color0 - bottom_right0) > tol or
abs(color1 - bottom_right1) > tol or
abs(color2 - bottom_right2) > tol):
right = j
right_done = True
if left_done and right_done: break
else: continue
break
return left, top, right, bottom
def get_crop_value(seq):
"""Get the most repeated value on a sequence if it repeats more than 50%,
the minimum value otherwise"""
d = defaultdict(int)
for i in seq:
d[i] += 1
max = sorted(d.keys(), key=lambda x:-d[x])[0]
if d[max] > len(seq) / 2:
return max
else:
ret_val = max
for value in seq:
if value < ret_val:
ret_val = value
return ret_val
def check_subsampling(value, colorspace, horizontal, overcrop):
'''Up or down crop values because of chroma subsampling'''
colorspace = colorspace.lower()
if colorspace in ('yuy2', 'yv16'):
if value % 2 and horizontal:
if overcrop:
value += 1
else:
value -= 1
elif colorspace == 'yv411':
q, r = divmod(value, 4)
if r and horizontal:
value = q * 4 + 4 if overcrop else q * 4
elif colorspace == 'yv12' and value % 2:
if overcrop:
value += 1
else:
value -= 1
return value
return autocrop(samples, tol, overcrop, insert, refresh, show_prompt)