I find the QGIS html annotation tool is very useful for quickly accessing a combination of key data attributes and photos. I am having trouble using relative paths for images.

This doesn't work:

<img src="[% html_src %]" /> 


<img src="/Image/Picture.JPG" />

I'm wondering if it's possible to employ a QgsProject.instance() in the QGIS Macro option under project properties. I don't know html very well, so I'm wondering how I could link the relative pathname to the project path using something like this:

proj = QgsProject.instance()
UriFile = str(proj.fileName())
pathname = str(os.path.dirname(UriFile))

How can I concatenate this in the html display option?

2 Answers 2


I think you can solve this problem, if you adapt your situation, like in the example bellow.

Presuming this is how it looks the vector layer's attribute table:

enter image description here

Here's the images' storing folder:

enter image description here

And this is the annotation.html :

<b> [% "name" %] </b>
<br />
<img src=file:///c:/temp/images/[% "img_src" %].png />

Eventually, here's the result:

enter image description here

  • Indeed, the attribute table contains a relative pathname, but the html display still requires the root folder c:/temp/ to be defined. Is it possible to adapt this to a situation where the only information known about the image source is relative to the directory containing the QGIS project?
    – user25976
    Commented Mar 28, 2014 at 21:02

If your imagesources are stored in the attribute table of a vector layer you can use the field calculator to add a new virtual field. In this field you can store i.e. the path to the project folder (can be found in the variables section):

add project folder field

Your annotation html would then look somthing like this:

<img src="file:///[% "root" %]/[% "img_path" %]">

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.