2
rasterlayer = QgsRasterLayer(infilename, layername)
renderer = rasterlayer.renderer()

rasterlayer is the active layer with renderer type is Palette, I want to get current Palette Color, how could I do? Is the Attribute in renderer?

enter image description here

1

You can get it with .getRgb() method of QColor object:

rgb_color = renderer.legendSymbologyItems()[-1][1].getRgb()

This will return you a tuple with (red, green, blue, transparency) values. Here renderer.legendSymbologyItems()[-1] returns you the last class from your palette as a tuple with its (name, PyQt5.QtGui.QColor) values.

P.S. there are also .getHsv() and .getCmyk() and a couple more methods at your service.

  • Thanks, it works well, well done. I can get label and color: legendSymbologyItems()[i][0], legendSymbologyItems()[i][1].getRgb(). But I also want to konw how to get the value in QgsPalettedRasterRenderer.Class(value, color, label) from legendSymbologyItems, can you help me? – magicmelon Jul 9 '18 at 1:33
  • You are welcome. Could you please ask a separate question and clearly specify details there? – Basile Jul 9 '18 at 21:40
  • ok! thank you, new question title: pyqgis: how i get "value", "color", "label" from existing palette raster layer – magicmelon Jul 10 '18 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.