How do I add any logo or img of any size on any layer using PyQGIS? Actually I want to add a watermark on a layer using PyQGIS.

  • Here in this blog post opensourceoptions.com/blog/… the author details the general process of using map layouts in pyQGIS. There is nothing on images in there, but you can see the general idea of it. – til_b Aug 24 '20 at 9:30
  • Actually i have general idea about adding layer and all i need some logo on my layer. e.g. when we call a layer we need some logo at bottom right corner of that layer. – abhinav gaurav Aug 24 '20 at 9:47
  • You cant add image to a layer but you can try to add an image to QGIS canvas or to print layout. If you want to add a watermark when a specific layer is added you may look a the copyright label decoration using an expression – J.R Aug 24 '20 at 11:16
  • 1
    Just go to View > Decorations > Copyright Label then erase the default copyright text and insert an expression like this one : CASE When array_to_string( @map_layer_ids ) ILIKE '%YOUR_LAYER_NAME%' then 'Watermark' ELSE '' END . Just edit the Watermak part to the text you want to display and change YOUR_LAYER_NAME by the actual layer name. I didn't post that as an answer because it will only display text and not an image like you want – J.R Aug 24 '20 at 12:33
  • 1
    @til_b No decorators don't show up in print layout but you may use a similar expression in a print layout text label to get the same result (this gis.stackexchange.com/questions/345953/… could help you to create the right expression) – J.R Aug 26 '20 at 16:33

I use this to add a logo in the print layout:

logo_path = os.path.join(os.path.expanduser('~'), '/home/xx/Destop/Logo.png')

logo = QgsLayoutItemPicture(layout)
logo.attemptMove(QgsLayoutPoint(x, y, QgsUnitTypes.LayoutMillimeters))
logo.attemptResize(QgsLayoutSize(x, y, QgsUnitTypes.LayoutMillimeters))

for x and y set the coordinates in the layout.


I just got this code working:

img = QgsLayoutItemPicture(layout)

# set the image size
img.attemptResize(QgsLayoutSize(20, 20,QgsUnitTypes.LayoutMillimeters))
# move to exact position
img.attemptMove(QgsLayoutPoint(230, 170, QgsUnitTypes.LayoutMillimeters))

What I found helpful was to create the layout by hand in the print composer to see how the elements look like at their position, then note the numbers for size and position of my elements, and then use these numbers in the code.

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