2

I'm trying to make an atlas with QGIS, with one image per page. My goal is more complex so I have to use HTML widget in order to display the images. The below code works, but only if the picture is lighter than 2 Mo. If bigger, I got a square with "?" in it.

Code used to display image :

[% '<img width="300px" src="' + "relative/path/to/image.jpg" +  '" />' %]

I've already tried to change width / height, without success. If I change the resolution of a non-displaying picture, it is shown immediately in QGIS Atlas. Export entire atlas doesn't change the result...

Has anyone already faced this issue? I wonder if it is a hardware limitation, a QGIS limitation or a QGIS bug...

Configuration : Win10 + QGIS 3.16.2

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
samsam38
  • 21
  • 2

2 Answers2

1

This problem results from a timing issue. The Qt widget has not enough time to render the images inside the HTML. The same can happen with JavaScript or with SVG images. With a simple trick you can pause the item rendering until everything will be displayed.

Put the following Python code into the macro section of your QGIS project (Project -> Properties -> Macros):

from qgis.core import qgsfunction
from qgis.PyQt.QtCore import QTimer,QEventLoop
@qgsfunction(args=1, group='Custom', usesGeometry=False, referenced_columns=[])
def waitX(values, feature, parent):
    x = values[0]
    loop = QEventLoop()
    QTimer.singleShot(x,loop.quit)
    loop.exec_()
    return 0

And configure "Exclude item from export" (see Label/HTML Rendering settings) using a data defined override:

waitX(300)

enter image description here enter image description here

You have to try different values for the waitX function until your images are rendered completely.

christoph
  • 5,605
  • 1
  • 20
  • 34
0

Try to use:

<img width="300px" src="file:///C:/absolute/path/to/image.jpg" />
dcsa
  • 1
  • 1