|
@@ -1,3 +1,4 @@
|
|
1
|
+#!/usr/bin/env python
|
1
|
2
|
# Copyright (C) 2014 The Android Open Source Project
|
2
|
3
|
#
|
3
|
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -13,19 +14,16 @@
|
13
|
14
|
# limitations under the License.
|
14
|
15
|
|
15
|
16
|
"""
|
16
|
|
-Script to take a set of frames (PNG files) for a recovery animation and turn
|
17
|
|
-it into a single output image which contains the input frames interlaced by
|
18
|
|
-row. Run with the names of all the input frames on the command line. Specify
|
19
|
|
-the name of the output file with -o (or --output), and optionally specify the
|
20
|
|
-number of frames per second (FPS) with --fps (default: 20).
|
21
|
|
-
|
22
|
|
-e.g.
|
23
|
|
-interlace-frames.py --fps 20 --output output.png frame0.png frame1.png frame3.png
|
|
17
|
+Script to take a set of frames (PNG files) for a recovery animation
|
|
18
|
+and turn it into a single output image which contains the input frames
|
|
19
|
+interlaced by row. Run with the names of all the input frames on the
|
|
20
|
+command line, in order, followed by the name of the output file.
|
24
|
21
|
"""
|
25
|
22
|
|
26
|
23
|
from __future__ import print_function
|
27
|
24
|
|
28
|
25
|
import argparse
|
|
26
|
+import os.path
|
29
|
27
|
import sys
|
30
|
28
|
try:
|
31
|
29
|
import Image
|
|
@@ -35,7 +33,7 @@ except ImportError:
|
35
|
33
|
sys.exit(1)
|
36
|
34
|
|
37
|
35
|
|
38
|
|
-def interlace(output, fps, inputs):
|
|
36
|
+def interlace(output, inputs):
|
39
|
37
|
frames = [Image.open(fn).convert("RGB") for fn in inputs]
|
40
|
38
|
assert len(frames) > 0, "Must have at least one input frame."
|
41
|
39
|
sizes = set()
|
|
@@ -60,21 +58,57 @@ def interlace(output, fps, inputs):
|
60
|
58
|
|
61
|
59
|
meta = PngImagePlugin.PngInfo()
|
62
|
60
|
meta.add_text("Frames", str(N))
|
63
|
|
- meta.add_text("FPS", str(fps))
|
64
|
61
|
|
65
|
62
|
out.save(output, pnginfo=meta)
|
66
|
63
|
|
67
|
64
|
|
|
65
|
+def deinterlace(output, input):
|
|
66
|
+ # Truncate the output filename extension if it's '.png'.
|
|
67
|
+ if os.path.splitext(output)[1].lower() == '.png':
|
|
68
|
+ output = output[:-4]
|
|
69
|
+
|
|
70
|
+ img2 = Image.open(input)
|
|
71
|
+ print(img2.mode)
|
|
72
|
+ palette = img2.getpalette()
|
|
73
|
+ img = img2.convert("RGB")
|
|
74
|
+ num_frames = int(img.info.get('Frames', 1))
|
|
75
|
+ print('Found %d frames in %s.' % (num_frames, input))
|
|
76
|
+ assert num_frames > 0, 'Invalid Frames meta.'
|
|
77
|
+
|
|
78
|
+ # palette = img.getpalette()
|
|
79
|
+ print(palette)
|
|
80
|
+
|
|
81
|
+ width, height = img.size
|
|
82
|
+ height /= num_frames
|
|
83
|
+ for k in range(num_frames):
|
|
84
|
+ out = Image.new('RGB', (width, height))
|
|
85
|
+ out.info = img.info
|
|
86
|
+ for i in range(width):
|
|
87
|
+ for j in range(height):
|
|
88
|
+ out.putpixel((i, j), img.getpixel((i, j * num_frames + k)))
|
|
89
|
+ # out.putpalette(img.getpalette(), rawmode='RGB')
|
|
90
|
+ out2 = out.convert(mode='P', palette=palette)
|
|
91
|
+ #out2 = out
|
|
92
|
+ print(out2.mode)
|
|
93
|
+ # out2.putpalette(palette)
|
|
94
|
+ filename = '%s%02d.png' % (output, k)
|
|
95
|
+ out2.save(filename)
|
|
96
|
+ print('Frame %d written to %s.' % (k, filename))
|
|
97
|
+
|
|
98
|
+
|
68
|
99
|
def main(argv):
|
69
|
|
- parser = argparse.ArgumentParser()
|
70
|
|
- parser.add_argument('--fps', default=20)
|
|
100
|
+ parser = argparse.ArgumentParser(description='Parse')
|
|
101
|
+ parser.add_argument('--deinterlace', '-d', action='store_true')
|
71
|
102
|
parser.add_argument('--output', '-o', required=True)
|
72
|
103
|
parser.add_argument('input', nargs='+')
|
73
|
104
|
args = parser.parse_args(argv)
|
74
|
105
|
|
75
|
|
- interlace(args.output, args.fps, args.input)
|
|
106
|
+ if args.deinterlace:
|
|
107
|
+ # args.input is a list, and we only process the first when deinterlacing.
|
|
108
|
+ deinterlace(args.output, args.input[0])
|
|
109
|
+ else:
|
|
110
|
+ interlace(args.output, args.input)
|
76
|
111
|
|
77
|
112
|
|
78
|
113
|
if __name__ == '__main__':
|
79
|
114
|
main(sys.argv[1:])
|
80
|
|
-
|