png.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/opt/local/bin/python
  2. import zlib
  3. import sys
  4. import argparse
  5. def decompress():
  6. data = sys.stdin.buffer.read()
  7. decom = zlib.decompress(data)
  8. sys.stdout.buffer.write(decom)
  9. def compress():
  10. data = sys.stdin.buffer.read()
  11. decom = zlib.compress(data)
  12. sys.stdout.buffer.write(decom)
  13. def crc():
  14. data = sys.stdin.buffer.read()
  15. ret = zlib.crc32(data)
  16. sys.stdout.write(f'{ret}')
  17. def main():
  18. parser = argparse.ArgumentParser(
  19. prog='png-helper',
  20. description='Tools for working with png files in ngn/k')
  21. parser.add_argument('-d', '--decompress', action='store_true')
  22. parser.add_argument('-c', '--compress', action='store_true')
  23. parser.add_argument('-r', '--crc', action='store_true')
  24. args = parser.parse_args()
  25. if not sum([args.decompress, args.compress, args.crc]) == 1:
  26. print("Must supply exactly one of --decompress, --compress or --crc")
  27. parser.print_help()
  28. return
  29. if args.decompress:
  30. decompress()
  31. elif args.compress:
  32. compress()
  33. else:
  34. crc()
  35. if __name__ == '__main__':
  36. main()