Can you edit a QGIS layer style file (.qml) to create random colors?

Yes there is a way, and it is only a bit hacky:

In a point file, open up the symbology and hit the "Data defined override" to the right of Fill color (for reference see the image). In the context menu which opens up, choose Edit....

Symbology

Here you can paste this code:

'#' || right(@layer_id, 6)

Then you can save the symbology as .qml and use it to import your photos.

The code takes advantage of how the layer ids get generated in QGIS. The layer name will be appended with somewhat random hex values, which we can use to change colors. Since every layer gets a unique layer ID, every layer will also most likely get a unique color.


You could open the python console and execute the code below, for QGIS 3.X, to randomly change all the layers' colors.

from random import randrange
layers = [layer for layer in QgsProject.instance().mapLayers().values()]
for layer in layers:
    layer.renderer().symbol().setColor(QColor.fromRgb(randrange(256), randrange(256), randrange(256)))
    #redraw the layer
    layer.triggerRepaint()
#refresh the legend
iface.layerTreeView().refreshLayerSymbology(layer.id())

Tags:

Qml

Qgis