gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

72 lines
1.9KB

  1. #!C:\odoobuild\WinPy64\python-3.12.3.amd64\python.exe
  2. # http://www.python.org/doc/2.4.4/lib/module-itertools.html
  3. import itertools
  4. import sys
  5. import png
  6. Description = """Join PNG images in a row left-to-right."""
  7. class FormatError(Exception):
  8. """
  9. Some problem with the image format.
  10. """
  11. def join_row(out, l):
  12. """
  13. Concatenate the list of images.
  14. All input images must be same height and
  15. have the same number of channels.
  16. They are concatenated left-to-right.
  17. `out` is the (open file) destination for the output image.
  18. `l` should be a list of open files (the input image files).
  19. """
  20. l = [png.Reader(file=f) for f in l]
  21. # Ewgh, side effects.
  22. for r in l:
  23. r.preamble()
  24. # The reference height; from the first image.
  25. height = l[0].height
  26. # The total target width
  27. width = 0
  28. for i,r in enumerate(l):
  29. if r.height != height:
  30. raise FormatError('Image %d, height %d, does not match %d.' %
  31. (i, r.height, height))
  32. width += r.width
  33. # Various bugs here because different numbers of channels and depths go wrong.
  34. pixel, info = zip(*[r.asDirect()[2:4] for r in l])
  35. tinfo = dict(info[0])
  36. del tinfo['size']
  37. w = png.Writer(width, height, **tinfo)
  38. def iter_all_rows():
  39. for row in zip(*pixel):
  40. # `row` is a sequence that has one row from each input image.
  41. # list() is required here to hasten the lazy row building;
  42. # not sure if that's a bug in PyPNG or not.
  43. yield list(itertools.chain(*row))
  44. w.write(out, iter_all_rows())
  45. def main(argv):
  46. import argparse
  47. parser = argparse.ArgumentParser(description=Description)
  48. parser.add_argument(
  49. "input", nargs="*", default="-", type=png.cli_open, metavar="PNG"
  50. )
  51. args = parser.parse_args()
  52. return join_row(png.binary_stdout(), args.input)
  53. if __name__ == '__main__':
  54. main(sys.argv)
上海开阖软件有限公司 沪ICP备12045867号-1