r.rgb.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. def main():
  43. options, unused = gscript.parser()
  44. input = options['input']
  45. red = options['red']
  46. green = options['green']
  47. blue = options['blue']
  48. if not gscript.find_file(input)['file']:
  49. gscript.fatal(_("Raster map <%s> not found") % input)
  50. expressions = []
  51. maps = []
  52. if red:
  53. expressions.append('%s = r#${input}' % red)
  54. maps.append(red)
  55. if green:
  56. expressions.append('%s = g#${input}' % green)
  57. maps.append(green)
  58. if blue:
  59. expressions.append('%s = b#${input}' % blue)
  60. maps.append(blue)
  61. expr = ';'.join(expressions)
  62. gscript.mapcalc(expr, input=input)
  63. for name in maps:
  64. gscript.run_command('r.colors', map=name, color='grey255')
  65. gscript.raster_history(name)
  66. if __name__ == "__main__":
  67. main()