I have a .qml file that I am using on a project importing photos using the 'import photos' plugin. Every import batch needs to have the color manually changed after importing in order to distinguish it from the other imports. This is something I'll be doing hundreds of times. Is there a way to set the qml file to randomize the color when importing the data?

2 Answers 2


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....


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.

  • Unfortunately it doesn't work with the import photos plugin.
    – brink
    Jul 1, 2020 at 23:35
  • 1
    Worked fine for me with import photos. If you create the .qml file using a layer created by import photos note that there are multiple markers, you may have to do this for each one. Probably easier to just use a point layer as @JonasV described.
    – Nick
    Jul 2, 2020 at 3:49
  • 1
    @brink In my test it also worked with the plugin. This is likely an issue with the qml file you have or maybe the qgis version you are using. If you are still using QGIS 2, there might be some issues. The Code I have pasted will definitely work for Version 3.12
    – JonasV
    Jul 2, 2020 at 6:33
  • @JonasV I'm using 3.12. I added the code to a point layer and saved as a .qml. My qml file also includes actions for viewing the images. The code is there on the new layer but no name change or color change. Are you including the qml file when running Import Photos or loading the style after? It is doing something strange though. The preview marker is a different color then the actual color.
    – brink
    Jul 7, 2020 at 17:20
  • For this it is probably better to use the code by @jgm_GIS posted in the answer below
    – JonasV
    Jul 7, 2020 at 18:13

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
#refresh the legend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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