r.rgb.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #% keyword: separate
  19. #% keyword: split
  20. #%end
  21. #%option G_OPT_R_INPUT
  22. #%end
  23. #%option G_OPT_R_OUTPUT
  24. #% key: red
  25. #% description: Red channel raster map name
  26. #% required: no
  27. #%end
  28. #%option G_OPT_R_OUTPUT
  29. #% key: green
  30. #% description: Green channel raster map name
  31. #% required: no
  32. #%end
  33. #%option G_OPT_R_OUTPUT
  34. #% key: blue
  35. #% description: Blue channel raster map name
  36. #% required: no
  37. #%end
  38. #%rules
  39. #% required: red, green, blue
  40. #%end
  41. import grass.script as gscript
  42. # i18N
  43. import os
  44. import gettext
  45. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  46. def main():
  47. options, unused = gscript.parser()
  48. input = options['input']
  49. red = options['red']
  50. green = options['green']
  51. blue = options['blue']
  52. if not gscript.find_file(input)['file']:
  53. gscript.fatal(_("Raster map <%s> not found") % input)
  54. expressions = []
  55. maps = []
  56. if red:
  57. expressions.append('%s = r#${input}' % red)
  58. maps.append(red)
  59. if green:
  60. expressions.append('%s = g#${input}' % green)
  61. maps.append(green)
  62. if blue:
  63. expressions.append('%s = b#${input}' % blue)
  64. maps.append(blue)
  65. expr = ';'.join(expressions)
  66. gscript.mapcalc(expr, input=input)
  67. for name in maps:
  68. gscript.run_command('r.colors', map=name, color='grey255')
  69. gscript.raster_history(name)
  70. if __name__ == "__main__":
  71. main()