i.colors.enhance.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 grass.script as gscript
  69. # i18N
  70. import os
  71. import gettext
  72. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  73. try:
  74. # new for python 2.6, in 2.5 it may be easy_install'd.
  75. import multiprocessing as mp
  76. do_mp = True
  77. except:
  78. do_mp = False
  79. def get_percentile(map, percentiles):
  80. # todo: generalize for any list length
  81. val1 = percentiles[0]
  82. val2 = percentiles[1]
  83. values = '%s,%s' % (val1, val2)
  84. s = gscript.read_command('r.quantile', input=map,
  85. percentiles=values, quiet=True)
  86. val_str1 = s.splitlines()[0].split(':')[2]
  87. val_str2 = s.splitlines()[1].split(':')[2]
  88. return (float(val_str1), float(val_str2))
  89. # wrapper to handle multiprocesses communications back to the parent
  90. def get_percentile_mp(map, percentiles, conn):
  91. # Process() doesn't like storing connection parts in
  92. # separate dictionaries, only wants to pass through tuples,
  93. # so instead of just sending the sending the pipe we have to
  94. # send both parts then keep the one we want. ??
  95. output_pipe, input_pipe = conn
  96. input_pipe.close()
  97. result = get_percentile(map, percentiles)
  98. gscript.debug('child (%s) (%.1f, %.1f)' % (map, result[0], result[1]))
  99. output_pipe.send(result)
  100. output_pipe.close()
  101. def set_colors(map, v0, v1):
  102. rules = ''.join(["0% black\n", "%f black\n" % v0,
  103. "%f white\n" % v1, "100% white\n"])
  104. gscript.write_command('r.colors', map=map, rules='-', stdin=rules,
  105. quiet=True)
  106. def main():
  107. red = options['red']
  108. green = options['green']
  109. blue = options['blue']
  110. brightness = options['strength']
  111. full = flags['f']
  112. preserve = flags['p']
  113. reset = flags['r']
  114. global do_mp
  115. if flags['s']:
  116. do_mp = False
  117. # 90 or 98? MAX value controls brightness
  118. # think of percent (0-100), must be positive or 0
  119. # must be more than "2" ?
  120. if full:
  121. for i in [red, green, blue]:
  122. gscript.run_command('r.colors', map=i, color='grey', quiet=True)
  123. sys.exit(0)
  124. if reset:
  125. for i in [red, green, blue]:
  126. gscript.run_command('r.colors', map=i, color='grey255', quiet=True)
  127. sys.exit(0)
  128. if not preserve:
  129. if do_mp:
  130. gscript.message(_("Processing..."))
  131. # set up jobs and launch them
  132. proc = {}
  133. conn = {}
  134. for i in [red, green, blue]:
  135. conn[i] = mp.Pipe()
  136. proc[i] = mp.Process(target=get_percentile_mp,
  137. args=(i, ['2', brightness],
  138. conn[i],))
  139. proc[i].start()
  140. gscript.percent(1, 2, 1)
  141. # collect results and wait for jobs to finish
  142. for i in [red, green, blue]:
  143. output_pipe, input_pipe = conn[i]
  144. (v0, v1) = input_pipe.recv()
  145. gscript.debug('parent (%s) (%.1f, %.1f)' % (i, v0, v1))
  146. input_pipe.close()
  147. proc[i].join()
  148. set_colors(i, v0, v1)
  149. gscript.percent(1, 1, 1)
  150. else:
  151. for i in [red, green, blue]:
  152. gscript.message(_("Processing..."))
  153. (v0, v1) = get_percentile(i, ['2', brightness])
  154. gscript.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  155. set_colors(i, v0, v1)
  156. else:
  157. all_max = 0
  158. all_min = 999999
  159. if do_mp:
  160. gscript.message(_("Processing..."))
  161. # set up jobs and launch jobs
  162. proc = {}
  163. conn = {}
  164. for i in [red, green, blue]:
  165. conn[i] = mp.Pipe()
  166. proc[i] = mp.Process(target=get_percentile_mp,
  167. args=(i, ['2', brightness],
  168. conn[i],))
  169. proc[i].start()
  170. gscript.percent(1, 2, 1)
  171. # collect results and wait for jobs to finish
  172. for i in [red, green, blue]:
  173. output_pipe, input_pipe = conn[i]
  174. (v0, v1) = input_pipe.recv()
  175. gscript.debug('parent (%s) (%.1f, %.1f)' % (i, v0, v1))
  176. input_pipe.close()
  177. proc[i].join()
  178. all_min = min(all_min, v0)
  179. all_max = max(all_max, v1)
  180. gscript.percent(1, 1, 1)
  181. else:
  182. for i in [red, green, blue]:
  183. gscript.message(_("Processing..."))
  184. (v0, v1) = get_percentile(i, ['2', brightness])
  185. gscript.debug("<%s>: min=%f max=%f" % (i, v0, v1))
  186. all_min = min(all_min, v0)
  187. all_max = max(all_max, v1)
  188. gscript.debug("all_min=%f all_max=%f" % (all_min, all_max))
  189. for i in [red, green, blue]:
  190. set_colors(i, all_min, all_max)
  191. # write cmd history:
  192. mapset = gscript.gisenv()['MAPSET']
  193. for i in [red, green, blue]:
  194. if gscript.find_file(i)['mapset'] == mapset:
  195. gscript.raster_history(i)
  196. if __name__ == "__main__":
  197. options, flags = gscript.parser()
  198. main()