r_rgb.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. ***************************************************************************
  3. r_rgb.py
  4. --------
  5. Date : February 2016
  6. Copyright : (C) 2016 by Médéric Ribreux
  7. Email : medspx at medspx dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Médéric Ribreux'
  18. __date__ = 'February 2016'
  19. __copyright__ = '(C) 2016, Médéric Ribreux'
  20. def processInputs(alg, parameters, context, feedback):
  21. if 'input' in alg.exportedLayers:
  22. return
  23. # We need to import all the bands and color tables of the input raster
  24. alg.loadRasterLayerFromParameter('input', parameters, context, False, None)
  25. alg.postInputs(context)
  26. def processCommand(alg, parameters, context, feedback):
  27. # if the input raster is multiband: export each component directly
  28. rasterInput = alg.exportedLayers['input']
  29. raster = alg.parameterAsRasterLayer(parameters, 'input', context)
  30. for color in ['red', 'green', 'blue']:
  31. alg.exportedLayers[color] = color + alg.uniqueSuffix
  32. # If the raster is not multiband, really do r.rgb
  33. if raster.bandCount() == 1:
  34. alg.commands.append(" r.rgb input={} red={} green={} blue={} --overwrite".format(
  35. rasterInput,
  36. alg.exportedLayers['red'],
  37. alg.exportedLayers['green'],
  38. alg.exportedLayers['blue']
  39. ))
  40. def processOutputs(alg, parameters, context, feedback):
  41. raster = alg.parameterAsRasterLayer(parameters, 'input', context)
  42. # if the raster was monoband, export from r.rgb
  43. for color in ['red', 'green', 'blue']:
  44. fileName = alg.parameterAsOutputLayer(parameters, color, context)
  45. if raster.bandCount() == 1:
  46. grassName = '{}{}'.format(color, alg.uniqueSuffix)
  47. else:
  48. grassName = '{}.{}'.format(alg.exportedLayers['input'], color)
  49. alg.exportRasterLayer(grassName, fileName, True)