Doug Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 1 | # 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 Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 15 | """ |
| 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). |
Doug Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 21 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 22 | e.g. |
| 23 | interlace-frames.py --fps 20 --output output.png frame0.png frame1.png frame3.png |
| 24 | """ |
| 25 | |
| 26 | from __future__ import print_function |
| 27 | |
| 28 | import argparse |
Doug Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 29 | import sys |
| 30 | try: |
| 31 | import Image |
| 32 | import PngImagePlugin |
| 33 | except ImportError: |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 34 | print("This script requires the Python Imaging Library to be installed.") |
Doug Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 35 | sys.exit(1) |
| 36 | |
Doug Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 37 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 38 | def 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 Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 44 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 45 | assert len(sizes) == 1, "All input images must have the same size." |
| 46 | w, h = sizes.pop() |
| 47 | N = len(frames) |
Doug Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 48 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 49 | 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 Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 54 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 55 | # 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 Zongker | 5120c9f | 2014-03-11 12:39:33 -0700 | [diff] [blame] | 60 | |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 61 | 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 | |
| 68 | def 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 | |
| 78 | if __name__ == '__main__': |
| 79 | main(sys.argv[1:]) |
| 80 | |