r.blend.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.blend for GRASS 5.7; based on blend.sh for GRASS 5
  5. # AUTHOR(S): CERL?; updated to GRASS 5.7 by Michael Barton
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: To redraw current displayed maps to 24 bit PNG output
  8. # COPYRIGHT: (C) 2004-2012 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Blends color components of two raster maps by a given ratio.
  17. #% keywords: raster
  18. #%end
  19. #%option G_OPT_R_INPUT
  20. #% key: first
  21. #% description: Name of first raster map for blending
  22. #%end
  23. #%option G_OPT_R_INPUT
  24. #% key: second
  25. #% description: Name of second raster map for blending
  26. #%end
  27. #%option
  28. #% key: output_prefix
  29. #% type: string
  30. #% description: Prefix for red, green and blue output raster maps containing
  31. #% key_desc : name
  32. #% required : yes
  33. #%end
  34. #%option
  35. #% key: percent
  36. #% type: double
  37. #% answer: 50
  38. #% options : 0-100
  39. #% description: Percentage weight of first map for color blending
  40. #% required : no
  41. #%end
  42. #% flag
  43. #% key: c
  44. #% description: Combine resulting R,G,B layers into single output map
  45. #%end
  46. import sys
  47. import os
  48. import string
  49. import grass.script as grass
  50. def main():
  51. first = options['first']
  52. second = options['second']
  53. output = options['output_prefix']
  54. percent = options['percent']
  55. mapset = grass.gisenv()['MAPSET']
  56. if not grass.overwrite():
  57. for ch in ['r','g','b']:
  58. map = '%s.%s' % (output, ch)
  59. if grass.find_file(map, element = 'cell', mapset = mapset)['file']:
  60. grass.fatal(_("Raster map <%s> already exists.") % map)
  61. percent = float(percent)
  62. perc_inv = 100.0 - percent
  63. frac1 = percent / 100.0
  64. frac2 = perc_inv / 100.0
  65. grass.message(_("Calculating the three component maps..."))
  66. template = string.Template("$$output.$ch = $$frac1 * $ch#$$first + $$frac2 * $ch#$$second")
  67. cmd = [template.substitute(ch = ch) for ch in ['r','g','b']]
  68. cmd = ';'.join(cmd)
  69. grass.mapcalc(cmd,
  70. output = output,
  71. first = first, second = second,
  72. frac1 = frac1, frac2 = frac2)
  73. for ch in ['r','g','b']:
  74. map = "%s.%s" % (output, ch)
  75. grass.run_command('r.colors', map = map, color = 'grey255')
  76. grass.run_command('r.support', map = map, history="",
  77. title = "Color blend of %s and %s" % (first, second),
  78. description = "generated by r.blend")
  79. grass.run_command('r.support', map = map,
  80. history = "r.blend %s channel." % ch)
  81. grass.run_command('r.support', map = map,
  82. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  83. grass.run_command('r.support', map = map, history = "")
  84. grass.run_command('r.support', map = map, history = os.environ['CMDLINE'])
  85. if flags['c']:
  86. grass.run_command('r.composite', r = '%s.r' % output,
  87. g = '%s.g' % output, b = '%s.b' % output, output = output)
  88. grass.run_command('r.support', map = output, history="",
  89. title = "Color blend of %s and %s" % (first, second),
  90. description = "generated by r.blend")
  91. grass.run_command('r.support', map = output,
  92. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  93. grass.run_command('r.support', map = output, history = "")
  94. grass.run_command('r.support', map = output, history = os.environ['CMDLINE'])
  95. else:
  96. grass.message(_("Done. Use the following command to visualize the result:"))
  97. grass.message(_("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
  98. if __name__ == "__main__":
  99. options, flags = grass.parser()
  100. main()