QgsImageOperation class

Contains operations and filters which apply to QImages.

A set of optimised pixel manipulation operations and filters which can be applied to QImages. All operations only apply to ARGB32 format images, and it is left up to the calling procedure to ensure that any passed images are of the correct format.

Operations are written to either modify an image in place or return a new image, depending on which is faster for the particular operation.

Public types

struct DistanceTransformProperties
Struct for storing properties of a distance transform operation.
enum FlipType { FlipHorizontal, FlipVertical }
Flip operation types.
enum GrayscaleMode { GrayscaleLightness, GrayscaleLuminosity, GrayscaleAverage, GrayscaleOff }
Modes for converting a QImage to grayscale.

Public static functions

static void adjustBrightnessContrast(QImage& image, int brightness, double contrast)
Alter the brightness or contrast of a QImage.
static void adjustHueSaturation(QImage& image, double saturation, const QColor& colorizeColor = QColor(), double colorizeStrength = 1.0)
Alter the hue or saturation of a QImage.
static void convertToGrayscale(QImage& image, GrayscaleMode mode = GrayscaleLuminosity)
Convert a QImage to a grayscale image.
static auto cropTransparent(const QImage& image, QSize minSize = QSize(), bool center = false) -> QImage
Crop any transparent border from around an image.
static void distanceTransform(QImage& image, const QgsImageOperation::DistanceTransformProperties& properties)
Performs a distance transform on the source image and shades the result using a color ramp.
static void flipImage(QImage& image, FlipType type)
Flips an image horizontally or vertically.
static auto gaussianBlur(QImage& image, int radius) -> QImage*
Performs a gaussian blur on an image.
static void multiplyOpacity(QImage& image, double factor)
Multiplies opacity of image pixel values by a factor.
static auto nonTransparentImageRect(const QImage& image, QSize minSize = QSize(), bool center = false) -> QRect
Calculates the non-transparent region of an image.
static void overlayColor(QImage& image, const QColor& color)
Overlays a color onto an image.
static void stackBlur(QImage& image, int radius, bool alphaOnly = false)
Performs a stack blur on an image.

Enum documentation

enum QgsImageOperation::FlipType

Flip operation types.

Enumerators
FlipHorizontal

Flip the image horizontally.

FlipVertical

Flip the image vertically.

enum QgsImageOperation::GrayscaleMode

Modes for converting a QImage to grayscale.

Enumerators
GrayscaleLightness

Keep the lightness of the color, drops the saturation.

GrayscaleLuminosity

Grayscale by perceptual luminosity (weighted sum of color RGB components)

GrayscaleAverage

Grayscale by taking average of color RGB components.

GrayscaleOff

No change.

Function documentation

static void QgsImageOperation::adjustBrightnessContrast(QImage& image, int brightness, double contrast)

Alter the brightness or contrast of a QImage.

Parameters
image QImage to alter
brightness brightness value, in the range -255 to 255. A brightness value of 0 indicates no change to brightness, a negative value will darken the image, and a positive value will brighten the image.
contrast contrast value. Must be a positive or zero value. A value of 1.0 indicates no change to the contrast, a value of 0 represents an image with 0 contrast, and a value > 1.0 will increase the contrast of the image.

static void QgsImageOperation::adjustHueSaturation(QImage& image, double saturation, const QColor& colorizeColor = QColor(), double colorizeStrength = 1.0)

Alter the hue or saturation of a QImage.

Parameters
image QImage to alter
saturation double between 0 and 2 inclusive, where 0 = desaturate and 1.0 = no change
colorizeColor color to use for colorizing image. Set to an invalid QColor to disable colorization.
colorizeStrength double between 0 and 1, where 0 = no colorization and 1.0 = full colorization

static void QgsImageOperation::convertToGrayscale(QImage& image, GrayscaleMode mode = GrayscaleLuminosity)

Convert a QImage to a grayscale image.

Parameters
image QImage to convert
mode mode to use during grayscale conversion

Alpha channel is preserved.

static QImage QgsImageOperation::cropTransparent(const QImage& image, QSize minSize = QSize(), bool center = false)

Crop any transparent border from around an image.

Parameters
image source image
minSize minimum size for cropped image, if desired. If the cropped image is smaller than the minimum size, it will be centered in the returned image.
center cropped image will be centered on the center of the original image if set to true

static void QgsImageOperation::distanceTransform(QImage& image, const QgsImageOperation::DistanceTransformProperties& properties)

Performs a distance transform on the source image and shades the result using a color ramp.

Parameters
image QImage to alter
properties DistanceTransformProperties object with parameters for the distance transform operation

static void QgsImageOperation::flipImage(QImage& image, FlipType type)

Flips an image horizontally or vertically.

Parameters
image QImage to flip
type type of flip to perform (horizontal or vertical)

static QImage* QgsImageOperation::gaussianBlur(QImage& image, int radius)

Performs a gaussian blur on an image.

Parameters
image QImage to blur
radius blur radius in pixels
Returns blurred image

Gaussian blur is slower but results in a high quality blur.

static void QgsImageOperation::multiplyOpacity(QImage& image, double factor)

Multiplies opacity of image pixel values by a factor.

Parameters
image QImage to alter
factor factor to multiple pixel's opacity by

static QRect QgsImageOperation::nonTransparentImageRect(const QImage& image, QSize minSize = QSize(), bool center = false)

Calculates the non-transparent region of an image.

Parameters
image source image
minSize minimum size for returned region, if desired. If the non-transparent region of the image is smaller than this minimum size, it will be centered in the returned rectangle.
center return rectangle will be centered on the center of the original image if set to true

static void QgsImageOperation::overlayColor(QImage& image, const QColor& color)

Overlays a color onto an image.

Parameters
image QImage to alter
color color to overlay (any alpha component of the color is ignored)

This operation retains the alpha channel of the original image, but replaces all image pixel colors with the specified color.

static void QgsImageOperation::stackBlur(QImage& image, int radius, bool alphaOnly = false)

Performs a stack blur on an image.

Parameters
image QImage to blur
radius blur radius in pixels, maximum value of 16
alphaOnly set to true to blur only the alpha component of the image

Stack blur represents a good balance between speed and blur quality.