forked from OndrejTexler/Few-Shot-Patch-Based-Training
-
Notifications
You must be signed in to change notification settings - Fork 8
/
deflicker.py
116 lines (102 loc) · 3.68 KB
/
deflicker.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
"""
deflicker.py
------------
Remove flicker from a series of images.
This scripts reads images from a specified directory to determine an RGB
"timeseries", smooths the RGB timeseries with a square filter of specified
width, and either outputs plots of the smoothed and unsmoothed RGB timeseries
or adjusts the RGB values of each image such that their RGB values match the
smoothed values.
To use this script, run ``python deflicker.py <directory> <width>
[options]``. ``<directory>`` should specify a path to a folder than contains
the image files that are to the deflickered. The image names must contain
numbers somewhere, and the images will included in the timeseries in ascending
numerical order. <width> specified the width (in images) of the square filter
used the smooth the image values. Other options include
``--plot <file>``:
do not output images with adjusted means; instead, print a plot
of the RGB timeseries before and after smoothing to a PNG image in
``<file>``. If ``<file>`` already exists, it may be overwritten.
``--outdir <output>``:
output images with adjusted means in the directory specified by
``<output>``. If the directory is the same as ``<directory>``, the
smoothing is done in-place and the input files are overwritten.
.. moduleauthor Tristan Abbott
"""
from libdeflicker import meanRGB, squareFilter, relaxToMean, toIntColor
import os
import re
import sys
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
if __name__ == "__main__":
# Process input arguments
if len(sys.argv) < 3:
print ('Usage: python deflicker.py <directory> <width> [..]')
exit(0)
loc = sys.argv[1]
w = int(sys.argv[2])
__plot = False
__outdir = False
for ii in range(3, len(sys.argv)):
a = sys.argv[ii]
if a == '--plot':
__plot = True
__file = sys.argv[ii+1]
elif a == '--outdir':
__outdir = True
__output = sys.argv[ii+1]
# Just stop if not told to do anything
if not (__plot or __outdir):
print ('Exiting without doing anything')
exit(0)
# Get list of image names in order
loc = sys.argv[1]
f = os.listdir(loc)
n = []
ii = 0
while ii < len(f):
match = re.search('\d+', f[ii])
if match is not None:
n.append(int(match.group(0)))
ii += 1
else:
f.pop(ii)
n = np.array(n)
i = np.argsort(n)
f = [f[ii] for ii in i]
# Load images and calculate smoothed RGB curves
print ('Calculating smoothed sequence')
n = len(f)
rgb = np.zeros((n, 3))
ii = 0
for ff in f:
img = np.asarray(Image.open('%s/%s' % (loc, ff))) / 255.
rgb[ii,:] = meanRGB(img)
ii += 1
# Filter series
rgbi = np.zeros(rgb.shape)
for ii in range(0,3):
rgbi[:,ii] = squareFilter(rgb[:,ii], w)
# Print initial and filtered series
if __plot:
print ('Plotting smoothed and unsmoothed sequences in %s') % __file
plt.subplot(1, 2, 1)
plt.plot(rgb[:,0], 'r', rgb[:,1], 'g', rgb[:,2], 'b')
plt.title('Unfiltered RGB sequence')
plt.subplot(1, 2, 2)
plt.plot(rgbi[:,0], 'r', rgbi[:,1], 'g', rgbi[:,2], 'b')
plt.title('Filtered RGB sequence (w = %d)' % w)
plt.savefig(__file)
# Process images sequentially
if __outdir:
print ('Processing images')
ii = 0
for ff in f:
img = np.asarray(Image.open('%s/%s' % (loc, ff))) / 255.
relaxToMean(img, rgbi[ii,:])
jpg = Image.fromarray(toIntColor(img))
jpg.save('%s/%s' % (__output, ff))
ii += 1
print ('Finished')