i.landsat.rgb.py 3.5 KB

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