i.colors.enhance.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: i.colors.enhance (former i.landsat.rgb)
  5. #
  6. # AUTHOR(S): Markus Neteler, original author
  7. # Hamish Bowman, scripting enhancements
  8. # Converted to Python by Glynn Clements
  9. #
  10. # PURPOSE: create pretty RGBs: the trick is to remove outliers
  11. # using percentiles (area under the histogram curve)
  12. #
  13. # COPYRIGHT: (C) 2006, 2008, 2012-2014 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 RGB images.
  23. #% keyword: imagery
  24. #% keyword: RGB
  25. #% keyword: satellite
  26. #% keyword: colors
  27. #%end
  28. #%option G_OPT_R_INPUT
  29. #% key: red
  30. #% description: Name of red channel
  31. #%end
  32. #%option G_OPT_R_INPUT
  33. #% key: green
  34. #% description: Name of green channel
  35. #%end
  36. #%option G_OPT_R_INPUT
  37. #% key: blue
  38. #% description: Name of blue channel
  39. #%end
  40. #%option
  41. #% key: strength
  42. #% type: double
  43. #% description: Cropping intensity (upper brightness level)
  44. #% options: 0-100
  45. #% answer : 98
  46. #% required: no
  47. #%end
  48. #%flag
  49. #% key: f
  50. #% description: Extend colors to full range of data on each channel
  51. #% guisection: Colors
  52. #%end
  53. #%flag
  54. #% key: p
  55. #% description: Preserve relative colors, adjust brightness only
  56. #% guisection: Colors
  57. #%end
  58. #%flag
  59. #% key: r
  60. #% description: Reset to standard color range
  61. #% guisection: Colors
  62. #%end
  63. #%flag
  64. #% key: s
  65. #% description: Process bands serially (default: run in parallel)
  66. #%end
  67. import sys
  68. import os
  69. import string
  70. import grass.script as grass
  71. try:
  72. # new for python 2.6, in 2.5 it may be easy_install'd.
  73. import multiprocessing as mp
  74. do_mp = True
  75. except:
  76. do_mp = False
  77. def get_percentile(map, percentiles):
  78. # todo: generalize for any list length
  79. val1 = percentiles[0]
  80. val2 = percentiles[1]
  81. values = '%s,%s' % (val1, val2)
  82. s = grass.read_command('r.quantile', input = map,
  83. percentiles = values, quiet = True)
  84. val_str1 = s.splitlines()[0].split(':')[2]
  85. val_str2 = s.splitlines()[1].split(':')[2]
  86. return (float(val_str1), float(val_str2))
  87. # wrapper to handle multiprocesses communications back to the parent
  88. def get_percentile_mp(map, percentiles, conn):
  89. # Process() doesn't like storing connection parts in
  90. # separate dictionaries, only wants to pass through tuples,
  91. # so instead of just sending the sending the pipe we have to
  92. # send both parts then keep the one we want. ??
  93. output_pipe, input_pipe = conn
  94. input_pipe.close()
  95. result = get_percentile(map, percentiles)
  96. grass.debug('child (%s) (%.1f, %.1f)' % (map, result[0], result[1]))
  97. output_pipe.send(result)
  98. output_pipe.close()
  99. def set_colors(map, v0, v1):
  100. rules = [
  101. "0% black\n",
  102. "%f black\n" % v0,
  103. "%f white\n" % v1,
  104. "100% white\n"
  105. ]
  106. rules = ''.join(rules)
  107. grass.write_command('r.colors', map = map, rules = '-', stdin = rules, quiet = True)
  108. def main():
  109. red = options['red']
  110. green = options['green']
  111. blue = options['blue']
  112. brightness = options['strength']
  113. full = flags['f']
  114. preserve = flags['p']
  115. reset = flags['r']
  116. global do_mp
  117. if flags['s']:
  118. do_mp = False
  119. # 90 or 98? MAX value controls brightness
  120. # think of percent (0-100), must be positive or 0
  121. # must be more than "2" ?
  122. if full:
  123. for i in [red, green, blue]:
  124. grass.run_command('r.colors', map = i, color = 'grey', quiet = True)
  125. sys.exit(0)
  126. if reset:
  127. for i in [red, green, blue]:
  128. grass.run_command('r.colors', map = i, color = 'grey255', quiet = True)
  129. sys.exit(0)
  130. if not preserve:
  131. if do_mp:
  132. grass.message(_("Processing..."))
  133. # set up jobs and launch them
  134. proc = {}
  135. conn = {}
  136. for i in [red, green, blue]:
  137. conn[i] = mp.Pipe()
  138. proc[i] = mp.Process(target = get_percentile_mp,
  139. args = (i, ['2', brightness],
  140. conn[i],))
  141. proc[i].start()
  142. grass.percent(1, 2, 1)
  143. # collect results and wait for jobs to finish
  144. for i in [red, green, blue]:
  145. output_pipe, input_pipe = conn[i]
  146. (v0, v1) = input_pipe.recv()
  147. grass.debug('parent (%s) (%.1f, %.1f)' % (i, v0, v1))
  148. input_pipe.close()
  149. proc[i].join()
  150. set_colors(i, v0, v1)
  151. grass.percent(1, 1, 1)
  152. else:
  153. for i in [red, green, blue]:
  154. grass.message(_("Processing..."))
  155. (v0, v1) = get_percentile(i, ['2', brightness])
  156. grass.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  157. set_colors(i, v0, v1)
  158. else:
  159. all_max = 0
  160. all_min = 999999
  161. if do_mp:
  162. grass.message(_("Processing..."))
  163. # set up jobs and launch jobs
  164. proc = {}
  165. conn = {}
  166. for i in [red, green, blue]:
  167. conn[i] = mp.Pipe()
  168. proc[i] = mp.Process(target = get_percentile_mp,
  169. args = (i, ['2', brightness],
  170. conn[i],))
  171. proc[i].start()
  172. grass.percent(1, 2, 1)
  173. # collect results and wait for jobs to finish
  174. for i in [red, green, blue]:
  175. output_pipe, input_pipe = conn[i]
  176. (v0, v1) = input_pipe.recv()
  177. grass.debug('parent (%s) (%.1f, %.1f)' % (i, v0, v1))
  178. input_pipe.close()
  179. proc[i].join()
  180. all_min = min(all_min, v0)
  181. all_max = max(all_max, v1)
  182. grass.percent(1, 1, 1)
  183. else:
  184. for i in [red, green, blue]:
  185. grass.message(_("Processing..."))
  186. (v0, v1) = get_percentile(i, ['2', brightness])
  187. grass.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  188. all_min = min(all_min, v0)
  189. all_max = max(all_max, v1)
  190. grass.debug("all_min=%f all_max=%f" % (all_min, all_max))
  191. for i in [red, green, blue]:
  192. set_colors(i, all_min, all_max)
  193. # write cmd history:
  194. mapset = grass.gisenv()['MAPSET']
  195. for i in [red, green, blue]:
  196. if grass.find_file(i)['mapset'] == mapset:
  197. grass.raster_history(i)
  198. if __name__ == "__main__":
  199. options, flags = grass.parser()
  200. main()