No Description

interlace-frames.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python
  2. # Copyright (C) 2014 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Script to take a set of frames (PNG files) for a recovery animation
  17. and turn it into a single output image which contains the input frames
  18. interlaced by row. Run with the names of all the input frames on the
  19. command line, in order, followed by the name of the output file.
  20. """
  21. from __future__ import print_function
  22. import argparse
  23. import os.path
  24. import sys
  25. try:
  26. import Image
  27. import PngImagePlugin
  28. except ImportError:
  29. print("This script requires the Python Imaging Library to be installed.")
  30. sys.exit(1)
  31. def interlace(output, inputs):
  32. frames = [Image.open(fn).convert("RGB") for fn in inputs]
  33. assert len(frames) > 0, "Must have at least one input frame."
  34. sizes = set()
  35. for fr in frames:
  36. sizes.add(fr.size)
  37. assert len(sizes) == 1, "All input images must have the same size."
  38. w, h = sizes.pop()
  39. N = len(frames)
  40. out = Image.new("RGB", (w, h*N))
  41. for j in range(h):
  42. for i in range(w):
  43. for fn, f in enumerate(frames):
  44. out.putpixel((i, j*N+fn), f.getpixel((i, j)))
  45. # When loading this image, the graphics library expects to find a text
  46. # chunk that specifies how many frames this animation represents. If
  47. # you post-process the output of this script with some kind of
  48. # optimizer tool (eg pngcrush or zopflipng) make sure that your
  49. # optimizer preserves this text chunk.
  50. meta = PngImagePlugin.PngInfo()
  51. meta.add_text("Frames", str(N))
  52. out.save(output, pnginfo=meta)
  53. def deinterlace(output, input):
  54. # Truncate the output filename extension if it's '.png'.
  55. if os.path.splitext(output)[1].lower() == '.png':
  56. output = output[:-4]
  57. img2 = Image.open(input)
  58. print(img2.mode)
  59. palette = img2.getpalette()
  60. img = img2.convert("RGB")
  61. num_frames = int(img.info.get('Frames', 1))
  62. print('Found %d frames in %s.' % (num_frames, input))
  63. assert num_frames > 0, 'Invalid Frames meta.'
  64. # palette = img.getpalette()
  65. print(palette)
  66. width, height = img.size
  67. height /= num_frames
  68. for k in range(num_frames):
  69. out = Image.new('RGB', (width, height))
  70. out.info = img.info
  71. for i in range(width):
  72. for j in range(height):
  73. out.putpixel((i, j), img.getpixel((i, j * num_frames + k)))
  74. # out.putpalette(img.getpalette(), rawmode='RGB')
  75. out2 = out.convert(mode='P', palette=palette)
  76. #out2 = out
  77. print(out2.mode)
  78. # out2.putpalette(palette)
  79. filename = '%s%02d.png' % (output, k)
  80. out2.save(filename)
  81. print('Frame %d written to %s.' % (k, filename))
  82. def main(argv):
  83. parser = argparse.ArgumentParser(description='Parse')
  84. parser.add_argument('--deinterlace', '-d', action='store_true')
  85. parser.add_argument('--output', '-o', required=True)
  86. parser.add_argument('input', nargs='+')
  87. args = parser.parse_args(argv)
  88. if args.deinterlace:
  89. # args.input is a list, and we only process the first when deinterlacing.
  90. deinterlace(args.output, args.input[0])
  91. else:
  92. interlace(args.output, args.input)
  93. if __name__ == '__main__':
  94. main(sys.argv[1:])