gooderp18绿色标准版
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

102 lines
2.7KB

  1. #!C:\odoobuild\WinPy64\python-3.12.3.amd64\python.exe
  2. import struct
  3. import png
  4. def write_pnm(file, plain, rows, meta):
  5. """
  6. Write a Netpbm PNM (or PAM) file.
  7. *file* output file object;
  8. *plain* (a bool) true if writing plain format (not possible for PAM);
  9. *rows* an iterator for the rows;
  10. *meta* the info dictionary.
  11. """
  12. meta = dict(meta)
  13. meta["maxval"] = 2 ** meta["bitdepth"] - 1
  14. meta["width"], meta["height"] = meta["size"]
  15. # Number of planes determines both image formats:
  16. # 1 : L to PGM
  17. # 2 : LA to PAM
  18. # 3 : RGB to PPM
  19. # 4 : RGBA to PAM
  20. planes = meta["planes"]
  21. # Assume inputs are from a PNG file.
  22. assert planes in (1, 2, 3, 4)
  23. if planes in (1, 3):
  24. if 1 == planes:
  25. # PGM
  26. # Even if maxval is 1 we use PGM instead of PBM,
  27. # to avoid converting data.
  28. magic = "P5"
  29. if plain:
  30. magic = "P2"
  31. else:
  32. # PPM
  33. magic = "P6"
  34. if plain:
  35. magic = "P3"
  36. header = "{magic} {width:d} {height:d} {maxval:d}\n".format(magic=magic, **meta)
  37. if planes in (2, 4):
  38. # PAM
  39. # See http://netpbm.sourceforge.net/doc/pam.html
  40. if plain:
  41. raise Exception("PAM (%d-plane) does not support plain format" % planes)
  42. if 2 == planes:
  43. tupltype = "GRAYSCALE_ALPHA"
  44. else:
  45. tupltype = "RGB_ALPHA"
  46. header = (
  47. "P7\nWIDTH {width:d}\nHEIGHT {height:d}\n"
  48. "DEPTH {planes:d}\nMAXVAL {maxval:d}\n"
  49. "TUPLTYPE {tupltype}\nENDHDR\n".format(tupltype=tupltype, **meta)
  50. )
  51. file.write(header.encode("ascii"))
  52. # Values per row
  53. vpr = planes * meta["width"]
  54. if plain:
  55. for row in rows:
  56. row_b = b" ".join([b"%d" % v for v in row])
  57. file.write(row_b)
  58. file.write(b"\n")
  59. else:
  60. # format for struct.pack
  61. fmt = ">%d" % vpr
  62. if meta["maxval"] > 0xFF:
  63. fmt = fmt + "H"
  64. else:
  65. fmt = fmt + "B"
  66. for row in rows:
  67. file.write(struct.pack(fmt, *row))
  68. file.flush()
  69. def main(argv=None):
  70. import argparse
  71. parser = argparse.ArgumentParser(description="Convert PNG to PAM")
  72. parser.add_argument("--plain", action="store_true")
  73. parser.add_argument(
  74. "input", nargs="?", default="-", type=png.cli_open, metavar="PNG"
  75. )
  76. args = parser.parse_args()
  77. # Encode PNG to PNM (or PAM)
  78. image = png.Reader(file=args.input)
  79. _, _, rows, info = image.asDirect()
  80. write_pnm(png.binary_stdout(), args.plain, rows, info)
  81. if __name__ == "__main__":
  82. import sys
  83. sys.exit(main())
上海开阖软件有限公司 沪ICP备12045867号-1