Class: QgsImageOperation

class qgis.core.QgsImageOperation

Bases: sip.wrapper

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.

New in version 2.7:

Methods

adjustBrightnessContrast

Alter the brightness or contrast of a QImage.

adjustHueSaturation

Alter the hue or saturation of a QImage.

convertToGrayscale

Convert a QImage to a grayscale image.

cropTransparent

Crop any transparent border from around an image.

distanceTransform

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

flipImage

Flips an image horizontally or vertically

gaussianBlur

Performs a gaussian blur on an image.

multiplyOpacity

Multiplies opacity of image pixel values by a factor.

nonTransparentImageRect

Calculates the non-transparent region of an image.

overlayColor

Overlays a color onto an image.

stackBlur

Performs a stack blur on an image.

Attributes

FlipHorizontal

FlipVertical

GrayscaleAverage

GrayscaleLightness

GrayscaleLuminosity

GrayscaleOff

class DistanceTransformProperties
class DistanceTransformProperties(QgsImageOperation.DistanceTransformProperties)

Bases: sip.wrapper

ramp
shadeExterior
spread
useMaxDistance
FlipHorizontal = 0
class FlipType

Bases: int

FlipVertical = 1
GrayscaleAverage = 2
GrayscaleLightness = 0
GrayscaleLuminosity = 1
class GrayscaleMode

Bases: int

GrayscaleOff = 3
adjustBrightnessContrast(image: QImage, brightness: int, contrast: float, feedback: QgsFeedback = None)

Alter the brightness or contrast of a QImage.

Parameters
  • image (QImage) – QImage to alter

  • brightness (int) – 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 (float) – 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.

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

adjustHueSaturation(image: QImage, saturation: float, colorizeColor: Union[QColor, Qt.GlobalColor, QGradient] = QColor(), colorizeStrength: float = 1, feedback: QgsFeedback = None)

Alter the hue or saturation of a QImage.

Parameters
  • image (QImage) – QImage to alter

  • saturation (float) – double between 0 and 2 inclusive, where 0 = desaturate and 1.0 = no change

  • colorizeColor (Union[QColor) – color to use for colorizing image. Set to an invalid QColor to disable colorization.

  • colorizeStrength (float = 1) – double between 0 and 1, where 0 = no colorization and 1.0 = full colorization

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

convertToGrayscale(image: QImage, mode: QgsImageOperation.GrayscaleMode = QgsImageOperation.GrayscaleLuminosity, feedback: QgsFeedback = None)

Convert a QImage to a grayscale image. Alpha channel is preserved.

Parameters
  • image (QImage) – QImage to convert

  • mode (QgsImageOperation.GrayscaleMode = QgsImageOperation.GrayscaleLuminosity) – mode to use during grayscale conversion

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

cropTransparent(image: QImage, minSize: QSize = QSize(), center: bool = False) QImage

Crop any transparent border from around an image.

Parameters
  • image (QImage) – source image

  • minSize (QSize = QSize()) – 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 (bool = False) – cropped image will be centered on the center of the original image if set to True

New in version 2.9.

Return type

QImage

distanceTransform(image: QImage, properties: QgsImageOperation.DistanceTransformProperties, feedback: QgsFeedback = None)

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

Parameters
  • image (QImage) – QImage to alter

  • properties (QgsImageOperation.DistanceTransformProperties) – DistanceTransformProperties object with parameters for the distance transform operation

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

flipImage(image: QImage, type: QgsImageOperation.FlipType)

Flips an image horizontally or vertically

Parameters
gaussianBlur(image: QImage, radius: int, feedback: QgsFeedback = None) QImage

Performs a gaussian blur on an image. Gaussian blur is slower but results in a high quality blur.

Parameters
  • image (QImage) – QImage to blur

  • radius (int) – blur radius in pixels

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

Return type

QImage

Returns

blurred image

Note

for fastest operation, ensure the source image is ARGB32_Premultiplied

multiplyOpacity(image: QImage, factor: float, feedback: QgsFeedback = None)

Multiplies opacity of image pixel values by a factor.

Parameters
  • image (QImage) – QImage to alter

  • factor (float) – factor to multiple pixel’s opacity by

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

nonTransparentImageRect(image: QImage, minSize: QSize = QSize(), center: bool = False) QRect

Calculates the non-transparent region of an image.

Parameters
  • image (QImage) – source image

  • minSize (QSize = QSize()) – 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 (bool = False) – return rectangle will be centered on the center of the original image if set to True

New in version 2.9.

Return type

QRect

overlayColor(image: QImage, color: Union[QColor, Qt.GlobalColor, QGradient])

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

Parameters
  • image (QImage) – QImage to alter

  • color (Union[QColor) – color to overlay (any alpha component of the color is ignored)

stackBlur(image: QImage, radius: int, alphaOnly: bool = False, feedback: QgsFeedback = None)

Performs a stack blur on an image. Stack blur represents a good balance between speed and blur quality.

Parameters
  • image (QImage) – QImage to blur

  • radius (int) – blur radius in pixels, maximum value of 16

  • alphaOnly (bool = False) – set to True to blur only the alpha component of the image

  • feedback (QgsFeedback = None) – optional feedback object for responsive cancellation (since QGIS 3.22)

Note

for fastest operation, ensure the source image is ARGB32_Premultiplied if alphaOnly is set to False, or ARGB32 if alphaOnly is True