3

I have a big logo image (1443 x 1453) that needs to be imported and added to the print layout. The image should be resized (to about 15% of its original size) and placed at a specific location in the layout. What would be the command for such an action?

1 Answer 1

5

The following should more less do what you want

project = QgsProject.instance()
manager = project.layoutManager()
layout = manager.layoutByName('your_layout')

layoutItemPicture = QgsLayoutItemPicture(layout)
layoutItemPicture.setResizeMode(QgsLayoutItemPicture.Zoom)
layoutItemPicture.setMode(QgsLayoutItemPicture.FormatRaster)
layoutItemPicture.setPicturePath('/mypath/myfile.png')

dim_image_original = [1443, 1453]
new_dim = [i * 0.15 for i in dim_image_original]
layoutItemPicture.attemptMove(QgsLayoutPoint(50, 50, QgsUnitTypes.LayoutMillimeters))
layoutItemPicture.attemptResize(QgsLayoutSize(*new_dim, QgsUnitTypes.LayoutPixels))

layout.addLayoutItem(layoutItemPicture)
2
  • Thats exactly what I needed, I just didnt know the QgsLayoutItemPicture function
    – Lino.Lohan
    Aug 10, 2020 at 14:28
  • All layout items inherit from qgis.org/api/classQgsLayoutItem.html. If you follow the link, you will see them in the diagram. When you don't know a class, add it manually with the GUI and reuse my recipe gis.stackexchange.com/a/370705/638 by printing layout.selectedLayoutItems() to see the class name you will need to use
    – ThomasG77
    Aug 10, 2020 at 14:55

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