i.landsat.rgb.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: i.landsat.rgb
  5. #
  6. # AUTHOR(S): Markus Neteler. <neteler itc it>
  7. # Hamish Bowman, scripting enhancements
  8. # Converted to Python by Glynn Clements
  9. #
  10. # PURPOSE: create pretty LANDSAT RGBs: the trick is to remove outliers
  11. # using percentiles (area under the histogram curve)
  12. #
  13. # COPYRIGHT: (C) 2006,2008 by the GRASS Development Team
  14. #
  15. # This program is free software under the GNU General Public
  16. # License (>=v2). Read the file COPYING that comes with GRASS
  17. # for details.
  18. #
  19. # TODO: implement better brightness control
  20. #############################################################################
  21. #%module
  22. #% description: Performs auto-balancing of colors for LANDSAT images.
  23. #% keywords: raster
  24. #% keywords: imagery
  25. #% keywords: colors
  26. #%end
  27. #%option G_OPT_R_INPUT
  28. #% key: red
  29. #% description: Name of LANDSAT red channel
  30. #%end
  31. #%option G_OPT_R_INPUT
  32. #% key: green
  33. #% description: Name of LANDSAT green channel
  34. #%end
  35. #%option G_OPT_R_INPUT
  36. #% key: blue
  37. #% description: Name of LANDSAT blue channel
  38. #%end
  39. #%option
  40. #% key: strength
  41. #% type: double
  42. #% description: Cropping intensity (upper brightness level)
  43. #% options: 0-100
  44. #% answer : 98
  45. #% required: no
  46. #%end
  47. #%flag
  48. #% key: f
  49. #% description: Extend colors to full range of data on each channel
  50. #%end
  51. #%flag
  52. #% key: p
  53. #% description: Preserve relative colors, adjust brightness only
  54. #%end
  55. #%flag
  56. #% key: r
  57. #% description: Reset to standard color range
  58. #%end
  59. import sys
  60. import os
  61. import string
  62. import grass.script as grass
  63. def get_percentile(map, percentile):
  64. s = grass.read_command('r.univar', flags = 'ge', map = map, percentile = percentile)
  65. kv = grass.parse_key_val(s)
  66. return float(kv['percentile_%s' % percentile])
  67. def set_colors(map, v0, v1):
  68. rules = [
  69. "0% black\n",
  70. "%f black\n" % v0,
  71. "%f white\n" % v1,
  72. "100% white\n"
  73. ]
  74. rules = ''.join(rules)
  75. grass.write_command('r.colors', map = map, rules = '-', stdin = rules)
  76. def main():
  77. red = options['red']
  78. green = options['green']
  79. blue = options['blue']
  80. brightness = options['strength']
  81. full = flags['f']
  82. preserve = flags['p']
  83. reset = flags['r']
  84. # 90 or 98? MAX value controls brightness
  85. # think of percent (0-100), must be positive or 0
  86. # must be more than "2" ?
  87. if full:
  88. for i in [red, green, blue]:
  89. grass.run_command('r.colors', map = i, color = 'grey')
  90. sys.exit(0)
  91. if reset:
  92. for i in [red, green, blue]:
  93. grass.run_command('r.colors', map = i, color = 'grey255')
  94. sys.exit(0)
  95. if not preserve:
  96. for i in [red, green, blue]:
  97. grass.message(_("Processing <%s>...") % i)
  98. v0 = get_percentile(i, 2)
  99. v1 = get_percentile(i, brightness)
  100. grass.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  101. set_colors(i, v0, v1)
  102. else:
  103. all_max = 0
  104. all_min = 999999
  105. for i in [red, green, blue]:
  106. grass.message(_("Processing <%s>...") % i)
  107. v0 = get_percentile(i, 2)
  108. v1 = get_percentile(i, brightness)
  109. grass.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  110. all_min = min(all_min, v0)
  111. all_max = max(all_max, v1)
  112. grass.debug("all_min=%f all_max=%f" % (all_min, all_max))
  113. for i in [red, green, blue]:
  114. set_colors(i, v0, v1)
  115. # write cmd history:
  116. mapset = grass.gisenv()['MAPSET']
  117. for i in [red, green, blue]:
  118. if grass.find_file(i)['mapset'] == mapset:
  119. grass.raster_history(i)
  120. if __name__ == "__main__":
  121. options, flags = grass.parser()
  122. main()