I am trying to access individual pixel values in a raster layer I'm working with. I can use QgsRasterBandStats to get some information about the raster layer with the following code:
layer = iface.activeLayer() print("Active Layer: ", layer.name()) provider = layer.dataProvider() extent = layer.extent() stats = provider.bandStatistics(1, QgsRasterBandStats.All) print("min value =", stats.minimumValue) print("max value =", stats.maximumValue)
I'm using the minimumValue and maximumValue attributes of QgsRasterBandStats. All the attributes are listed in the documentation.
In this raster layer I'm working with, there are really only 7 raster values. If I go into the symbology and manually classify these values, you can see them below.
What I'd really like to do is capture these values in a list, for example. Is there a way to access the individual pixel values in a raster object using QgsRasterBandStats?
Or could I choose a raster value (155 for example) and print the number of pixels with that value? Using something like the following pseudocode?
desired_value = 155 print(count of pixels with desired_value)