Class: QgsImageCache

class qgis.core.QgsImageCache

Bases: QgsAbstractContentCacheBase

A cache for images derived from raster files.

QgsImageCache stores pre-rendered resampled versions of raster image files, allowing efficient reuse without incurring the cost of resampling on every render.

QgsImageCache is not usually directly created, but rather accessed through QgsApplication.imageCache().

New in version 3.6.

QgsImageCache(parent: QObject = None) Constructor for QgsImageCache, with the specified parent object.

Methods

checkReply

Runs additional checks on a network reply to ensure that the reply content is consistent with that required by the cache.

childEvent

connectNotify

customEvent

disconnectNotify

isSignalConnected

onRemoteContentFetched

Triggered after remote content (i.e.

originalSize

Returns the original size (in pixels) of the image at the specified path.

pathAsImage

Returns the specified path rendered as an image.

receivers

sender

senderSignalIndex

timerEvent

Signals

remoteImageFetched

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

checkReply(self, reply: QNetworkReply, path: str) bool

Runs additional checks on a network reply to ensure that the reply content is consistent with that required by the cache.

childEvent(self, QChildEvent)
connectNotify(self, QMetaMethod)
customEvent(self, QEvent)
disconnectNotify(self, QMetaMethod)
isSignalConnected(self, QMetaMethod) bool
onRemoteContentFetched(self, url: str, success: bool)

Triggered after remote content (i.e. HTTP linked content at the given url) has been fetched.

The success argument will be True if the content was successfully fetched, or False if it was not fetched successfully.

originalSize(self, path: str, blocking: bool = False) QSize

Returns the original size (in pixels) of the image at the specified path.

path may be a local file, remote (HTTP) url, or a base 64 encoded string (with a “base64:” prefix).

If path is a remote file, then an invalid size may be returned while the image is in the process of being fetched.

The blocking boolean forces to wait for loading before returning the original size. The content is loaded in the same thread to ensure provided the original size. WARNING: the blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

If the image could not be read then an invalid QSize is returned.

Parameters
  • path (str) –

  • blocking (bool = False) –

Return type

QSize

pathAsImage(self, path: str, size: QSize, keepAspectRatio: bool, opacity: float, blocking: bool = False, targetDpi: float = 96) Tuple[QImage, bool]

Returns the specified path rendered as an image. If possible, a pre-existing cached version of the image will be used. If not, the image is fetched and resampled to the desired size, and then the result cached for subsequent lookups.

path may be a local file, remote (HTTP) url, or a base 64 encoded string (with a “base64:” prefix).

The size parameter dictates the target size of the image. An invalid size indicates the original raster image size (with no resampling). A size in which the width or height is set to zero will have the zeroed value automatically computed when keepAspectRatio is True.

If keepAspectRatio is True, then the original raster aspect ratio will be maintained during any resampling operations.

An opacity parameter dictates the opacity of the image.

If the resultant raster was of a sufficiently small size to store in the cache, then fitsInCache will be set to True.

The blocking boolean forces to wait for loading before returning image. The content is loaded in the same thread to ensure provided the image. WARNING: the blocking parameter must NEVER be True from GUI based applications (like the main QGIS application) or crashes will result. Only for use in external scripts or QGIS server.

Since QGIS 3.22 the targetDpi argument can be used to specify an explicit DPI to render the image at. This is used for some image formats (e.g. PDF) to ensure that content is rendered at the desired DPI. This argument is only used when an invalid size argument is specified. If a valid size is specified then the image will always be rendered at this size, regardless of the targetDpi.

Parameters
  • path (str) –

  • size (QSize) –

  • keepAspectRatio (bool) –

  • opacity (float) –

  • blocking (bool = False) –

  • targetDpi (float = 96) –

Return type

Tuple[QImage, bool]

receivers(self, PYQT_SIGNAL) int
remoteImageFetched

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type

PYQT_SIGNAL

sender(self) QObject
senderSignalIndex(self) int
timerEvent(self, QTimerEvent)