PyQGIS Tutorial: Adding Images to Map

In this QGIS Python tutorial series, I'll show you how to spice up your maps by adding images. Integrating objects like images can significantly enhance the map's value and overall appeal. For instance, embedding a company or organization's logo offers viewers a clear indicator of the map's creator, providing valuable information at a glance.

Figure 1 showcases a time series map of Covid-19 death cases, crafted some time ago. With the addition of a logo, it distinctly communicates to viewers that Geodose is the creator, eliminating the need for them to inquire about its origin. In QGIS we can add an image to map easly using Decorations, but how to do using Python? Keep reading!

Map with an Image Logo
Figure 1. Map with an Image Logo

Importing Libraries

Let's start this tutorial with importing two classes in QT framework: QPixmap and QGraphicsPixmapItem as following.

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QGraphicsPixmapItem

Define Map Canvas

Next define a canvas variable with iface.mapCanvas(). In QGIS, iface.mapCanvas() is a method used to access the main map canvas through the QGIS Python API. This canvas represents the main area where map layers and their contents are displayed within the QGIS application.

canvas=iface.mapCanvas()  

Create a QPixmap from the image file

In the following code snippet, an image path pointing to the 'geodose_logo.png' file is stored in the variable 'image_path'. Using this path, a QPixmap object named 'original_pixmap' is created by converting the image file into a QPixmap format. This 'original_pixmap' variable now holds the image data in a format that can be further manipulated or utilized within the program.

# Image Path
image_path = '/media/ideageo/pic/logo/geodose_logo.png'

# Create a QPixmap from the image file
original_pixmap = QPixmap(image_path)

Scale the Image

Up to this code, we can add the image to QGIS map canvas, but might be the image is too small or too big, for that we need to scale it. To scale the image can be done using the following code:

# Scale the pixmap to 50% of its original size
scaled_pixmap = original_pixmap.scaled(
    original_pixmap.width() * 0.5,
    original_pixmap.height() * 0.5
)

# Create a QGraphicsPixmapItem using the scaled QPixmap
scaled_pixmap_item = QGraphicsPixmapItem(scaled_pixmap)

In the code snippet, the original QPixmap object, 'original_pixmap', is scaled down to 50% of its original size using the 'scaled' method. The resulting scaled pixmap is stored in the variable 'scaled_pixmap'. Next, a QGraphicsPixmapItem named 'scaled_pixmap_item' is created using this scaled QPixmap. This item is now a graphical representation of the scaled image and can be added to a QGraphicsScene for display within a QGIS map canvas.

Define Image's Position

Before we add it to map canvas, let's define the image's position. In this case I'd like to put it on left bottom of map canvas using the code below:

# Get the dimensions of the canvas
canvas_width = canvas.width()
canvas_height = canvas.height()

# Calculate the position for the bottom left corner
bottom_left_x = 0  # Leftmost position
bottom_left_y = canvas_height - scaled_pixmap.height()  # Bottom position

# Set the position of the QGraphicsPixmapItem
scaled_pixmap_item.setPos(bottom_left_x, bottom_left_y)

# Add the QGraphicsPixmapItem to the canvas scene
canvas.scene().addItem(scaled_pixmap_item)

This code snippet retrieves the dimensions of the map canvas using 'canvas.width()' and 'canvas.height()' to determine its width and height, respectively. It then calculates the position for the bottom-left corner of the canvas, assigning the leftmost position to 'bottom_left_x' and positioning 'bottom_left_y' at the bottom of the canvas, considering the height of the scaled pixmap.

The position of the QGraphicsPixmapItem, represented by 'scaled_pixmap_item', is then set using 'setPos()' to place it at the calculated bottom-left corner coordinates within the canvas.

Add the Image to Map Canvas

Finally, the 'scaled_pixmap_item' is added to the canvas scene using 'canvas.scene().addItem(scaled_pixmap_item)' to display the scaled image at the specified position within the map canvas.

Remove the Image from the Map

To delete the image from the map can be done using the removeItem() method as the  code below:

# Remove the QGraphicsPixmapItem from the canvas scene
canvas.scene().removeItem(scaled_pixmap_item)

That's all this tutorial how to add image to QGIS map canvas. Hope this tutorial could help you in learning QGIS with Python. For other interesting PyQGIS tutorials please visit PyQGIS Tutorial Series. Thanks for reading!


Related Posts

Disqus Comments