I'm basically making a script to save an image in .png from Qgis with a script and I would like to add a scale bar on it. I'm using this code :

    size = self.iface.mapCanvas().size()
    image = QImage(size, QImage.Format_RGB32)

    painter = QPainter(image)
    settings = self.iface.mapCanvas().mapSettings()

    layers = settings.layers()

    job = QgsMapRendererCustomPainterJob(settings, painter)

I see that it's possible to do it with a Map Composer, but I had other problems while saving from Map Composer (mainly with the quality of the image output) : https://docs.qgis.org/2.14/en/docs/pyqgis_developer_cookbook/composer.html

Is that possible to add a scale bar on my script ?

  • If the scale is always the same you could save an image with the scale and then apply it to the final .png of the map. It's a tricky workaround but could work. Surely pillow.readthedocs.io/en/latest could do the job. – Oscar Campo Mar 11 '18 at 9:34
  • Unfortunately, the scale is changing for all of the print. But I define the extent of the image before saving so I can do something similar to draw a scale ! Thank's for the trick. I will try it as soon as possible – Lionel Mar 12 '18 at 10:13

Your Answer

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

Browse other questions tagged or ask your own question.