QImage¶
Synopsis¶
Functions¶
def
__eq__
(arg__1)def
__ne__
(arg__1)def
allGray
()def
alphaChannel
()def
bitPlaneCount
()def
byteCount
()def
bytesPerLine
()def
cacheKey
()def
color
(i)def
colorSpace
()def
colorTable
()def
constBits
()def
constScanLine
(arg__1)def
convertTo
(f[, flags=Qt.AutoColor])def
convertToColorSpace
(arg__1)def
convertToFormat
(f, colorTable[, flags=Qt.AutoColor])def
convertToFormat
(f[, flags=Qt.AutoColor])def
convertToFormat_helper
(format, flags)def
convertToFormat_inplace
(format, flags)def
convertedToColorSpace
(arg__1)def
copy
([rect=QRect()])def
copy
(x, y, w, h)def
createAlphaMask
([flags=Qt.AutoColor])def
createHeuristicMask
([clipTight=true])def
createMaskFromColor
(color[, mode=Qt.MaskInColor])def
dotsPerMeterX
()def
dotsPerMeterY
()def
fill
(color)def
fill
(color)def
fill
(pixel)def
format
()def
hasAlphaChannel
()def
invertPixels
([mode=InvertRgb])def
isGrayscale
()def
isNull
()def
load
(device, format)def
load
(fileName[, format=None])def
loadFromData
(data[, aformat=None])def
mirrored
([horizontally=false[, vertically=true]])def
mirrored_helper
(horizontal, vertical)def
mirrored_inplace
(horizontal, vertical)def
offset
()def
pixel
(pt)def
pixel
(x, y)def
pixelColor
(pt)def
pixelColor
(x, y)def
pixelFormat
()def
pixelIndex
(pt)def
pixelIndex
(x, y)def
rect
()def
reinterpretAsFormat
(f)def
rgbSwapped
()def
rgbSwapped_helper
()def
rgbSwapped_inplace
()def
save
(device[, format=None[, quality=-1]])def
save
(fileName[, format=None[, quality=-1]])def
scaled
(s[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])def
scaled
(w, h[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])def
scaledToHeight
(h[, mode=Qt.FastTransformation])def
scaledToWidth
(w[, mode=Qt.FastTransformation])def
setAlphaChannel
(alphaChannel)def
setColor
(i, c)def
setColorCount
(arg__1)def
setColorSpace
(arg__1)def
setColorTable
(colors)def
setDevicePixelRatio
(scaleFactor)def
setDotsPerMeterX
(arg__1)def
setDotsPerMeterY
(arg__1)def
setOffset
(arg__1)def
setPixel
(pt, index_or_rgb)def
setPixel
(x, y, index_or_rgb)def
setPixelColor
(pt, c)def
setPixelColor
(x, y, c)def
setText
(key, value)def
size
()def
sizeInBytes
()def
smoothScaled
(w, h)def
swap
(other)def
text
([key=””])def
textKeys
()def
transformed
(matrix[, mode=Qt.FastTransformation])def
transformed
(matrix[, mode=Qt.FastTransformation])def
valid
(pt)def
valid
(x, y)
Static functions¶
def
fromData
(data[, format=None])def
toImageFormat
(format)def
toPixelFormat
(format)def
trueMatrix
(arg__1, w, h)def
trueMatrix
(arg__1, w, h)
Detailed Description¶
Qt provides four classes for handling image data:
QImage
,QPixmap
,QBitmap
andQPicture
.QImage
is designed and optimized for I/O, and for direct pixel access and manipulation, whileQPixmap
is designed and optimized for showing images on screen.QBitmap
is only a convenience class that inheritsQPixmap
, ensuring a depth of 1. Finally, theQPicture
class is a paint device that records and replaysQPainter
commands.Because
QImage
is aQPaintDevice
subclass,QPainter
can be used to draw directly onto images. When usingQPainter
on aQImage
, the painting can be performed in another thread than the current GUI thread.The
QImage
class supports several image formats described by theFormat
enum. These include monochrome, 8-bit, 32-bit and alpha-blended images which are available in all versions of Qt 4.x.
QImage
provides a collection of functions that can be used to obtain a variety of information about the image. There are also several functions that enables transformation of the image.
QImage
objects can be passed around by value since theQImage
class uses implicit data sharing .QImage
objects can also be streamed and compared.Note
If you would like to load
QImage
objects in a static build of Qt, refer to the Plugin HowTo.Warning
Painting on a
QImage
with the formatFormat_Indexed8
is not supported.
Reading and Writing Image Files¶
QImage
provides several ways of loading an image file: The file can be loaded when constructing theQImage
object, or by using theload()
orloadFromData()
functions later on.QImage
also provides the staticfromData()
function, constructing aQImage
from the given data. When loading an image, the file name can either refer to an actual file on disk or to one of the application’s embedded resources. See The Qt Resource System overview for details on how to embed images and other resource files in the application’s executable.Simply call the
save()
function to save aQImage
object.The complete list of supported file formats are available through the
supportedImageFormats()
andsupportedImageFormats()
functions. New file formats can be added as plugins. By default, Qt supports the following formats:
Format
Description
Qt’s support
BMP
Windows Bitmap
Read/write
GIF
Graphic Interchange Format (optional)
Read
JPG
Joint Photographic Experts Group
Read/write
JPEG
Joint Photographic Experts Group
Read/write
PNG
Portable Network Graphics
Read/write
PBM
Portable Bitmap
Read
PGM
Portable Graymap
Read
PPM
Portable Pixmap
Read/write
XBM
X11 Bitmap
Read/write
XPM
X11 Pixmap
Read/write
Image Information¶
QImage
provides a collection of functions that can be used to obtain a variety of information about the image:
Available Functions
Geometry
The
size()
,width()
,height()
,dotsPerMeterX()
, anddotsPerMeterY()
functions provide information about the image size and aspect ratio.The
rect()
function returns the image’s enclosing rectangle. Thevalid()
function tells if a given pair of coordinates is within this rectangle. Theoffset()
function returns the number of pixels by which the image is intended to be offset by when positioned relative to other images, which also can be manipulated using thesetOffset()
function.Colors
The color of a pixel can be retrieved by passing its coordinates to the
pixel()
function. Thepixel()
function returns the color as aQRgb
value indepedent of the image’s format.In case of monochrome and 8-bit images, the
colorCount()
andcolorTable()
functions provide information about the color components used to store the image data: ThecolorTable()
function returns the image’s entire color table. To obtain a single entry, use thepixelIndex()
function to retrieve the pixel index for a given pair of coordinates, then use thecolor()
function to retrieve the color. Note that if you create an 8-bit image manually, you have to set a valid color table on the image as well.The
hasAlphaChannel()
function tells if the image’s format respects the alpha channel, or not. TheallGray()
andisGrayscale()
functions tell whether an image’s colors are all shades of gray.See also the
Pixel Manipulation
andImage Transformations
sections.Text
The
text()
function returns the image text associated with the given text key. An image’s text keys can be retrieved using thetextKeys()
function. Use thesetText()
function to alter an image’s text.Low-level information
The
depth()
function returns the depth of the image. The supported depths are 1 (monochrome), 8, 16, 24 and 32 bits. ThebitPlaneCount()
function tells how many of those bits that are used. For more information see theImage Formats
section.The
format()
,bytesPerLine()
, andsizeInBytes()
functions provide low-level information about the data stored in the image.The
cacheKey()
function returns a number that uniquely identifies the contents of thisQImage
object.
Pixel Manipulation¶
The functions used to manipulate an image’s pixels depend on the image format. The reason is that monochrome and 8-bit images are index-based and use a color lookup table, while 32-bit images store ARGB values directly. For more information on image formats, see the
Image Formats
section.In case of a 32-bit image, the
setPixel()
function can be used to alter the color of the pixel at the given coordinates to any other color specified as an ARGB quadruplet. To make a suitableQRgb
value, use theqRgb()
(adding a default alpha component to the given RGB values, i.e. creating an opaque color) orqRgba()
function. For example:
32-bit
image = QImage(3, 3, QImage.Format_RGB32) value = qRgb(189, 149, 39) # 0xffbd9527 image.setPixel(1, 1, value) value = qRgb(122, 163, 39) # 0xff7aa327 image.setPixel(0, 1, value) image.setPixel(1, 0, value) value = qRgb(237, 187, 51) # 0xffedba31 image.setPixel(2, 1, value)In case of a 8-bit and monchrome images, the pixel value is only an index from the image’s color table. So the
setPixel()
function can only be used to alter the color of the pixel at the given coordinates to a predefined color from the image’s color table, i.e. it can only change the pixel’s index value. To alter or add a color to an image’s color table, use thesetColor()
function.An entry in the color table is an ARGB quadruplet encoded as an
QRgb
value. Use theqRgb()
andqRgba()
functions to make a suitableQRgb
value for use with thesetColor()
function. For example:
8-bit
image = QImage(3, 3, QImage.Format_Indexed8) value = qRgb(122, 163, 39) # 0xff7aa327 image.setColor(0, value) value = qRgb(237, 187, 51) # 0xffedba31 image.setColor(1, value) value = qRgb(189, 149, 39) # 0xffbd9527 image.setColor(2, value) image.setPixel(0, 1, 0) image.setPixel(1, 0, 0) image.setPixel(1, 1, 2) image.setPixel(2, 1, 1)For images with more than 8-bit per color-channel. The methods
setPixelColor()
andpixelColor()
can be used to set and get withQColor
values.
QImage
also provide thescanLine()
function which returns a pointer to the pixel data at the scanline with the given index, and thebits()
function which returns a pointer to the first pixel data (this is equivalent toscanLine(0)
).
Image Formats¶
Each pixel stored in a
QImage
is represented by an integer. The size of the integer varies depending on the format.QImage
supports several image formats described by theFormat
enum.Monochrome images are stored using 1-bit indexes into a color table with at most two colors. There are two different types of monochrome images: big endian (MSB first) or little endian (LSB first) bit order.
8-bit images are stored using 8-bit indexes into a color table, i.e. they have a single byte per pixel. The color table is a
QVector
<QRgb
>, and theQRgb
typedef is equivalent to an unsigned int containing an ARGB quadruplet on the format 0xAARRGGBB.32-bit images have no color table; instead, each pixel contains an
QRgb
value. There are three different types of 32-bit images storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB values respectively. In the premultiplied format the red, green, and blue channels are multiplied by the alpha component divided by 255.An image’s format can be retrieved using the
format()
function. Use theconvertToFormat()
functions to convert an image into another format. TheallGray()
andisGrayscale()
functions tell whether a color image can safely be converted to a grayscale image.
Image Transformations¶
QImage
supports a number of functions for creating a new image that is a transformed version of the original: ThecreateAlphaMask()
function builds and returns a 1-bpp mask from the alpha buffer in this image, and thecreateHeuristicMask()
function creates and returns a 1-bpp heuristic mask for this image. The latter function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges.The
mirrored()
function returns a mirror of the image in the desired direction, thescaled()
returns a copy of the image scaled to a rectangle of the desired measures, and thergbSwapped()
function constructs a BGR image from a RGB image.The
scaledToWidth()
andscaledToHeight()
functions return scaled copies of the image.The
transformed()
function returns a copy of the image that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e.transformed()
returns the smallest image containing all transformed points of the original image. The statictrueMatrix()
function returns the actual matrix used for transforming the image.There are also functions for changing attributes of an image in-place:
Function
Description
Defines the aspect ratio by setting the number of pixels that fit horizontally in a physical meter.
Defines the aspect ratio by setting the number of pixels that fit vertically in a physical meter.
Fills the entire image with the given pixel value.
Inverts all pixel values in the image using the given
InvertMode
value.Sets the color table used to translate color indexes. Only monochrome and 8-bit formats.
Resizes the color table. Only monochrome and 8-bit formats.
See also
QImageReader
QImageWriter
QPixmap
QSvgRenderer
Image Composition Example Image Viewer Example Scribble Example Pixelator Example
- class PySide2.QtGui.QImage¶
PySide2.QtGui.QImage(arg__1, arg__2, arg__3, arg__4)
PySide2.QtGui.QImage(arg__1, arg__2, arg__3, arg__4, arg__5)
PySide2.QtGui.QImage(arg__1)
PySide2.QtGui.QImage(size, format)
PySide2.QtGui.QImage(fileName[, format=None])
PySide2.QtGui.QImage(xpm)
PySide2.QtGui.QImage(width, height, format)
PySide2.QtGui.QImage(data, width, height, format[, cleanupFunction=None[, cleanupInfo=None]])
PySide2.QtGui.QImage(data, width, height, bytesPerLine, format[, cleanupFunction=None[, cleanupInfo=None]])
- param cleanupInfo:
void
- param xpm:
char[]
- param cleanupFunction:
QImageCleanupFunction
- param format:
- param bytesPerLine:
int
- param size:
- param arg__1:
str
- param arg__2:
int
- param arg__3:
int
- param fileName:
str
- param arg__4:
- param arg__5:
- param data:
str
- param width:
int
- param height:
int
Constructs a null image.
See also
Constructs an image with the given
width
,height
andformat
.A
null
image will be returned if memory cannot be allocated.Warning
This will create a
QImage
with uninitialized data. Callfill()
to fill the image with an appropriate pixel value before drawing onto it withQPainter
.Constructs an image with the given
width
,height
andformat
, that uses an existing memory buffer,data
. Thewidth
andheight
must be specified in pixels,data
must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.The buffer must remain valid throughout the life of the
QImage
and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointercleanupFunction
along with an extra pointercleanupInfo
that will be called when the last copy is destroyed.If
format
is an indexed color format, the image color table is initially empty and must be sufficiently expanded withsetColorCount()
orsetColorTable()
before the image is used.Constructs an image with the given
width
,height
andformat
, that uses an existing memory buffer,data
. Thewidth
andheight
must be specified in pixels.bytesPerLine
specifies the number of bytes per line (stride).The buffer must remain valid throughout the life of the
QImage
and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointercleanupFunction
along with an extra pointercleanupInfo
that will be called when the last copy is destroyed.If
format
is an indexed color format, the image color table is initially empty and must be sufficiently expanded withsetColorCount()
orsetColorTable()
before the image is used.
- PySide2.QtGui.QImage.InvertMode¶
This enum type is used to describe how pixel values should be inverted in the
invertPixels()
function.Constant
Description
QImage.InvertRgb
Invert only the RGB values and leave the alpha channel unchanged.
QImage.InvertRgba
Invert all channels, including the alpha channel.
See also
- PySide2.QtGui.QImage.Format¶
The following image formats are available in Qt. See the notes after the table.
Constant
Description
QImage.Format_Invalid
The image is invalid.
QImage.Format_Mono
The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first.
QImage.Format_MonoLSB
The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first.
QImage.Format_Indexed8
The image is stored using 8-bit indexes into a colormap.
QImage.Format_RGB32
The image is stored using a 32-bit RGB format (0xffRRGGBB).
QImage.Format_ARGB32
The image is stored using a 32-bit ARGB format (0xAARRGGBB).
QImage.Format_ARGB32_Premultiplied
The image is stored using a premultiplied 32-bit ARGB format (0xAARRGGBB), i.e. the red, green, and blue channels are multiplied by the alpha component divided by 255. (If RR, GG, or BB has a higher value than the alpha channel, the results are undefined.) Certain operations (such as image composition using alpha blending) are faster using premultiplied ARGB32 than with plain ARGB32.
QImage.Format_RGB16
The image is stored using a 16-bit RGB format (5-6-5).
QImage.Format_ARGB8565_Premultiplied
The image is stored using a premultiplied 24-bit ARGB format (8-5-6-5).
QImage.Format_RGB666
The image is stored using a 24-bit RGB format (6-6-6). The unused most significant bits is always zero.
QImage.Format_ARGB6666_Premultiplied
The image is stored using a premultiplied 24-bit ARGB format (6-6-6-6).
QImage.Format_RGB555
The image is stored using a 16-bit RGB format (5-5-5). The unused most significant bit is always zero.
QImage.Format_ARGB8555_Premultiplied
The image is stored using a premultiplied 24-bit ARGB format (8-5-5-5).
QImage.Format_RGB888
The image is stored using a 24-bit RGB format (8-8-8).
QImage.Format_RGB444
The image is stored using a 16-bit RGB format (4-4-4). The unused bits are always zero.
QImage.Format_ARGB4444_Premultiplied
The image is stored using a premultiplied 16-bit ARGB format (4-4-4-4).
QImage.Format_RGBX8888
The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). This is the same as the except alpha must always be 255. (added in Qt 5.2)
QImage.Format_RGBA8888
The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). Unlike ARGB32 this is a byte-ordered format, which means the 32bit encoding differs between big endian and little endian architectures, being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2)
QImage.Format_RGBA8888_Premultiplied
The image is stored using a premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). (added in Qt 5.2)
QImage.Format_BGR30
The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4)
QImage.Format_A2BGR30_Premultiplied
The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). (added in Qt 5.4)
QImage.Format_RGB30
The image is stored using a 32-bit RGB format (x-10-10-10). (added in Qt 5.4)
QImage.Format_A2RGB30_Premultiplied
The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). (added in Qt 5.4)
QImage.Format_Alpha8
The image is stored using an 8-bit alpha only format. (added in Qt 5.5)
QImage.Format_Grayscale8
The image is stored using an 8-bit grayscale format. (added in Qt 5.5)
QImage.Format_Grayscale16
The image is stored using an 16-bit grayscale format. (added in Qt 5.13)
QImage.Format_RGBX64
The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). This is the same as the except alpha must always be 65535. (added in Qt 5.12)
QImage.Format_RGBA64
The image is stored using a 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12)
QImage.Format_RGBA64_Premultiplied
The image is stored using a premultiplied 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12)
QImage.Format_BGR888
The image is stored using a 24-bit BGR format. (added in Qt 5.14)
Note
Drawing into a
QImage
with is not supported.Note
Avoid most rendering directly to most of these formats using
QPainter
. Rendering is best optimized to theFormat_RGB32
andFormat_ARGB32_Premultiplied
formats, and secondarily for rendering to theFormat_RGB16
,Format_RGBX8888
,Format_RGBA8888_Premultiplied
,Format_RGBX64
andFormat_RGBA64_Premultiplied
formatsSee also
- PySide2.QtGui.QImage.allGray()¶
- Return type:
bool
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.Note that this function is slow for images without color table.
See also
- PySide2.QtGui.QImage.alphaChannel()¶
- Return type:
Note
This function is deprecated.
Returns the alpha channel of the image as a new grayscale
QImage
in which each pixel’s red, green, and blue values are given the alpha value of the original image. The color depth of the returned image is 8-bit.You can see an example of use of this function in
QPixmap
‘salphaChannel()
, which works in the same way as this function on QPixmaps.Most usecases for this function can be replaced with
QPainter
and using composition modes.Note this returns a color-indexed image if you want the alpha channel in the alpha8 format instead use
convertToFormat
(Format_Alpha8
) on the source image.Warning
This is an expensive function.
See also
setAlphaChannel()
hasAlphaChannel()
convertToFormat()
Pixmap
Image Transformations
- PySide2.QtGui.QImage.bitPlaneCount()¶
- Return type:
int
Returns the number of bit planes in the image.
The number of bit planes is the number of bits of color and transparency information for each pixel. This is different from (i.e. smaller than) the depth when the image format contains unused bits.
See also
depth()
format()
Image Formats
- PySide2.QtGui.QImage.byteCount()¶
- Return type:
int
Note
This function is deprecated.
Returns the number of bytes occupied by the image data.
Note this method should never be called on an image larger than 2 gigabytes. Instead use
sizeInBytes()
.See also
sizeInBytes()
bytesPerLine()
bits()
Image Information
- PySide2.QtGui.QImage.bytesPerLine()¶
- Return type:
int
Returns the number of bytes per image scanline.
This is equivalent to
sizeInBytes()
/height()
ifheight()
is non-zero.See also
scanLine()
- PySide2.QtGui.QImage.cacheKey()¶
- Return type:
int
Returns a number that identifies the contents of this
QImage
object. DistinctQImage
objects can only have the same key if they refer to the same contents.The key will change when the image is altered.
- PySide2.QtGui.QImage.color(i)¶
- Parameters:
i – int
- Return type:
int
Returns the color in the color table at index
i
. The first color is at index 0.The colors in an image’s color table are specified as ARGB quadruplets (
QRgb
). Use theqAlpha()
,qRed()
,qGreen()
, andqBlue()
functions to get the color value components.See also
setColor()
pixelIndex()
Pixel Manipulation
- PySide2.QtGui.QImage.colorSpace()¶
- Return type:
Returns the color space of the image if a color space is defined.
See also
- PySide2.QtGui.QImage.colorTable()¶
- Return type:
Returns a list of the colors contained in the image’s color table, or an empty list if the image does not have a color table
See also
setColorTable()
colorCount()
color()
- PySide2.QtGui.QImage.constBits()¶
- Return type:
str
Returns a pointer to the first pixel data.
Note that
QImage
uses implicit data sharing , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.See also
bits()
constScanLine()
- PySide2.QtGui.QImage.constScanLine(arg__1)¶
- Parameters:
arg__1 – int
- Return type:
PyObject
Returns a pointer to the pixel data at the scanline with index
i
. The first scanline is at index 0.The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).
Note that
QImage
uses implicit data sharing , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.See also
scanLine()
constBits()
- PySide2.QtGui.QImage.convertTo(f[, flags=Qt.AutoColor])¶
- Parameters:
f –
Format
flags –
ImageConversionFlags
Detach and convert the image to the given
format
in place.The specified image conversion
flags
control how the image data is handled during the conversion process.See also
- PySide2.QtGui.QImage.convertToColorSpace(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtGui.QColorSpace
Converts the image to
colorSpace
.If the image has no valid color space, the method does nothing.
See also
- PySide2.QtGui.QImage.convertToFormat(f[, flags=Qt.AutoColor])¶
- Parameters:
f –
Format
flags –
ImageConversionFlags
- Return type:
- PySide2.QtGui.QImage.convertToFormat(f, colorTable[, flags=Qt.AutoColor])
- Parameters:
f –
Format
colorTable –
flags –
ImageConversionFlags
- Return type:
This is an overloaded function.
Returns a copy of the image converted to the given
format
, using the specifiedcolorTable
.Conversion from RGB formats to indexed formats is a slow operation and will use a straightforward nearest color approach, with no dithering.
- PySide2.QtGui.QImage.convertToFormat_helper(format, flags)¶
- Parameters:
format –
Format
flags –
ImageConversionFlags
- Return type:
- PySide2.QtGui.QImage.convertToFormat_inplace(format, flags)¶
- Parameters:
format –
Format
flags –
ImageConversionFlags
- Return type:
bool
- PySide2.QtGui.QImage.convertedToColorSpace(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtGui.QColorSpace
- Return type:
Returns the image converted to
colorSpace
.If the image has no valid color space, a null
QImage
is returned.See also
- PySide2.QtGui.QImage.copy([rect=QRect()])¶
- Parameters:
rect –
PySide2.QtCore.QRect
- Return type:
Returns a sub-area of the image as a new image.
The returned image is copied from the position (
rectangle
.x(),rectangle
.y()) in this image, and will always have the size of the givenrectangle
.In areas beyond this image, pixels are set to 0. For 32-bit RGB images, this means black; for 32-bit ARGB images, this means transparent black; for 8-bit images, this means the color with index 0 in the color table which can be anything; for 1-bit images, this means
color0
.If the given
rectangle
is a null rectangle the entire image is copied.See also
QImage()
- PySide2.QtGui.QImage.copy(x, y, w, h)
- Parameters:
x – int
y – int
w – int
h – int
- Return type:
This is an overloaded function.
The returned image is copied from the position (
x
,y
) in this image, and will always have the givenwidth
andheight
. In areas beyond this image, pixels are set to 0.
- PySide2.QtGui.QImage.createAlphaMask([flags=Qt.AutoColor])¶
- Parameters:
flags –
ImageConversionFlags
- Return type:
Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if the image’s format is
Format_RGB32
.The
flags
argument is a bitwise-OR of theImageConversionFlags
, and controls the conversion process. Passing 0 for flags sets all the default options.The returned image has little-endian bit order (i.e. the image’s format is
Format_MonoLSB
), which you can convert to big-endian (Format_Mono
) using theconvertToFormat()
function.See also
createHeuristicMask()
Image Transformations
- PySide2.QtGui.QImage.createHeuristicMask([clipTight=true])¶
- Parameters:
clipTight – bool
- Return type:
Creates and returns a 1-bpp heuristic mask for this image.
The function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges. The four corners vote for which color is to be masked away. In case of a draw (this generally means that this function is not applicable to the image), the result is arbitrary.
The returned image has little-endian bit order (i.e. the image’s format is
Format_MonoLSB
), which you can convert to big-endian (Format_Mono
) using theconvertToFormat()
function.If
clipTight
is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.Note that this function disregards the alpha buffer.
See also
createAlphaMask()
Image Transformations
- PySide2.QtGui.QImage.createMaskFromColor(color[, mode=Qt.MaskInColor])¶
- Parameters:
color – int
mode –
MaskMode
- Return type:
Creates and returns a mask for this image based on the given
color
value. If themode
is MaskInColor (the default value), all pixels matchingcolor
will be opaque pixels in the mask. Ifmode
is MaskOutColor, all pixels matching the given color will be transparent.See also
- PySide2.QtGui.QImage.dotsPerMeterX()¶
- Return type:
int
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.See also
setDotsPerMeterX()
Image Information
- PySide2.QtGui.QImage.dotsPerMeterY()¶
- Return type:
int
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.See also
setDotsPerMeterY()
Image Information
- PySide2.QtGui.QImage.fill(color)¶
- Parameters:
color –
GlobalColor
This is an overloaded function.
Fills the image with the given
color
, described as a standard global color.
- PySide2.QtGui.QImage.fill(color)
- Parameters:
color –
PySide2.QtGui.QColor
- PySide2.QtGui.QImage.fill(pixel)
- Parameters:
pixel –
uint
Fills the entire image with the given
pixelValue
.If the depth of this image is 1, only the lowest bit is used. If you say fill(0), fill(2), etc., the image is filled with 0s. If you say fill(1), fill(3), etc., the image is filled with 1s. If the depth is 8, the lowest 8 bits are used and if the depth is 16 the lowest 16 bits are used.
Note:
pixel()
returns the color of the pixel at the given coordinates while QColor::pixel() returns the pixel value of the underlying window system (essentially an index value), so normally you will want to usepixel()
to use a color from an existing image orrgb()
to use a specific color.See also
depth()
Image Transformations
- PySide2.QtGui.QImage.format()¶
- Return type:
Returns the format of the image.
See also
Image Formats
- static PySide2.QtGui.QImage.fromData(data[, format=None])¶
- Parameters:
data –
PySide2.QtCore.QByteArray
format – str
- Return type:
This is an overloaded function.
Loads an image from the given
QByteArray
data
.
- PySide2.QtGui.QImage.hasAlphaChannel()¶
- Return type:
bool
Returns
true
if the image has a format that respects the alpha channel, otherwise returnsfalse
.See also
Image Information
- PySide2.QtGui.QImage.invertPixels([mode=InvertRgb])¶
- Parameters:
mode –
InvertMode
Inverts all pixel values in the image.
The given invert
mode
only have a meaning when the image’s depth is 32. The defaultmode
isInvertRgb
, which leaves the alpha channel unchanged. If themode
isInvertRgba
, the alpha bits are also inverted.Inverting an 8-bit image means to replace all pixels using color index i with a pixel using color index 255 minus i . The same is the case for a 1-bit image. Note that the color table is not changed.
If the image has a premultiplied alpha channel, the image is first converted to an unpremultiplied image format to be inverted and then converted back.
See also
Image Transformations
- PySide2.QtGui.QImage.isGrayscale()¶
- Return type:
bool
For 32-bit images, this function is equivalent to
allGray()
.For color indexed images, this function returns
true
if color(i) isQRgb
(i, i, i) for all indexes of the color table; otherwise returnsfalse
.See also
allGray()
Image Formats
- PySide2.QtGui.QImage.isNull()¶
- Return type:
bool
Returns
true
if it is a null image, otherwise returnsfalse
.A null image has all parameters set to zero and no allocated data.
- PySide2.QtGui.QImage.load(device, format)¶
- Parameters:
device –
PySide2.QtCore.QIODevice
format – str
- Return type:
bool
This is an overloaded function.
This function reads a
QImage
from the givendevice
. This can, for example, be used to load an image directly into aQByteArray
.
- PySide2.QtGui.QImage.load(fileName[, format=None])
- Parameters:
fileName – str
format – str
- Return type:
bool
- PySide2.QtGui.QImage.loadFromData(data[, aformat=None])¶
- Parameters:
data –
PySide2.QtCore.QByteArray
aformat – str
- Return type:
bool
This is an overloaded function.
Loads an image from the given
QByteArray
data
.
- PySide2.QtGui.QImage.mirrored([horizontally=false[, vertically=true]])¶
- Parameters:
horizontally – bool
vertically – bool
- Return type:
- PySide2.QtGui.QImage.mirrored_helper(horizontal, vertical)¶
- Parameters:
horizontal – bool
vertical – bool
- Return type:
- PySide2.QtGui.QImage.mirrored_inplace(horizontal, vertical)¶
- Parameters:
horizontal – bool
vertical – bool
- PySide2.QtGui.QImage.offset()¶
- Return type:
Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.
See also
setOffset()
Image Information
- PySide2.QtGui.QImage.__ne__(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtGui.QImage
- Return type:
bool
Returns
true
if this image and the givenimage
have different contents; otherwise returnsfalse
.The comparison can be slow, unless there is some obvious difference, such as different widths, in which case the function will return quickly.
See also
operator=()
- PySide2.QtGui.QImage.__eq__(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtGui.QImage
- Return type:
bool
Returns
true
if this image and the givenimage
have the same contents; otherwise returnsfalse
.The comparison can be slow, unless there is some obvious difference (e.g. different size or format), in which case the function will return quickly.
See also
operator=()
- PySide2.QtGui.QImage.pixel(pt)¶
- Parameters:
- Return type:
int
Returns the color of the pixel at the given
position
.If the
position
is not valid, the results are undefined.Warning
This function is expensive when used for massive pixel manipulations. Use
constBits()
orconstScanLine()
when many pixels needs to be read.See also
setPixel()
valid()
constBits()
constScanLine()
Pixel Manipulation
- PySide2.QtGui.QImage.pixel(x, y)
- Parameters:
x – int
y – int
- Return type:
int
This is an overloaded function.
Returns the color of the pixel at coordinates (
x
,y
).
- PySide2.QtGui.QImage.pixelColor(pt)¶
- Parameters:
- Return type:
Returns the color of the pixel at the given
position
as aQColor
.If the
position
is not valid, an invalidQColor
is returned.Warning
This function is expensive when used for massive pixel manipulations. Use
constBits()
orconstScanLine()
when many pixels needs to be read.See also
setPixelColor()
setPixel()
valid()
constBits()
constScanLine()
Pixel Manipulation
- PySide2.QtGui.QImage.pixelColor(x, y)
- Parameters:
x – int
y – int
- Return type:
This is an overloaded function.
Returns the color of the pixel at coordinates (
x
,y
) as aQColor
.
- PySide2.QtGui.QImage.pixelFormat()¶
- Return type:
Returns the
Format
as aQPixelFormat
- PySide2.QtGui.QImage.pixelIndex(pt)¶
- Parameters:
- Return type:
int
Returns the pixel index at the given
position
.If
position
is not valid, or if the image is not a paletted image (depth()
> 8), the results are undefined.See also
valid()
depth()
Pixel Manipulation
- PySide2.QtGui.QImage.pixelIndex(x, y)
- Parameters:
x – int
y – int
- Return type:
int
This is an overloaded function.
Returns the pixel index at (
x
,y
).
- PySide2.QtGui.QImage.rect()¶
- Return type:
Returns the enclosing rectangle (0, 0,
width()
,height()
) of the image.See also
Image Information
- PySide2.QtGui.QImage.reinterpretAsFormat(f)¶
- Parameters:
f –
Format
- Return type:
bool
Changes the format of the image to
format
without changing the data. Only works between formats of the same depth.Returns
true
if successful.This function can be used to change images with alpha-channels to their corresponding opaque formats if the data is known to be opaque-only, or to change the format of a given image buffer before overwriting it with new data.
Warning
The function does not check if the image data is valid in the new format and will still return
true
if the depths are compatible. Operations on an image with invalid data are undefined.Warning
If the image is not detached, this will cause the data to be copied.
See also
- PySide2.QtGui.QImage.rgbSwapped()¶
- Return type:
- PySide2.QtGui.QImage.rgbSwapped_helper()¶
- Return type:
- PySide2.QtGui.QImage.rgbSwapped_inplace()¶
- PySide2.QtGui.QImage.save(device[, format=None[, quality=-1]])¶
- Parameters:
device –
PySide2.QtCore.QIODevice
format – str
quality – int
- Return type:
bool
This is an overloaded function.
This function writes a
QImage
to the givendevice
.This can, for example, be used to save an image directly into a
QByteArray
:image = QImage() ba = QByteArray() buffer(ba) buffer.open(QIODevice.WriteOnly) image.save(buffer, "PNG") # writes image into ba in PNG format
- PySide2.QtGui.QImage.save(fileName[, format=None[, quality=-1]])
- Parameters:
fileName – str
format – str
quality – int
- Return type:
bool
- PySide2.QtGui.QImage.scaled(s[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])¶
- Parameters:
aspectMode –
AspectRatioMode
mode –
TransformationMode
- Return type:
Returns a copy of the image scaled to a rectangle defined by the given
size
according to the givenaspectRatioMode
andtransformMode
.If
aspectRatioMode
isIgnoreAspectRatio
, the image is scaled tosize
.If
aspectRatioMode
isKeepAspectRatio
, the image is scaled to a rectangle as large as possible insidesize
, preserving the aspect ratio.If
aspectRatioMode
isKeepAspectRatioByExpanding
, the image is scaled to a rectangle as small as possible outsidesize
, preserving the aspect ratio.
If the given
size
is empty, this function returns a null image.See also
isNull()
Image Transformations
- PySide2.QtGui.QImage.scaled(w, h[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])
- Parameters:
w – int
h – int
aspectMode –
AspectRatioMode
mode –
TransformationMode
- Return type:
This is an overloaded function.
Returns a copy of the image scaled to a rectangle with the given
width
andheight
according to the givenaspectRatioMode
andtransformMode
.If either the
width
or theheight
is zero or negative, this function returns a null image.
- PySide2.QtGui.QImage.scaledToHeight(h[, mode=Qt.FastTransformation])¶
- Parameters:
h – int
mode –
TransformationMode
- Return type:
Returns a scaled copy of the image. The returned image is scaled to the given
height
using the specified transformationmode
.This function automatically calculates the width of the image so that the ratio of the image is preserved.
If the given
height
is 0 or negative, a null image is returned.See also
Image Transformations
- PySide2.QtGui.QImage.scaledToWidth(w[, mode=Qt.FastTransformation])¶
- Parameters:
w – int
mode –
TransformationMode
- Return type:
Returns a scaled copy of the image. The returned image is scaled to the given
width
using the specified transformationmode
.This function automatically calculates the height of the image so that its aspect ratio is preserved.
If the given
width
is 0 or negative, a null image is returned.See also
Image Transformations
- PySide2.QtGui.QImage.setAlphaChannel(alphaChannel)¶
- Parameters:
alphaChannel –
PySide2.QtGui.QImage
Sets the alpha channel of this image to the given
alphaChannel
.If
alphaChannel
is an 8 bit alpha image, the alpha values are used directly. Otherwise,alphaChannel
is converted to 8 bit grayscale and the intensity of the pixel values is used.If the image already has an alpha channel, the existing alpha channel is multiplied with the new one. If the image doesn’t have an alpha channel it will be converted to a format that does.
The operation is similar to painting
alphaChannel
as an alpha image over this image usingQPainter::CompositionMode_DestinationIn
.See also
hasAlphaChannel()
alphaChannel()
Image Transformations
Image Formats
- PySide2.QtGui.QImage.setColor(i, c)¶
- Parameters:
i – int
c – int
Sets the color at the given
index
in the color table, to the given tocolorValue
. The color value is an ARGB quadruplet.If
index
is outside the current size of the color table, it is expanded withsetColorCount()
.See also
color()
colorCount()
setColorTable()
Pixel Manipulation
- PySide2.QtGui.QImage.setColorCount(arg__1)¶
- Parameters:
arg__1 – int
Resizes the color table to contain
colorCount
entries.If the color table is expanded, all the extra colors will be set to transparent (i.e
qRgba
(0, 0, 0, 0)).When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.
See also
colorCount()
colorTable()
setColor()
Image Transformations
- PySide2.QtGui.QImage.setColorSpace(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtGui.QColorSpace
Sets the image color space to
colorSpace
without performing any conversions on image data.See also
- PySide2.QtGui.QImage.setColorTable(colors)¶
- Parameters:
colors –
Sets the color table used to translate color indexes to
QRgb
values, to the specifiedcolors
.When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.
See also
colorTable()
setColor()
Image Transformations
- PySide2.QtGui.QImage.setDevicePixelRatio(scaleFactor)¶
- Parameters:
scaleFactor – float
Sets the device pixel ratio for the image. This is the ratio between image pixels and device-independent pixels.
The default
scaleFactor
is 1.0. Setting it to something else has two effects:QPainters that are opened on the image will be scaled. For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.
Code paths in Qt that calculate layout geometry based on the image size will take the ratio into account:
QSize
layoutSize = image.size()
/ image.devicePixelRatio()
The net effect of this is that the image is displayed as high-DPI image rather than a large image (seeDrawing High Resolution Versions of Pixmaps and Images
).See also
devicePixelRatio()
- PySide2.QtGui.QImage.setDotsPerMeterX(arg__1)¶
- Parameters:
arg__1 – int
Sets the number of pixels that fit horizontally in a physical meter, to
x
.Together with
dotsPerMeterY()
, this number defines the intended scale and aspect ratio of the image, and determines the scale at whichQPainter
will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.See also
dotsPerMeterX()
Image Information
- PySide2.QtGui.QImage.setDotsPerMeterY(arg__1)¶
- Parameters:
arg__1 – int
Sets the number of pixels that fit vertically in a physical meter, to
y
.Together with
dotsPerMeterX()
, this number defines the intended scale and aspect ratio of the image, and determines the scale at whichQPainter
will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.See also
dotsPerMeterY()
Image Information
- PySide2.QtGui.QImage.setOffset(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtCore.QPoint
Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to
offset
.See also
offset()
Image Information
- PySide2.QtGui.QImage.setPixel(pt, index_or_rgb)¶
- Parameters:
index_or_rgb –
uint
Sets the pixel index or color at the given
position
toindex_or_rgb
.If the image’s format is either monochrome or paletted, the given
index_or_rgb
value must be an index in the image’s color table, otherwise the parameter must be aQRgb
value.If
position
is not a valid coordinate pair in the image, or ifindex_or_rgb
>=colorCount()
in the case of monochrome and paletted images, the result is undefined.Warning
This function is expensive due to the call of the internal
detach()
function called within; if performance is a concern, we recommend the use ofscanLine()
orbits()
to access pixel data directly.See also
pixel()
Pixel Manipulation
- PySide2.QtGui.QImage.setPixel(x, y, index_or_rgb)
- Parameters:
x – int
y – int
index_or_rgb –
uint
This is an overloaded function.
Sets the pixel index or color at (
x
,y
) toindex_or_rgb
.
- PySide2.QtGui.QImage.setPixelColor(pt, c)¶
- Parameters:
Sets the color at the given
position
tocolor
.If
position
is not a valid coordinate pair in the image, or the image’s format is either monochrome or paletted, the result is undefined.Warning
This function is expensive due to the call of the internal
detach()
function called within; if performance is a concern, we recommend the use ofscanLine()
orbits()
to access pixel data directly.See also
pixelColor()
pixel()
bits()
scanLine()
Pixel Manipulation
- PySide2.QtGui.QImage.setPixelColor(x, y, c)
- Parameters:
x – int
y – int
This is an overloaded function.
Sets the pixel color at (
x
,y
) tocolor
.
- PySide2.QtGui.QImage.setText(key, value)¶
- Parameters:
key – str
value – str
Sets the image text to the given
text
and associate it with the givenkey
.If you just want to store a single text block (i.e., a “comment” or just a description), you can either pass an empty key, or use a generic key like “Description”.
The image text is embedded into the image data when you call
save()
orwrite()
.Not all image formats support embedded text. You can find out if a specific image or format supports embedding text by using
supportsOption()
. We give an example:writer = QImageWriter() writer.setFormat("png") if writer.supportsOption(QImageIOHandler.Description): print "Png supports embedded text"
You can use
supportedImageFormats()
to find out which image formats are available to you.See also
- PySide2.QtGui.QImage.size()¶
- Return type:
Returns the size of the image, i.e. its
width()
andheight()
.See also
Image Information
- PySide2.QtGui.QImage.sizeInBytes()¶
- Return type:
long long
Returns the image data size in bytes.
See also
byteCount()
bytesPerLine()
bits()
Image Information
- PySide2.QtGui.QImage.smoothScaled(w, h)¶
- Parameters:
w – int
h – int
- Return type:
Returns a smoothly scaled copy of the image. The returned image has a size of width
w
by heighth
pixels.
- PySide2.QtGui.QImage.swap(other)¶
- Parameters:
other –
PySide2.QtGui.QImage
Swaps image
other
with this image. This operation is very fast and never fails.
- PySide2.QtGui.QImage.text([key=""])¶
- Parameters:
key – str
- Return type:
str
- PySide2.QtGui.QImage.textKeys()¶
- Return type:
list of strings
Returns the text keys for this image.
You can use these keys with
text()
to list the image text for a certain key.See also
- static PySide2.QtGui.QImage.toImageFormat(format)¶
- Parameters:
format –
PySide2.QtGui.QPixelFormat
- Return type:
Converts
format
into aFormat
- static PySide2.QtGui.QImage.toPixelFormat(format)¶
- Parameters:
format –
Format
- Return type:
Converts
format
into aQPixelFormat
- PySide2.QtGui.QImage.transformed(matrix[, mode=Qt.FastTransformation])¶
- Parameters:
matrix –
PySide2.QtGui.QMatrix
mode –
TransformationMode
- Return type:
Note
This function is deprecated.
- PySide2.QtGui.QImage.transformed(matrix[, mode=Qt.FastTransformation])
- Parameters:
matrix –
PySide2.QtGui.QTransform
mode –
TransformationMode
- Return type:
- static PySide2.QtGui.QImage.trueMatrix(arg__1, w, h)¶
- Parameters:
arg__1 –
PySide2.QtGui.QMatrix
w – int
h – int
- Return type:
Note
This function is deprecated.
- static PySide2.QtGui.QImage.trueMatrix(arg__1, w, h)
- Parameters:
arg__1 –
PySide2.QtGui.QTransform
w – int
h – int
- Return type:
- PySide2.QtGui.QImage.valid(pt)¶
- Parameters:
- Return type:
bool
Returns
true
ifpos
is a valid coordinate pair within the image; otherwise returnsfalse
.See also
rect()
contains()
- PySide2.QtGui.QImage.valid(x, y)
- Parameters:
x – int
y – int
- Return type:
bool
This is an overloaded function.
Returns
true
ifQPoint
(x
,y
) is a valid coordinate pair within the image; otherwise returnsfalse
.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.