r.blend.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 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: integer
  37. #% answer: 50
  38. #% options : 1-99
  39. #% description: Percentage weight of first map for color blending
  40. #% required : no
  41. #%end
  42. import sys
  43. import os
  44. import string
  45. import grass.script as grass
  46. def main():
  47. first = options['first']
  48. second = options['second']
  49. output = options['output']
  50. percent = options['percent']
  51. mapset = grass.gisenv()['MAPSET']
  52. if not grass.overwrite():
  53. for ch in ['r','g','b']:
  54. map = '%s.%s' % (output, ch)
  55. if grass.find_file(map, element = 'cell', mapset = mapset)['file']:
  56. grass.fatal(_("Raster map <%s> already exists.") % map)
  57. percent = int(percent)
  58. perc_inv = 100 - percent
  59. frac1 = percent / 100.0
  60. frac2 = perc_inv / 100.0
  61. grass.message(_("Calculating the three component maps..."))
  62. template = string.Template("$$output.$ch = $$frac1 * $ch#$$first + $$frac2 * $ch#$$second")
  63. cmd = [template.substitute(ch = ch) for ch in ['r','g','b']]
  64. cmd = ';'.join(cmd)
  65. grass.mapcalc(cmd,
  66. output = output,
  67. first = first, second = second,
  68. frac1 = frac1, frac2 = frac2)
  69. for ch in ['r','g','b']:
  70. map = "%s.%s" % (output, ch)
  71. grass.run_command('r.colors', map = map, color = 'grey255')
  72. grass.run_command('r.support', map = map,
  73. title = "Color blend of %s and %s" % (first, second), history="")
  74. grass.run_command('r.support', map = map,
  75. history = "r.blend %s channel." % ch)
  76. grass.run_command('r.support', map = map,
  77. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  78. grass.run_command('r.support', map = map, history = os.environ['CMDLINE'])
  79. grass.message(_("Done. Use the following command to visualize the result:"))
  80. grass.message(_("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
  81. if __name__ == "__main__":
  82. options, flags = grass.parser()
  83. main()