piprgb 732 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python
  2. # $URL: http://pypng.googlecode.com/svn/trunk/code/piprgb $
  3. # $Rev: 131 $
  4. # piprgb
  5. #
  6. # Convert input image to RGB or RGBA format. Output will be colour type
  7. # 2 or 6, and will not have a tRNS chunk.
  8. import png
  9. def rgb(out, inp):
  10. """Convert to RGB/RGBA."""
  11. r = png.Reader(file=inp)
  12. r.preamble()
  13. if r.alpha or r.trns:
  14. get = r.asRGBA
  15. else:
  16. get = r.asRGB
  17. pixels,info = get()[2:4]
  18. w = png.Writer(**info)
  19. w.write(out, pixels)
  20. def main(argv=None):
  21. import sys
  22. if argv is None:
  23. argv = sys.argv
  24. if len(argv) > 1:
  25. f = open(argv[1], 'rb')
  26. else:
  27. f = sys.stdin
  28. return rgb(sys.stdout, f)
  29. if __name__ == '__main__':
  30. main()