QImage

The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.

This class is a JS wrapper around Qt's QImage class

Example

const { QImage } = require('@nodegui/nodegui');
const image = new QImage();

Hierarchy

Index

Constructors

Properties

Methods

Constructors

constructor

+ new QImage(): QImage

Overrides Component.constructor

Constructs a null image

Returns: QImage

+ new QImage(native: NativeElement): QImage

Overrides Component.constructor

Constructs a null image Constructs an image and tries to load the image from the file with the given fileName Constructs an image with the given width, height and format Constructs an image with the given size and format

Parameters:

NameType
nativeNativeElement

Returns: QImage

+ new QImage(filename: string): QImage

Overrides Component.constructor

Constructs an image and tries to load the image from the file with the given fileName

Parameters:

NameType
filenamestring

Returns: QImage

+ new QImage(width: number, height: number, format: QImageFormat): QImage

Overrides Component.constructor

Constructs an image with the given width, height and format

Parameters:

NameType
widthnumber
heightnumber
formatQImageFormat

Returns: QImage

+ new QImage(size: QSize, format: QImageFormat): QImage

Overrides Component.constructor

Constructs an image with the given size and format

Parameters:

NameType
sizeQSize
formatQImageFormat

Returns: QImage

Properties

native

• native: NativeElement | null

Inherited from Component.native

Methods

allGray

â–¸ allGray(): boolean

Returns true if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise false

Returns: boolean


bitPlaneCount

â–¸ bitPlaneCount(): number

Returns the number of bit planes in the image

Returns: number


bytesPerLine

â–¸ bytesPerLine(): number

Returns the number of bytes per image scanline

Returns: number


cacheKey

â–¸ cacheKey(): number

Returns a number that identifies the contents of this QImage object. Distinct QImage objects can only have the same key if they refer to the same contents

Returns: number


color

â–¸ color(i: number): number

Returns the color in the color table at index i. The first color is at index 0

Parameters:

NameType
inumber

Returns: number


colorCount

â–¸ colorCount(): number

Returns the size of the color table for the image

Returns: number


convertTo

â–¸ convertTo(format: QImageFormat, flags: ImageConversionFlag): void

Detach and convert the image to the given format in place

Parameters:

NameTypeDefault
formatQImageFormat-
flagsImageConversionFlagImageConversionFlag.AutoColor

Returns: void


convertToFormat

â–¸ convertToFormat(format: QImageFormat, flags: ImageConversionFlag): QImage

Returns a copy of the image in the given format

Parameters:

NameTypeDefault
formatQImageFormat-
flagsImageConversionFlagImageConversionFlag.AutoColor

Returns: QImage


copy

â–¸ copy(rectangle: QRect): QImage

Returns a sub-area of the image as a new image

Parameters:

NameType
rectangleQRect

Returns: QImage

â–¸ copy(x: number, y: number, width: number, height: number): QImage

This is an overloaded function

Parameters:

NameType
xnumber
ynumber
widthnumber
heightnumber

Returns: QImage


createAlphaMask

â–¸ createAlphaMask(flags: ImageConversionFlag): QImage

Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if the image's format is QImageFormat::Format_RGB32

Parameters:

NameTypeDefault
flagsImageConversionFlagImageConversionFlag.AutoColor

Returns: QImage


createHeuristicMask

â–¸ createHeuristicMask(clipTight: boolean): QImage

Creates and returns a 1-bpp heuristic mask for this image

Parameters:

NameTypeDefault
clipTightbooleantrue

Returns: QImage


depth

â–¸ depth(): number

Returns the depth of the image

Returns: number


devicePixelRatio

â–¸ devicePixelRatio(): number

Returns the device pixel ratio for the image. This is the ratio between device pixels and device independent pixels.

Returns: number


dotsPerMeterX

â–¸ dotsPerMeterX(): number

Returns the number of pixels that fit horizontally in a physical meter. Together with dotsPerMeterY(), this number defines the intended scale and aspect ratio of the image

Returns: number


dotsPerMeterY

â–¸ dotsPerMeterY(): number

Returns the number of pixels that fit vertically in a physical meter. Together with dotsPerMeterX(), this number defines the intended scale and aspect ratio of the image

Returns: number


fill

â–¸ fill(color: QColor | GlobalColor): void

Fills the entire image with the given color

Parameters:

NameType
colorQColor | GlobalColor

Returns: void


format

â–¸ format(): QImageFormat

Returns the format of the image

Returns: QImageFormat


hasAlphaChannel

â–¸ hasAlphaChannel(): boolean

Returns true if the image has a format that respects the alpha channel, otherwise returns false

Returns: boolean


height

â–¸ height(): number

Returns the height of the image

Returns: number


invertPixels

â–¸ invertPixels(mode: QImageInvertMode): void

Inverts all pixel values in the image

Parameters:

NameTypeDefault
modeQImageInvertModeQImageInvertMode.InvertRgb

Returns: void


isGrayscale

â–¸ isGrayscale(): boolean

For 32-bit images, this function is equivalent to allGray()

Returns: boolean


isNull

â–¸ isNull(): boolean

Returns true if it is a null image, otherwise returns false

Returns: boolean


load

â–¸ load(fileName: string, format: string | null): boolean

Loads an image from the file with the given fileName. Returns true if the image was successfully loaded; otherwise invalidates the image and returns false

Parameters:

NameTypeDefault
fileNamestring-
formatstring | nullnull

Returns: boolean


loadFromData

â–¸ loadFromData(data: Buffer, format: string | null): boolean

Loads an image from the specified buffer

Parameters:

NameTypeDefault
dataBuffer-
formatstring | nullnull

Returns: boolean


mirrored

â–¸ mirrored(horizontal: boolean, vertical: boolean): QImage

Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false

Parameters:

NameTypeDefault
horizontalbooleanfalse
verticalbooleantrue

Returns: QImage


offset

â–¸ offset(): QPoint

Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images

Returns: QPoint


pixelColor

â–¸ pixelColor(point: QPoint): QColor

Returns the color of the pixel at the given position as a QColor

Parameters:

NameType
pointQPoint

Returns: QColor

â–¸ pixelColor(x: number, y: number): QColor

Returns the color of the pixel at coordinates (x, y) as a QColor

Parameters:

NameType
xnumber
ynumber

Returns: QColor


pixelIndex

â–¸ pixelIndex(point: QPoint): number

Returns the pixel index at the given position

Parameters:

NameType
pointQPoint

Returns: number

â–¸ pixelIndex(x: number, y: number): number

Returns the pixel index at (x, y)

Parameters:

NameType
xnumber
ynumber

Returns: number


rect

â–¸ rect(): QRect

Returns the enclosing rectangle (0, 0, width(), height()) of the image

Returns: QRect


reinterpretAsFormat

â–¸ reinterpretAsFormat(format: QImageFormat): boolean

Changes the format of the image to format without changing the data. Only works between formats of the same depth. Returns true if successful

Parameters:

NameType
formatQImageFormat

Returns: boolean


save

â–¸ save(fileName: string, format: string | null, quality: number): boolean

Saves the image to the file with the given fileName, using the given image file format and quality factor. If format is null, QImage will attempt to guess the format by looking at fileName's suffix.

Parameters:

NameTypeDefault
fileNamestring-
formatstring | nullnull
qualitynumber-1

Returns: boolean


scaled

â–¸ scaled(size: QSize, aspectRatioMode: AspectRatioMode, transformMode: TransformationMode): QImage

Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioMode and transformMode.

Parameters:

NameType
sizeQSize
aspectRatioModeAspectRatioMode
transformModeTransformationMode

Returns: QImage

â–¸ scaled(width: number, height: number, aspectRatioMode: AspectRatioMode, transformMode: TransformationMode): QImage

Returns a copy of the image scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode.

Parameters:

NameType
widthnumber
heightnumber
aspectRatioModeAspectRatioMode
transformModeTransformationMode

Returns: QImage


scaledToHeight

â–¸ scaledToHeight(height: number, mode: TransformationMode): QImage

Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode

Parameters:

NameTypeDefault
heightnumber-
modeTransformationModeTransformationMode.FastTransformation

Returns: QImage


scaledToWidth

â–¸ scaledToWidth(width: number, mode: TransformationMode): QImage

Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode

Parameters:

NameTypeDefault
widthnumber-
modeTransformationModeTransformationMode.FastTransformation

Returns: QImage


setAlphaChannel

â–¸ setAlphaChannel(alphaChannel: QImage): void

Sets the alpha channel of this image to the given alphaChannel

Parameters:

NameType
alphaChannelQImage

Returns: void


setColor

â–¸ setColor(index: number, colorValue: number): void

Sets the color at the given index in the color table, to the given to colorValue. The color value is an ARGB quadruplet

Parameters:

NameType
indexnumber
colorValuenumber

Returns: void


setColorCount

â–¸ setColorCount(colorCount: number): void

Resizes the color table to contain colorCount entries

Parameters:

NameType
colorCountnumber

Returns: void


setDevicePixelRatio

â–¸ setDevicePixelRatio(ratio: number): void

Sets the device pixel ratio for the image. This is the ratio between image pixels and device-independent pixels

Parameters:

NameType
rationumber

Returns: void


setDotsPerMeterX

â–¸ setDotsPerMeterX(x: number): void

Sets the number of pixels that fit horizontally in a physical meter, to x

Parameters:

NameType
xnumber

Returns: void


setDotsPerMeterY

â–¸ setDotsPerMeterY(y: number): void

Sets the number of pixels that fit vertically in a physical meter, to y

Parameters:

NameType
ynumber

Returns: void


setOffset

â–¸ setOffset(offset: QPoint): void

Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset

Parameters:

NameType
offsetQPoint

Returns: void


setPixel

â–¸ setPixel(position: QPoint, indexOrRgb: number): void

Sets the pixel index or color at the given position to indexOrRgb

Parameters:

NameType
positionQPoint
indexOrRgbnumber

Returns: void

â–¸ setPixel(x: number, y: number, indexOrRgb: number): void

Sets the pixel index or color at (x, y) to indexOrRgb

Parameters:

NameType
xnumber
ynumber
indexOrRgbnumber

Returns: void


setPixelColor

â–¸ setPixelColor(position: QPoint, color: QColor): void

Sets the color at the given position to color

Parameters:

NameType
positionQPoint
colorQColor

Returns: void

â–¸ setPixelColor(x: number, y: number, color: QColor): void

Sets the pixel color at (x, y) to color

Parameters:

NameType
xnumber
ynumber
colorQColor

Returns: void


setText

â–¸ setText(key: string, value: string): void

Sets the image text to the given text and associate it with the given key

Parameters:

NameType
keystring
valuestring

Returns: void


size

â–¸ size(): QSize

Returns the size of the image, i.e. its width() and height()

Returns: QSize


sizeInBytes

â–¸ sizeInBytes(): number

Returns the image data size in bytes

Returns: number


swap

â–¸ swap(other: QImage): void

Swaps image other with this image. This operation is very fast and never fails

Parameters:

NameType
otherQImage

Returns: void


text

â–¸ text(key: string): string

Returns the image text associated with the given key. If the specified key is an empty string, the whole image text is returned, with each key-text pair separated by a newline

Parameters:

NameType
keystring

Returns: string


textKeys

â–¸ textKeys(): string[]

Returns the text keys for this image

Returns: string[]


valid

â–¸ valid(pos: QPoint): boolean

Returns true if pos is a valid coordinate pair within the image; otherwise returns false

Parameters:

NameType
posQPoint

Returns: boolean

â–¸ valid(x: number, y: number): boolean

Returns true if QPoint(x, y) is a valid coordinate pair within the image; otherwise returns false

Parameters:

NameType
xnumber
ynumber

Returns: boolean


width

â–¸ width(): number

Returns the width of the image

Returns: number


Static fromQVariant

â–¸ fromQVariant(variant: QVariant): QImage

Parameters:

NameType
variantQVariant

Returns: QImage