-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_processer.py
56 lines (42 loc) · 1.62 KB
/
image_processer.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
from PIL import Image
from os import listdir
import sys
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
# ffmpeg -i in.mp4 -vf fps=20 -s 48x36 -vsync 0 frames/f%5d.png
def main(frames_folder, output):
file_n = 0
frame_list = []
lastFrame = []
for fr in listdir(frames_folder):
print(f"{fr} / {len(listdir(frames_folder))}", end="\r")
im = Image.open(frames_folder+f"/{fr}").convert("LA")
pixels = list(im.getdata())
for p in range(len(pixels)):
if lastFrame and round(pixels[p][0]/pixels[p][1], 1) != lastFrame[p] or not lastFrame:
pixels[p] = round(pixels[p][0]/pixels[p][1], 1) # the pixel is not repeated
else:
pixels[p] = None
lastFrame = pixels
pixels = list(chunks(pixels, im.width))
p_dict = {}
for a in range(len(pixels)):
for b in range(len(pixels[a])):
if pixels[a][b] is not None:
p_dict[f"{a}-{b}"] = pixels[a][b]
frame_list.append(p_dict)
split = 1400
if len(frame_list) > split or (file_n * (split + 1)) + len(frame_list) >= len(listdir(frames_folder)):
with open(f"{output}{file_n}.js", "w") as f:
f.write(f"var frames{file_n}={frame_list};\n")
print(f"\nsaved frames{file_n} to file")
frame_list = []
file_n += 1
return file_n
if __name__ == '__main__':
if len(sys.argv) < 3:
main("/frames/", "docs/frames.js")
else:
print(sys.argv[1], sys.argv[2])
main(sys.argv[1], sys.argv[2])