i.landsat.rgb.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  28. #% key: red
  29. #% type: string
  30. #% gisprompt: old,cell,raster
  31. #% description: LANDSAT red channel
  32. #% required : yes
  33. #%end
  34. #%option
  35. #% key: green
  36. #% type: string
  37. #% gisprompt: old,cell,raster
  38. #% description: LANDSAT green channel
  39. #% required : yes
  40. #%end
  41. #%option
  42. #% key: blue
  43. #% type: string
  44. #% gisprompt: old,cell,raster
  45. #% description: LANDSAT blue channel
  46. #% required : yes
  47. #%end
  48. #%option
  49. #% key: strength
  50. #% type: integer
  51. #% description: Cropping intensity (upper brightness level)
  52. #% options: 0-100
  53. #% answer : 98
  54. #% required : no
  55. #%end
  56. #%flag
  57. #% key: f
  58. #% description: Extend colors to full range of data on each channel
  59. #%end
  60. #%flag
  61. #% key: p
  62. #% description: Preserve relative colors, adjust brightness only
  63. #%end
  64. #%flag
  65. #% key: r
  66. #% description: Reset to standard color range
  67. #%end
  68. import sys
  69. import os
  70. import string
  71. import grass.script as grass
  72. def get_percentile(map, percentile):
  73. s = grass.read_command('r.univar', flags = 'ge', map = map, percentile = percentile)
  74. kv = grass.parse_key_val(s)
  75. return float(kv['percentile_%s' % percentile])
  76. def set_colors(map, v0, v1):
  77. rules = [
  78. "0% black\n",
  79. "%f black\n" % v0,
  80. "%f white\n" % v1,
  81. "100% white\n"
  82. ]
  83. rules = ''.join(rules)
  84. grass.write_command('r.colors', map = map, rules = '-', stdin = rules)
  85. def main():
  86. red = options['red']
  87. green = options['green']
  88. blue = options['blue']
  89. brightness = options['strength']
  90. full = flags['f']
  91. preserve = flags['p']
  92. reset = flags['r']
  93. # 90 or 98? MAX value controls brightness
  94. # think of percent (0-100), must be positive or 0
  95. # must be more than "2" ?
  96. if full:
  97. for i in [red, green, blue]:
  98. grass.run_command('r.colors', map = i, color = 'grey')
  99. sys.exit(0)
  100. if reset:
  101. for i in [red, green, blue]:
  102. grass.run_command('r.colors', map = i, color = 'grey255')
  103. sys.exit(0)
  104. if not preserve:
  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. set_colors(i, v0, v1)
  111. else:
  112. all_max = 0
  113. all_min = 999999
  114. for i in [red, green, blue]:
  115. grass.message(_("Processing <%s>...") % i)
  116. v0 = get_percentile(i, 2)
  117. v1 = get_percentile(i, brightness)
  118. grass.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  119. all_min = min(all_min, v0)
  120. all_max = max(all_max, v1)
  121. grass.debug("all_min=%f all_max=%f" % (all_min, all_max))
  122. for i in [red, green, blue]:
  123. set_colors(i, v0, v1)
  124. # write cmd history:
  125. for i in [red, green, blue]:
  126. grass.raster_history(i)
  127. if __name__ == "__main__":
  128. options, flags = grass.parser()
  129. main()