blob: 3e777b4705d1db7bbcd7f3777f2fa464910d71fa [file] [log] [blame]
Doug Zongker5120c9f2014-03-11 12:39:33 -07001# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Tao Baob723f4f2015-12-11 15:18:51 -080015"""
16Script to take a set of frames (PNG files) for a recovery animation and turn
17it into a single output image which contains the input frames interlaced by
18row. Run with the names of all the input frames on the command line. Specify
19the name of the output file with -o (or --output), and optionally specify the
20number of frames per second (FPS) with --fps (default: 20).
Doug Zongker5120c9f2014-03-11 12:39:33 -070021
Tao Baob723f4f2015-12-11 15:18:51 -080022e.g.
23interlace-frames.py --fps 20 --output output.png frame0.png frame1.png frame3.png
24"""
25
26from __future__ import print_function
27
28import argparse
Doug Zongker5120c9f2014-03-11 12:39:33 -070029import sys
30try:
31 import Image
32 import PngImagePlugin
33except ImportError:
Tao Baob723f4f2015-12-11 15:18:51 -080034 print("This script requires the Python Imaging Library to be installed.")
Doug Zongker5120c9f2014-03-11 12:39:33 -070035 sys.exit(1)
36
Doug Zongker5120c9f2014-03-11 12:39:33 -070037
Tao Baob723f4f2015-12-11 15:18:51 -080038def interlace(output, fps, inputs):
39 frames = [Image.open(fn).convert("RGB") for fn in inputs]
40 assert len(frames) > 0, "Must have at least one input frame."
41 sizes = set()
42 for fr in frames:
43 sizes.add(fr.size)
Doug Zongker5120c9f2014-03-11 12:39:33 -070044
Tao Baob723f4f2015-12-11 15:18:51 -080045 assert len(sizes) == 1, "All input images must have the same size."
46 w, h = sizes.pop()
47 N = len(frames)
Doug Zongker5120c9f2014-03-11 12:39:33 -070048
Tao Baob723f4f2015-12-11 15:18:51 -080049 out = Image.new("RGB", (w, h*N))
50 for j in range(h):
51 for i in range(w):
52 for fn, f in enumerate(frames):
53 out.putpixel((i, j*N+fn), f.getpixel((i, j)))
Doug Zongker5120c9f2014-03-11 12:39:33 -070054
Tao Baob723f4f2015-12-11 15:18:51 -080055 # When loading this image, the graphics library expects to find a text
56 # chunk that specifies how many frames this animation represents. If
57 # you post-process the output of this script with some kind of
58 # optimizer tool (eg pngcrush or zopflipng) make sure that your
59 # optimizer preserves this text chunk.
Doug Zongker5120c9f2014-03-11 12:39:33 -070060
Tao Baob723f4f2015-12-11 15:18:51 -080061 meta = PngImagePlugin.PngInfo()
62 meta.add_text("Frames", str(N))
63 meta.add_text("FPS", str(fps))
64
65 out.save(output, pnginfo=meta)
66
67
68def main(argv):
69 parser = argparse.ArgumentParser()
70 parser.add_argument('--fps', default=20)
71 parser.add_argument('--output', '-o', required=True)
72 parser.add_argument('input', nargs='+')
73 args = parser.parse_args(argv)
74
75 interlace(args.output, args.fps, args.input)
76
77
78if __name__ == '__main__':
79 main(sys.argv[1:])
80