PySide6.QtGui.QPixmap¶
- class QPixmap¶
- The - QPixmapclass is an off-screen image representation that can be used as a paint device. More…- Inherited by: - QBitmap- Synopsis¶- Methods¶- def - __init__()
- def - cacheKey()
- def - copy()
- def - fill()
- def - hasAlpha()
- def - isNull()
- def - isQBitmap()
- def - load()
- def - loadFromData()
- def - mask()
- def - rect()
- def - save()
- def - scaled()
- def - scaledToHeight()
- def - scaledToWidth()
- def - scroll()
- def - setMask()
- def - size()
- def - swap()
- def - toImage()
- def - transformed()
 - Static functions¶- def - defaultDepth()
- def - fromImage()
- def - trueMatrix()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Qt provides four classes for handling image data: - QImage,- QPixmap,- QBitmapand- QPicture.- QImageis designed and optimized for I/O, and for direct pixel access and manipulation, while- QPixmapis designed and optimized for showing images on screen.- QBitmapis only a convenience class that inherits- QPixmap, ensuring a depth of 1. The- isQBitmap()function returns- trueif a- QPixmapobject is really a bitmap, otherwise returns- false. Finally, the- QPictureclass is a paint device that records and replays- QPaintercommands.- A - QPixmapcan easily be displayed on the screen using QLabel or one of QAbstractButton’s subclasses (such as QPushButton and QToolButton). QLabel has a pixmap property, whereas QAbstractButton has an icon property.- QPixmapobjects can be passed around by value since the- QPixmapclass uses implicit data sharing. For more information, see the Implicit Data Sharing documentation.- QPixmapobjects can also be streamed.- Note that the pixel data in a pixmap is internal and is managed by the underlying window system. Because - QPixmapis a- QPaintDevicesubclass,- QPaintercan be used to draw directly onto pixmaps. Pixels can only be accessed through- QPainterfunctions or by converting the- QPixmapto a- QImage. However, the- fill()function is available for initializing the entire pixmap with a given color.- There are functions to convert between - QImageand- QPixmap. Typically, the- QImageclass is used to load an image file, optionally manipulating the image data, before the- QImageobject is converted into a- QPixmapto be shown on screen. Alternatively, if no manipulation is desired, the image file can be loaded directly into a- QPixmap.- QPixmapprovides a collection of functions that can be used to obtain a variety of information about the pixmap. In addition, there are several functions that enables transformation of the pixmap.- Reading and Writing Image Files¶- QPixmapprovides several ways of reading an image file: The file can be loaded when constructing the- QPixmapobject, or by using the- load()or- loadFromData()functions later on. 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 a- QPixmapobject.- The complete list of supported file formats are available through the - supportedImageFormats()and- supportedImageFormats()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 - Pixmap Information¶- QPixmapprovides a collection of functions that can be used to obtain a variety of information about the pixmap:- Available Functions - Geometry - The - size(),- width()and- height()functions provide information about the pixmap’s size. The- rect()function returns the image’s enclosing rectangle.- Alpha component - The hasAlphaChannel() returns - trueif the pixmap has a format that respects the alpha channel, otherwise returns- false. The hasAlpha(),- setMask()and- mask()functions are legacy and should not be used. They are potentially very slow.- The - createHeuristicMask()function creates and returns a 1-bpp heuristic mask (i.e. a- QBitmap) for this pixmap. It works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. The- createMaskFromColor()function creates and returns a mask (i.e. a- QBitmap) for the pixmap based on a given color.- Low-level information - The - depth()function returns the depth of the pixmap. The defaultDepth() function returns the default depth, i.e. the depth used by the application on the given screen.- The - cacheKey()function returns a number that uniquely identifies the contents of the- QPixmapobject.- Pixmap Conversion¶- A - QPixmapobject can be converted into a- QImageusing the- toImage()function. Likewise, a- QImagecan be converted into a- QPixmapusing the- fromImage(). If this is too expensive an operation, you can use- fromImage()instead.- To convert a - QPixmapto and from HICON you can use the- toHICON()and- fromHICON()functions respectively (after converting the- QPixmapto a- QImage, as explained above).- Pixmap Transformations¶- QPixmapsupports a number of functions for creating a new pixmap that is a transformed version of the original:- The - scaled(),- scaledToWidth()and- scaledToHeight()functions return scaled copies of the pixmap, while the- copy()function creates a- QPixmapthat is a plain copy of the original one.- The transformed() function returns a copy of the pixmap 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 pixmap containing all transformed points of the original pixmap. The static - trueMatrix()function returns the actual matrix used for transforming the pixmap.- See also - __init__()¶
 - Constructs a null pixmap. - See also - __init__(image)
- Parameters:
- image – - QImage
 
 - __init__(pixmap)
- Parameters:
- pixmap – - QPixmap
 
 - Constructs a pixmap that is a copy of the given - pixmap.- See also - __init__(size)
- Parameters:
- size – - QSize
 
 - This is an overloaded function. - Constructs a pixmap of the given - size.- Warning - This will create a - QPixmapwith uninitialized data. Call- fill()to fill the pixmap with an appropriate color before drawing onto it with- QPainter.- __init__(xpm)
- Parameters:
- xpm – - char[]
 
 - __init__(w, h)
- Parameters:
- w – int 
- h – int 
 
 
 - Constructs a pixmap with the given - widthand- height. If either- widthor- heightis zero, a null pixmap is constructed.- Warning - This will create a - QPixmapwith uninitialized data. Call- fill()to fill the pixmap with an appropriate color before drawing onto it with- QPainter.- See also - __init__(fileName[, format=None[, flags=Qt.AutoColor]])
- Parameters:
- fileName – str 
- format – str 
- flags – Combination of - ImageConversionFlag
 
 
 - Constructs a pixmap from the file with the given - fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap.- The loader attempts to read the pixmap using the specified - format. If the- formatis not specified (which is the default), the loader probes the file for a header to guess the file format.- The file name can either refer to an actual file on disk or to one of the application’s embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application’s executable. - If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the - flagsto control the conversion.- The - fileName,- formatand- flagsparameters are passed on to- load(). This means that the data in- fileNameis not compiled into the binary. If- fileNamecontains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.- See also - Reading and Writing Image Files- cacheKey()¶
- Return type:
- int 
 
 - Returns a number that identifies this - QPixmap. Distinct- QPixmapobjects can only have the same cache key if they refer to the same contents.- The cacheKey() will change when the pixmap is altered. - convertFromImage(img[, flags=Qt.AutoColor])¶
- Parameters:
- img – - QImage
- flags – Combination of - ImageConversionFlag
 
- Return type:
- bool 
 
 - Replaces this pixmap’s data with the given - imageusing the specified- flagsto control the conversion. The- flagsargument is a bitwise-OR of the Qt::ImageConversionFlags. Passing 0 for- flagssets all the default options. Returns- trueif the result is that this pixmap is not null.- See also - Returns a deep copy of the subset of the pixmap that is specified by the given - rectangle. For more information on deep copies, see the Implicit Data Sharing documentation.- If the given - rectangleis empty, the whole image is copied.- See also - operator=()- QPixmap()- Pixmap Transformations- copy(x, y, width, height)
- Parameters:
- x – int 
- y – int 
- width – int 
- height – int 
 
- Return type:
 
 - This is an overloaded function. - Returns a deep copy of the subset of the pixmap that is specified by the rectangle QRect( - x,- y,- width,- height).- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Creates and returns a heuristic mask for this pixmap. - The function works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. If - clipTightis true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.- The mask may not be perfect but it should be reasonable, so you can do things such as the following: - myPixmap = QPixmap() myPixmap.setMask(myPixmap.createHeuristicMask()) - This function is slow because it involves converting to/from a - QImage, and non-trivial computations.- See also - createMaskFromColor(maskColor[, mode=Qt.MaskInColor])¶
 - Creates and returns a mask for this pixmap based on the given - maskColor. If the- modeis Qt::MaskInColor, all pixels matching the maskColor will be transparent. If- modeis Qt::MaskOutColor, all pixels matching the maskColor will be opaque.- This function is slow because it involves converting to/from a - QImage.- static defaultDepth()¶
- Return type:
- int 
 
 - Returns the size of the pixmap in device independent pixels. - This value should be used when using the pixmap size in user interface size calculations. - The return value is equivalent to pixmap. - size()/ pixmap.- devicePixelRatio().- Fills the pixmap with the given - color.- The effect of this function is undefined when the pixmap is being painted on. - See also - Pixmap Transformations- static fromImage(image[, flags=Qt.AutoColor])¶
- Parameters:
- image – - QImage
- flags – Combination of - ImageConversionFlag
 
- Return type:
 
 - Converts the given - imageto a pixmap using the specified- flagsto control the conversion. The- flagsargument is a bitwise-OR of the Qt::ImageConversionFlags. Passing 0 for- flagssets all the default options.- In case of monochrome and 8-bit images, the image is first converted to a 32-bit pixmap and then filled with the colors in the color table. If this is too expensive an operation, you can use - fromImage()instead.- See also - fromImageReader()- toImage()- Pixmap Conversion- static fromImageInPlace(image[, flags=Qt.AutoColor])¶
- Parameters:
- image – - QImage
- flags – Combination of - ImageConversionFlag
 
- Return type:
 
 - static fromImageReader(imageReader[, flags=Qt.AutoColor])¶
- Parameters:
- imageReader – - QImageReader
- flags – Combination of - ImageConversionFlag
 
- Return type:
 
 - Create a - QPixmapfrom an image read directly from an- imageReader. The- flagsargument is a bitwise-OR of the Qt::ImageConversionFlags. Passing 0 for- flagssets all the default options.- On some systems, reading an image directly to - QPixmapcan use less memory than reading a- QImageto convert it to- QPixmap.- See also - fromImage()- toImage()- Pixmap Conversion- hasAlpha()¶
- Return type:
- bool 
 
 - hasAlphaChannel()¶
- Return type:
- bool 
 
 - isNull()¶
- Return type:
- bool 
 
 - Returns - trueif this is a null pixmap; otherwise returns- false.- A null pixmap has zero width, zero height and no contents. You cannot draw in a null pixmap. - isQBitmap()¶
- Return type:
- bool 
 
 - Returns - trueif this is a- QBitmap; otherwise returns- false.- load(fileName[, format=None[, flags=Qt.AutoColor]])¶
- Parameters:
- fileName – str 
- format – str 
- flags – Combination of - ImageConversionFlag
 
- Return type:
- bool 
 
 - Loads a pixmap from the file with the given - fileName. Returns true if the pixmap was successfully loaded; otherwise invalidates the pixmap and returns- false.- The loader attempts to read the pixmap using the specified - format. If the- formatis not specified (which is the default), the loader probes the file for a header to guess the file format.- The file name can either refer to an actual file on disk or to one of the application’s embedded resources. See the Resource System overview for details on how to embed pixmaps and other resource files in the application’s executable. - If the data needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the - flagsto control the conversion.- Note that QPixmaps are automatically added to the - QPixmapCachewhen loaded from a file in main thread; the key used is internal and cannot be acquired.- See also - loadFromData()- Reading and Writing Image Files- loadFromData(data[, format=None[, flags=Qt.AutoColor]])¶
- Parameters:
- data – - QByteArray
- format – str 
- flags – Combination of - ImageConversionFlag
 
- Return type:
- bool 
 
 - This is an overloaded function. - Loads a pixmap from the binary - datausing the specified- formatand conversion- flags.- loadFromData(buf[, format=None[, flags=Qt.AutoColor]])
- Parameters:
- buf – str 
- format – str 
- flags – Combination of - ImageConversionFlag
 
- Return type:
- bool 
 
 - Loads a pixmap from the - lenfirst bytes of the given binary- data. Returns- trueif the pixmap was loaded successfully; otherwise invalidates the pixmap and returns- false.- The loader attempts to read the pixmap using the specified - format. If the- formatis not specified (which is the default), the loader probes the file for a header to guess the file format.- If the data needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the - flagsto control the conversion.- See also - load()- Reading and Writing Image Files- This method must be used with an QPixmap object, not the class: - # Wrong pixmap = QPixmap.loadFromData(...) # Right pixmap = QPixmap().loadFromData(...) - Extracts a bitmap mask from the pixmap’s alpha channel. - Warning - This is potentially an expensive operation. The mask of the pixmap is extracted dynamically from the pixeldata. - See also - setMask()- Pixmap Information- Returns the pixmap’s enclosing rectangle. - See also - Pixmap Information- save(device[, format=None[, quality=-1]])¶
- Parameters:
- device – - QIODevice
- format – str 
- quality – int 
 
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - This is an overloaded function. - This function writes a - QPixmapto the given- deviceusing the specified image file- formatand- qualityfactor. This can be used, for example, to save a pixmap directly into a QByteArray:- pixmap = QPixmap() bytes = QByteArray() buffer = QBuffer(bytes) buffer.open(QIODevice.WriteOnly) pixmap.save(buffer, "PNG") # writes pixmap into bytes in PNG format - save(fileName[, format=None[, quality=-1]])
- Parameters:
- fileName – str 
- format – str 
- quality – int 
 
- Return type:
- bool 
 
 - Saves the pixmap to the file with the given - fileNameusing the specified image file- formatand- qualityfactor. Returns- trueif successful; otherwise returns- false.- The - qualityfactor must be in the range [0,100] or -1. Specify 0 to obtain small compressed files, 100 for large uncompressed files, and -1 to use the default settings.- If - formatis- None, an image format will be chosen from- fileName's suffix.- See also - Reading and Writing Image Files- scaled(s[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])¶
- Parameters:
- s – - QSize
- aspectMode – - AspectRatioMode
- mode – - TransformationMode
 
- Return type:
 
 - Scales the pixmap to the given - size, using the aspect ratio and transformation modes specified by- aspectRatioModeand- transformMode.  - If - aspectRatioModeis Qt::IgnoreAspectRatio, the pixmap is scaled to- size.
- If - aspectRatioModeis Qt::KeepAspectRatio, the pixmap is scaled to a rectangle as large as possible inside- size, preserving the aspect ratio.
- If - aspectRatioModeis Qt::KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside- size, preserving the aspect ratio.
 - If the given - sizeis empty, this function returns a null pixmap.- In some cases it can be more beneficial to draw the pixmap to a painter with a scale set rather than scaling the pixmap. This is the case when the painter is for instance based on OpenGL or when the scale factor changes rapidly. - See also - isNull()- Pixmap Transformations- 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 pixmap scaled to a rectangle with the given - widthand- heightaccording to the given- aspectRatioModeand- transformMode.- If either the - widthor the- heightis zero or negative, this function returns a null pixmap.- 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 - heightusing the specified transformation- mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.- If - heightis 0 or negative, a null pixmap is returned.- See also - isNull()- Pixmap Transformations- 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 - widthusing the specified transformation- mode. The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.- If - widthis 0 or negative, a null pixmap is returned.- See also - isNull()- Pixmap Transformations- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Scrolls the area - rectof this pixmap by (- dx,- dy). The exposed region is left unchanged. You can optionally pass a pointer to an empty- QRegionto get the region that is- exposedby the scroll operation.- pixmap = QPixmap("background.png") exposed = QRegion() pixmap.scroll(10, 10, pixmap.rect(), exposed) - You cannot scroll while there is an active painter on the pixmap. - See also - scroll()- scroll(dx, dy, x, y, width, height[, exposed=None])
- Parameters:
- dx – int 
- dy – int 
- x – int 
- y – int 
- width – int 
- height – int 
- exposed – - QRegion
 
 
 - This convenience function is equivalent to calling - scroll(- dx,- dy, QRect(- x,- y,- width,- height),- exposed).- See also - scroll()- setDevicePixelRatio(scaleFactor)¶
- Parameters:
- scaleFactor – float 
 
 - Sets the device pixel ratio for the pixmap. This is the ratio between image pixels and device-independent pixels. - The default - scaleFactoris 1.0. Setting it to something else has two effects:- QPainters that are opened on the pixmap 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 pixmap size will take the ratio into account: QSize layoutSize = pixmap. - size()/ pixmap.- devicePixelRatio()The net effect of this is that the pixmap is displayed as high-DPI pixmap rather than a large pixmap (see- Drawing High Resolution Versions of Pixmaps and Images).- See also - devicePixelRatio()- deviceIndependentSize()- Sets a mask bitmap. - This function merges the - maskwith the pixmap’s alpha channel. A pixel value of 1 on the mask means the pixmap’s pixel is unchanged; a value of 0 means the pixel is transparent. The mask must have the same size as this pixmap.- Setting a null mask resets the mask, leaving the previously transparent pixels black. The effect of this function is undefined when the pixmap is being painted on. - Returns the size of the pixmap. - See also - width()- height()- Pixmap Information- Swaps this pixmap with - other. This operation is very fast and never fails.- Converts the pixmap to a - QImage. Returns a null image if the conversion fails.- If the pixmap has 1-bit depth, the returned image will also be 1 bit deep. Images with more bits will be returned in a format closely represents the underlying system. Usually this will be - Format_ARGB32_Premultipliedfor pixmaps with an alpha and- Format_RGB32or- Format_RGB16for pixmaps without alpha.- Note that for the moment, alpha masks on monochrome images are ignored. - See also - fromImage()- Image Formats- transformed(arg__1[, mode=Qt.FastTransformation])¶
- Parameters:
- arg__1 – - QTransform
- mode – - TransformationMode
 
- Return type:
 
 - static trueMatrix(m, w, h)¶
- Parameters:
- m – - QTransform
- w – int 
- h – int 
 
- Return type:
 
 - Returns the actual matrix used for transforming a pixmap with the given - width,- heightand- matrix.- When transforming a pixmap using the transformed() function, the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest pixmap containing all transformed points of the original pixmap. This function returns the modified matrix, which maps points correctly from the original pixmap into the new pixmap. - See also - Pixmap Transformations