r.blend.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 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
  20. #% key: first
  21. #% type: string
  22. #% gisprompt: old,cell,raster
  23. #% description: Name of first raster map for blending
  24. #% key_desc : name
  25. #% required : yes
  26. #%end
  27. #%option
  28. #% key: second
  29. #% type: string
  30. #% gisprompt: old,cell,raster
  31. #% description: Name of second raster map for blending
  32. #% key_desc : name
  33. #% required : yes
  34. #%end
  35. #%option
  36. #% key: output
  37. #% type: string
  38. #% description: Base name for red, green and blue output maps containing the blend
  39. #% key_desc : name
  40. #% required : yes
  41. #%end
  42. #%option
  43. #% key: percent
  44. #% type: integer
  45. #% answer: 50
  46. #% options : 1-99
  47. #% description: Percentage weight of first map for color blending
  48. #% required : no
  49. #%end
  50. import sys
  51. import os
  52. import string
  53. from grass.script import core, raster as grass
  54. def main():
  55. first = options['first']
  56. second = options['second']
  57. output = options['output']
  58. percent = options['percent']
  59. mapset = grass.gisenv()['MAPSET']
  60. if not grass.overwrite():
  61. for ch in ['r','g','b']:
  62. map = '%s.%s' % (output, ch)
  63. if grass.find_file(map, element = 'cell', mapset = mapset)['file']:
  64. grass.fatal("Raster map <%s> already exists." % map)
  65. percent = int(percent)
  66. perc_inv = 100 - percent
  67. frac1 = percent / 100.0
  68. frac2 = perc_inv / 100.0
  69. grass.message("Calculating the three component maps...")
  70. template = string.Template("$$output.$ch = $$frac1 * $ch#$$first + $$frac2 * $ch#$$second")
  71. cmd = [template.substitute(ch = ch) for ch in ['r','g','b']]
  72. cmd = ';'.join(cmd)
  73. grass.mapcalc(cmd,
  74. output = output,
  75. first = first, second = second,
  76. frac1 = frac1, frac2 = frac2)
  77. for ch in ['r','g','b']:
  78. map = "%s.%s" % (output, ch)
  79. grass.run_command('r.colors', map = map, color = 'grey255')
  80. grass.run_command('r.support', map = map,
  81. title = "Color blend of %s and %s" % (first, second), history="")
  82. grass.run_command('r.support', map = map,
  83. history = "r.blend %s channel." % ch)
  84. grass.run_command('r.support', map = map,
  85. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  86. grass.run_command('r.support', map = map, history = os.environ['CMDLINE'])
  87. grass.message("Done. Use the following command to visualize the result:")
  88. grass.message("d.rgb r=%s.r g=%s.g b=%s.b" % (output, output, output))
  89. if __name__ == "__main__":
  90. options, flags = grass.parser()
  91. main()