r.rgb.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.rgb
  5. # AUTHOR(S): Glynn Clements
  6. # PURPOSE: Split a raster map into red, green and blue maps
  7. # COPYRIGHT: (C) 2009 Glynn Clements and the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #############################################################################
  14. #%module
  15. #% description: Splits a raster map into red, green and blue maps.
  16. #% keyword: raster
  17. #% keyword: RGB
  18. #%end
  19. #%option G_OPT_R_INPUT
  20. #%end
  21. #%option G_OPT_R_OUTPUT
  22. #% key: red
  23. #% description: Red channel raster map name
  24. #% required: no
  25. #%end
  26. #%option G_OPT_R_OUTPUT
  27. #% key: green
  28. #% description: Green channel raster map name
  29. #% required: no
  30. #%end
  31. #%option G_OPT_R_OUTPUT
  32. #% key: blue
  33. #% description: Blue channel raster map name
  34. #% required: no
  35. #%end
  36. import grass.script as gscript
  37. def main():
  38. options, unused = gscript.parser()
  39. input = options['input']
  40. red = options['red']
  41. green = options['green']
  42. blue = options['blue']
  43. if not gscript.find_file(input)['file']:
  44. gscript.fatal(_("Raster map <%s> not found") % input)
  45. expressions = []
  46. maps = []
  47. if red:
  48. expressions.append('%s = r#${input}' % red)
  49. maps.append(red)
  50. if green:
  51. expressions.append('%s = g#${input}' % green)
  52. maps.append(green)
  53. if blue:
  54. expressions.append('%s = b#${input}' % blue)
  55. maps.append(blue)
  56. expr = ';'.join(expressions)
  57. gscript.mapcalc(expr, input=input)
  58. for name in maps:
  59. gscript.run_command('r.colors', map=name, color='grey255')
  60. gscript.raster_history(name)
  61. if __name__ == "__main__":
  62. main()