PySide6.QtGui.QImageReader¶
- class QImageReader¶
- The - QImageReaderclass provides a format independent interface for reading images from files or other devices. More…- Synopsis¶- Methods¶- def - __init__()
- def - autoTransform()
- def - canRead()
- def - clipRect()
- def - device()
- def - error()
- def - errorString()
- def - fileName()
- def - format()
- def - imageCount()
- def - imageFormat()
- def - jumpToImage()
- def - loopCount()
- def - nextImageDelay()
- def - quality()
- def - read()
- def - scaledClipRect()
- def - scaledSize()
- def - setClipRect()
- def - setDevice()
- def - setFileName()
- def - setFormat()
- def - setQuality()
- def - setScaledSize()
- def - size()
- def - subType()
- def - supportsOption()
- def - text()
- def - textKeys()
- def - transformation()
 - Static functions¶- 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¶- The most common way to read images is through - QImageand- QPixmap‘s constructors, or by calling QImage::load() and- load().- QImageReaderis a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling- setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling- setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.- To read an image, you start by constructing a - QImageReaderobject. Pass either a file name or a device pointer, and the image format to- QImageReader‘s constructor. You can then set several options, such as the clip rect (by calling- setClipRect()) and scaled size (by calling- setScaledSize()).- canRead()returns the image if the- QImageReadercan read the image (i.e., the image format is supported and the device is open for reading). Call- read()to read the image.- If any error occurs when reading the image, - read()will return a null- QImage. You can then call- error()to find the type of error that occurred, or- errorString()to get a human readable description of what went wrong.- Note - QImageReaderassumes exclusive control over the file or device that is assigned. Any attempts to modify the assigned file or device during the lifetime of the- QImageReaderobject will yield undefined results.- Formats¶- Call - supportedImageFormats()for a list of formats that- QImageReadercan read.- QImageReadersupports all built-in image formats, in addition to any image format plugins that support reading. Call- supportedMimeTypes()to obtain a list of supported MIME types, which for example can be passed to QFileDialog::setMimeTypeFilters().- QImageReaderautodetects the image format by default, by looking at the provided (optional) format string, the file name suffix, and the data stream contents. You can enable or disable this feature, by calling- setAutoDetectImageFormat().- High Resolution Versions of Images¶- It is possible to provide high resolution versions of images should a scaling between device pixels and device independent pixels be in effect. - The high resolution version is marked by the suffix - @2xon the base name. The image read will have its device pixel ratio set to a value of 2.- This can be disabled by setting the environment variable - QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING.- See also - QImageWriter- QImageIOHandler- QImageIOPlugin- QColorSpace- devicePixelRatio()- devicePixelRatio()- QIcon- drawPixmap()- drawImage()- class ImageReaderError¶
- This enum describes the different types of errors that can occur when reading images with - QImageReader.- Constant - Description - QImageReader.FileNotFoundError - QImageReaderwas used with a file name, but not file was found with that name. This can also happen if the file name contained no extension, and the file with the correct extension is not supported by Qt.- QImageReader.DeviceError - QImageReaderencountered a device error when reading the image. You can consult your particular device for more details on what went wrong.- QImageReader.UnsupportedFormatError - Qt does not support the requested image format. - QImageReader.InvalidDataError - The image data was invalid, and - QImageReaderwas unable to read an image from it. The can happen if the image file is damaged.- QImageReader.UnknownError - An unknown error occurred. If you get this value after calling - read(), it is most likely caused by a bug in- QImageReader.
 - __init__()¶
 - Constructs an empty - QImageReaderobject. Before reading an image, call- setDevice()or- setFileName().- __init__(device[, format=QByteArray()])
- Parameters:
- device – - QIODevice
- format – - QByteArray
 
 
 - Constructs a - QImageReaderobject with the device- deviceand the image format- format.- __init__(fileName[, format=QByteArray()])
- Parameters:
- fileName – str 
- format – - QByteArray
 
 
 - Constructs a - QImageReaderobject with the file name- fileNameand the image format- format.- See also - static allocationLimit()¶
- Return type:
- int 
 
 - Returns the current allocation limit, in megabytes. - See also - autoDetectImageFormat()¶
- Return type:
- bool 
 
 - Returns - trueif image format autodetection is enabled on this image reader; otherwise returns- false. By default, autodetection is enabled.- See also - autoTransform()¶
- Return type:
- bool 
 
 - Returns - trueif the image handler will apply transformation metadata on- read().- See also - Returns the background color that’s used when reading an image. If the image format does not support setting the background color an invalid color is returned. - See also - canRead()¶
- Return type:
- bool 
 
 - Returns - trueif an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns- false.- canRead() is a lightweight function that only does a quick test to see if the image data is valid. - read()may still return false after canRead() returns- true, if the image data is corrupt.- Note - A QMimeDatabase lookup is normally a better approach than this function for identifying potentially non-image files or data. - For images that support animation, canRead() returns - falsewhen all frames have been read.- See also - Returns the clip rect (also known as the ROI, or Region Of Interest) of the image. If no clip rect has been set, an invalid QRect is returned. - See also - currentImageNumber()¶
- Return type:
- int 
 
 - For image formats that support animation, this function returns the sequence number of the current frame. If the image format doesn’t support animation, 0 is returned. - This function returns -1 if an error occurred. - For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned. - See also - decideFormatFromContent()¶
- Return type:
- bool 
 
 - Returns whether the image reader should decide which plugin to use only based on the contents of the datastream rather than on the file extension. - See also - Returns the device currently assigned to - QImageReader, or- Noneif no device has been assigned.- See also - error()¶
- Return type:
 
 - Returns the type of error that occurred last. - See also - errorString()¶
- Return type:
- str 
 
 - Returns a human readable description of the last error that occurred. - See also - fileName()¶
- Return type:
- str 
 
 - If the currently assigned device is a QFile, or if - setFileName()has been called, this function returns the name of the file- QImageReaderreads from. Otherwise (i.e., if no device has been assigned or the device is not a QFile), an empty QString is returned.- See also - format()¶
- Return type:
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns the format - QImageReaderuses for reading images.- You can call this function after assigning a device to the reader to determine the format of the device. For example: - reader = QImageReader("image.png") # reader.format() == "png" - If the reader cannot read any image from the device (e.g., there is no image there, or the image has already been read), or if the format is unsupported, this function returns an empty QByteArray(). - See also - imageCount()¶
- Return type:
- int 
 
 - For image formats that support animation, this function returns the total number of images in the animation. If the format does not support animation, 0 is returned. - This function returns -1 if an error occurred. - See also - Returns the format of the image, without actually reading the image contents. The format describes the image format - read()returns, not the format of the actual image.- If the image format does not support this feature, this function returns an invalid format. - See also - static imageFormat(device)
- Parameters:
- device – - QIODevice
- Return type:
 
 - If supported, this function returns the image format of the device - device. Otherwise, an empty string is returned.- See also - static imageFormat(fileName)
- Parameters:
- fileName – str 
- Return type:
 
 - If supported, this function returns the image format of the file - fileName. Otherwise, an empty string is returned.- static imageFormatsForMimeType(mimeType)¶
- Parameters:
- mimeType – - QByteArray
- Return type:
- .list of QByteArray 
 
 - Returns the list of image formats corresponding to - mimeType.- Note that the - QGuiApplicationinstance must be created before this function is called.- jumpToImage(imageNumber)¶
- Parameters:
- imageNumber – int 
- Return type:
- bool 
 
 - For image formats that support animation, this function skips to the image whose sequence number is - imageNumber, returning true if successful or false if the corresponding image cannot be found.- The next call to - read()will attempt to read this image.- See also - jumpToNextImage()¶
- Return type:
- bool 
 
 - For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation. - The default implementation calls - read(), then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.- See also - loopCount()¶
- Return type:
- int 
 
 - For image formats that support animation, this function returns the number of times the animation should loop. If this function returns -1, it can either mean the animation should loop forever, or that an error occurred. If an error occurred, - canRead()will return false.- See also - nextImageDelay()¶
- Return type:
- int 
 
 - For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation. If the image format doesn’t support animation, 0 is returned. - This function returns -1 if an error occurred. - See also - quality()¶
- Return type:
- int 
 
 - Returns the quality setting of the image format. - See also - Reads an image from the device. On success, the image that was read is returned; otherwise, a null - QImageis returned. You can then call- error()to find the type of error that occurred, or- errorString()to get a human readable description of the error.- For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned. - Returns the scaled clip rect of the image. - See also - Returns the scaled size of the image. - See also - static setAllocationLimit(mbLimit)¶
- Parameters:
- mbLimit – int 
 
 - Sets the allocation limit to - mbLimitmegabytes. Images that would require a- QImagememory allocation above this limit will be rejected. If- mbLimitis 0, the allocation size check will be disabled.- This limit helps applications avoid unexpectedly large memory usage from loading corrupt image files. It is normally not needed to change it. The default limit is large enough for all commonly used image sizes. - At runtime, this value may be overridden by the environment variable - QT_IMAGEIO_MAXALLOC.- Note - The memory requirements are calculated for a minimum of 32 bits per pixel, since Qt will typically convert an image to that depth when it is used in GUI. This means that the effective allocation limit is significantly smaller than - mbLimitwhen reading 1 bpp and 8 bpp images.- See also - setAutoDetectImageFormat(enabled)¶
- Parameters:
- enabled – bool 
 
 - If - enabledis true, image format autodetection is enabled; otherwise, it is disabled. By default, autodetection is enabled.- QImageReaderuses an extensive approach to detecting the image format; firstly, if you pass a file name to- QImageReader, it will attempt to detect the file extension if the given file name does not point to an existing file, by appending supported default extensions to the given file name, one at a time. It then uses the following approach to detect the image format:- Image plugins are queried first, based on either the optional format string, or the file name suffix (if the source device is a file). No content detection is done at this stage. - QImageReaderwill choose the first plugin that supports reading for this format.
- If no plugin supports the image format, Qt’s built-in handlers are checked based on either the optional format string, or the file name suffix. 
- If no capable plugins or built-in handlers are found, each plugin is tested by inspecting the content of the data stream. 
- If no plugins could detect the image format based on data contents, each built-in image handler is tested by inspecting the contents. 
- Finally, if all above approaches fail, - QImageReaderwill report failure when trying to read the image.
 - By disabling image format autodetection, - QImageReaderwill only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).- See also - autoDetectImageFormat()- canRead()- capabilities()- setAutoTransform(enabled)¶
- Parameters:
- enabled – bool 
 
 - Determines that images returned by - read()should have transformation metadata automatically applied if- enabledis- true.- See also - Sets the background color to - color. Image formats that support this operation are expected to initialize the background to- colorbefore reading an image.- See also - Sets the image clip rect (also known as the ROI, or Region Of Interest) to - rect. The coordinates of- rectare relative to the untransformed image size, as returned by- size().- See also - setDecideFormatFromContent(ignored)¶
- Parameters:
- ignored – bool 
 
 - If - ignoredis set to true, then the image reader will ignore specified formats or file extensions and decide which plugin to use only based on the contents in the datastream.- Setting this flag means that all image plugins gets loaded. Each plugin will read the first bytes in the image data and decide if the plugin is compatible or not. - This also disables auto detecting the image format. - See also - Sets - QImageReader‘s device to- device. If a device has already been set, the old device is removed from- QImageReaderand is otherwise left unchanged.- If the device is not already open, - QImageReaderwill attempt to open the device in ReadOnly mode by calling open(). Note that this does not work for certain devices, such as QProcess, QTcpSocket and QUdpSocket, where more logic is required to open the device.- See also - setFileName(fileName)¶
- Parameters:
- fileName – str 
 
 - Sets the file name of - QImageReaderto- fileName. Internally,- QImageReaderwill create a QFile object and open it in ReadOnly mode, and use this when reading images.- If - fileNamedoes not include a file extension (e.g., .png or .bmp),- QImageReaderwill cycle through all supported extensions until it finds a matching file.- See also - setFormat(format)¶
- Parameters:
- format – - QByteArray
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets the format - QImageReaderwill use when reading images, to- format.- formatis a case insensitive text string. Example:- reader = QImageReader() reader.setFormat("png") # same as reader.setFormat("PNG") - You can call - supportedImageFormats()for the full list of formats- QImageReadersupports.- See also - setQuality(quality)¶
- Parameters:
- quality – int 
 
 - Sets the quality setting of the image format to - quality.- Some image formats, in particular lossy ones, entail a tradeoff between a) visual quality of the resulting image, and b) decoding execution time. This function sets the level of that tradeoff for image formats that support it. - In case of scaled image reading, the quality setting may also influence the tradeoff level between visual quality and execution speed of the scaling algorithm. - The value range of - qualitydepends on the image format. For example, the “jpeg” format supports a quality range from 0 (low visual quality) to 100 (high visual quality).- See also - Sets the scaled clip rect to - rect. The scaled clip rect is the clip rect (also known as ROI, or Region Of Interest) that is applied after the image has been scaled.- See also - Sets the scaled size of the image to - size. The scaling is performed after the initial clip rect, but before the scaled clip rect is applied. The algorithm used for scaling depends on the image format. By default (i.e., if the image format does not support scaling),- QImageReaderwill use QImage::scale() with Qt::SmoothScaling.- If only one dimension is set in - size, the other one will be computed from the image’s- natural sizeso as to maintain the aspect ratio.- See also - Returns the size of the image, without actually reading the image contents. - If the image format does not support this feature, this function returns an invalid size. Qt’s built-in image handlers all support this feature, but custom image format plugins are not required to do so. - See also - subType()¶
- Return type:
 
 - Returns the subtype of the image. - static supportedImageFormats()¶
- Return type:
- .list of QByteArray 
 
 - Returns the list of image formats supported by - QImageReader.- By default, Qt can read the following formats: - Format - MIME type - Description - BMP - image/bmp - Windows Bitmap - GIF - image/gif - Graphic Interchange Format (optional) - JPG - image/jpeg - Joint Photographic Experts Group - PNG - image/png - Portable Network Graphics - PBM - image/x-portable-bitmap - Portable Bitmap - PGM - image/x-portable-graymap - Portable Graymap - PPM - image/x-portable-pixmap - Portable Pixmap - XBM - image/x-xbitmap - X11 Bitmap - XPM - image/x-xpixmap - X11 Pixmap - SVG - image/svg+xml - Scalable Vector Graphics - Reading and writing SVG files is supported through the Qt SVG module. The Qt Image Formats module provides support for additional image formats. - Note that the QCoreApplication instance must be created before this function is called. - See also - setFormat()- supportedImageFormats()- QImageIOPlugin- static supportedMimeTypes()¶
- Return type:
- .list of QByteArray 
 
 - Returns the list of MIME types supported by - QImageReader.- Note that the QApplication instance must be created before this function is called. - supportedSubTypes()¶
- Return type:
- .list of QByteArray 
 
 - Returns the list of subtypes supported by an image. - supportsAnimation()¶
- Return type:
- bool 
 
 - Returns - trueif the image format supports animation; otherwise, false is returned.- See also - supportsOption(option)¶
- Parameters:
- option – - ImageOption
- Return type:
- bool 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns - trueif the reader supports- option; otherwise returns false.- Different image formats support different options. Call this function to determine whether a certain option is supported by the current format. For example, the PNG format allows you to embed text into the image’s metadata (see - text()), and the BMP format allows you to determine the image’s size without loading the whole image into memory (see- size()).- reader = QImageReader(":/image.png") if reader.supportsOption(QImageIOHandler.Size): print("Size:", reader.size()) - See also - text(key)¶
- Parameters:
- key – str 
- Return type:
- str 
 
 - Returns the image text associated with - key.- Support for this option is implemented through - Description.- See also - 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.- Support for this option is implemented through - Description.- See also - transformation()¶
- Return type:
- Combination of - Transformation
 
 - Returns the transformation metadata of the image, including image orientation. If the format does not support transformation metadata, - TransformationNoneis returned.- See also