I'm working in the Python-Console of QGIS and I have an object form the class
QgsRasterLayer. I want to get the indices of those pixels that have certain pixel value (e.g. I need the indices of all the pixels with a pixel value = 50), but I don't know how.
This script should work:
# lyr = iface.mapCanvas().currentLayer() # You don't need this if you already have a QgsRasterLayer prov = lyr.dataProvider() extent = prov.extent() width = prov.xSize() height = prov.ySize() block = prov.block(1, extent, width, height) for x in range(width): for y in range(height): val = block.value(y, x) if val == 50: print x, y
Note that this gives you the indices of the pixels, and not their geographic coordinates.