3

I am looking for a way to add a picture to my map using pyqgis. Are there any examples of how to do this. I know it can be done using the classQgsComposerPicture. I am not much of a programmer so I cannot decode the api documentation. Any links where I can find such examples or documentation with options.

2 Answers 2

2

If I want to add an icon QGIS logo (111 pixels x 111 pixels) to my composer map, the snipped code that it works is:

.
.
.

#Initialize the picture object
logo = QgsComposerPicture(c) #c is a QgsComposition object
logo.setPictureFile("/home/zeito/pyqgis_data/qgis-icon_21.png")
logo.setSceneRect(QRectF(0,0,40,40)) #Resize logo
logo. setItemPosition(20,20)
c.addItem(logo)
.
.
.

The result is:

enter image description here

With this QRectF(0,0,20,20) object I have:

enter image description here

I have to use the 'setSceneRect' method of QgsComposerPicture class.

2

Seeing other examples from the PyQGIS cookbook , the following should work

image = QgsComposerPicture(c)
image.setItemPosition(x, y)
image.setPicturePath('/path/to/image')
image.updateItem()
c.addItem(image)

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.