Subgroup: Image

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.

Signals

Attributes

FlipHorizontal
FlipVertical
GrayscaleAverage
GrayscaleLightness
GrayscaleLuminosity
GrayscaleOff
class DistanceTransformProperties

Bases: sip.wrapper

QgsImageOperation.DistanceTransformProperties(QgsImageOperation.DistanceTransformProperties)

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)

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.
adjustHueSaturation(image: QImage, saturation: float, colorizeColor: Union[QColor, Qt.GlobalColor, QGradient] = QColor(), colorizeStrength: float = 1)

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
convertToGrayscale(image: QImage, mode: QgsImageOperation.GrayscaleMode = QgsImageOperation.GrayscaleLuminosity)

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

Parameters:
  • image – QImage to convert
  • mode – mode to use during grayscale conversion
cropTransparent(image: QImage, minSize: QSize = QSize(), center: bool = False) → QImage

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

New in version 2.9.

distanceTransform(image: QImage, properties: QgsImageOperation.DistanceTransformProperties)

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
flipImage(image: QImage, type: QgsImageOperation.FlipType)

Flips an image horizontally or vertically

Parameters:
  • image – QImage to flip
  • type – type of flip to perform (horizontal or vertical)
gaussianBlur(image: QImage, radius: int) → QImage

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

Parameters:
  • image – QImage to blur
  • radius – blur radius in pixels
Returns:

blurred image

Note

for fastest operation, ensure the source image is ARGB32_Premultiplied

multiplyOpacity(image: QImage, factor: float)

Multiplies opacity of image pixel values by a factor.

Parameters:
  • image – QImage to alter
  • factor – factor to multiple pixel’s opacity by
nonTransparentImageRect(image: QImage, minSize: QSize = QSize(), center: bool = False) → QRect

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

New in version 2.9.

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 to alter
  • color – color to overlay (any alpha component of the color is ignored)
stackBlur(image: QImage, radius: int, alphaOnly: bool = False)

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

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

Note

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