1

I can't seem to add a picture to my composition using PyQGIS in Windows 8. My code is based on the example I found in this question. I've tried using .setPicturePath and .setPictureFile but none seem to work for me. Instead, my map shows a red 'x' where the arrow should appear. Since the question was answered 2 years ago, has this method been superseded?

My code is:

#Add north arrow
arrow = QgsComposerPicture(c) 
arrow.setPicturePath("C:\Program Files\QGIS 2.18\apps\qgis\svg\arrows\Arrow_03")
arrow.setSceneRect(QRectF(0,0,10,10)) #Resize 
arrow.setItemPosition(20,20)
c.addItem(arrow)

Ideally, I want to replace the path with QgsApplication.svgPaths()[0] + "\Arrow_03"but I figured I should get it working with a hard coded path first

1
  • 3
    you are missing the file extension
    – JGH
    Sep 12, 2017 at 12:12

2 Answers 2

2

Along with JGH comment the path to the image needs to be forward slashes or have the 'r' in front of it so the back slashes are not interpreted as character escapes.

Try:

arrow.setPicturePath(r"C:\Program Files\QGIS 2.18\apps\qgis\svg\arrows\Arrow_03.svg")

or

arrow.setPicturePath("C:/Program Files/QGIS 2.18/apps/qgis/svg/arrows/Arrow_03.svg")
1

You are missing the file extension

Your Answer

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

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