pipcat 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # $URL: http://pypng.googlecode.com/svn/trunk/code/pipcat $
  3. # $Rev: 77 $
  4. # http://www.python.org/doc/2.4.4/lib/module-itertools.html
  5. import itertools
  6. import sys
  7. import png
  8. def cat(out, l):
  9. """Concatenate the list of images. All input images must be same
  10. height and have the same number of channels. They are concatenated
  11. left-to-right. `out` is the (open file) destination for the
  12. output image. `l` should be a list of open files (the input
  13. image files).
  14. """
  15. l = map(lambda f: png.Reader(file=f), l)
  16. # Ewgh, side effects.
  17. map(lambda r: r.preamble(), l)
  18. # The reference height; from the first image.
  19. height = l[0].height
  20. # The total target width
  21. width = 0
  22. for i,r in enumerate(l):
  23. if r.height != height:
  24. raise Error('Image %d, height %d, does not match %d.' %
  25. (i, r.height, height))
  26. width += r.width
  27. pixel,info = zip(*map(lambda r: r.asDirect()[2:4], l))
  28. tinfo = dict(info[0])
  29. del tinfo['size']
  30. w = png.Writer(width, height, **tinfo)
  31. def itercat():
  32. for row in itertools.izip(*pixel):
  33. yield itertools.chain(*row)
  34. w.write(out, itercat())
  35. def main(argv):
  36. return cat(sys.stdout, map(lambda n: open(n, 'rb'), argv[1:]))
  37. if __name__ == '__main__':
  38. main(sys.argv)