Doug Zongker | 3ceeb58 | 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 | |
| 15 | """Script to take a set of frames (PNG files) for a recovery animation |
| 16 | and turn it into a single output image which contains the input frames |
| 17 | interlaced by row. Run with the names of all the input frames on the |
| 18 | command line, in order, followed by the name of the output file.""" |
| 19 | |
| 20 | import sys |
| 21 | try: |
| 22 | import Image |
| 23 | import PngImagePlugin |
| 24 | except ImportError: |
| 25 | print "This script requires the Python Imaging Library to be installed." |
| 26 | sys.exit(1) |
| 27 | |
| 28 | frames = [Image.open(fn).convert("RGB") for fn in sys.argv[1:-1]] |
| 29 | assert len(frames) > 0, "Must have at least one input frame." |
| 30 | sizes = set() |
| 31 | for fr in frames: |
| 32 | sizes.add(fr.size) |
| 33 | |
| 34 | assert len(sizes) == 1, "All input images must have the same size." |
| 35 | w, h = sizes.pop() |
| 36 | N = len(frames) |
| 37 | |
| 38 | out = Image.new("RGB", (w, h*N)) |
| 39 | for j in range(h): |
| 40 | for i in range(w): |
| 41 | for fn, f in enumerate(frames): |
| 42 | out.putpixel((i, j*N+fn), f.getpixel((i, j))) |
| 43 | |
| 44 | # When loading this image, the graphics library expects to find a text |
| 45 | # chunk that specifies how many frames this animation represents. If |
| 46 | # you post-process the output of this script with some kind of |
| 47 | # optimizer tool (eg pngcrush or zopflipng) make sure that your |
| 48 | # optimizer preserves this text chunk. |
| 49 | |
| 50 | meta = PngImagePlugin.PngInfo() |
| 51 | meta.add_text("Frames", str(N)) |
| 52 | |
| 53 | out.save(sys.argv[-1], pnginfo=meta) |