QImageReaderclass provides a format independent interface for reading images from files or other devices. 更多 …
def
autoDetectImageFormat
()
def
autoTransform
()
def
backgroundColor
()
def
canRead
()
def
clipRect
()
def
currentImageNumber
()
def
currentImageRect
()
def
decideFormatFromContent
()
def
device
()
def
error
()
def
errorString
()
def
fileName
()
def
format
()
def
gamma
()
def
imageCount
()
def
imageFormat
()
def
jumpToImage
(imageNumber)
def
jumpToNextImage
()
def
loopCount
()
def
nextImageDelay
()
def
quality
()
def
read
()
def
scaledClipRect
()
def
scaledSize
()
def
setAutoDetectImageFormat
(enabled)
def
setAutoTransform
(enabled)
def
setBackgroundColor
(color)
def
setClipRect
(rect)
def
setDecideFormatFromContent
(ignored)
def
setDevice
(device)
def
setFileName
(fileName)
def
setFormat
(format)
def
setGamma
(gamma)
def
setQuality
(quality)
def
setScaledClipRect
(rect)
def
setScaledSize
(size)
def
size
()
def
subType
()
def
supportedSubTypes
()
def
supportsAnimation
()
def
supportsOption
(option)
def
text
(key)
def
textKeys
()
def
transformation
()
def
imageFormat
(device)
def
imageFormat
(fileName)
def
imageFormatsForMimeType
(mimeType)
def
supportedImageFormats
()
def
supportedMimeTypes
()
读取图像的最常见方式是透过
QImageandQPixmap‘s constructors, or by callingload()andload().QImageReaderis a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by callingsetScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by callingsetClipRect(). 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 toQImageReader‘s constructor. You can then set several options, such as the clip rect (by callingsetClipRect()) and scaled size (by callingsetScaledSize()).canRead()returns the image if theQImageReadercan read the image (i.e., the image format is supported and the device is open for reading). Callread()to read the image.若读取图像时发生任何错误,
read()will return a nullQImage. You can then callerror()to find the type of error that occurred, orerrorString()to get a human readable description of what went wrong.注意
QImageReaderassumes exclusive control over the file or device that is assigned. Any attempts to modify the assigned file or device during the lifetime of theQImageReaderobject will yield undefined results.
调用
supportedImageFormats()for a list of formats thatQImageReadercan read.QImageReadersupports all built-in image formats, in addition to any image format plugins that support reading. CallsupportedMimeTypes()to obtain a list of supported MIME types, which for example can be passed tosetMimeTypeFilters().
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 callingsetAutoDetectImageFormat().
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.另请参阅
QImageWriterQImageIOHandlerQImageIOPluginQMimeDatabaseQColorSpacedevicePixelRatio()devicePixelRatio()QIcondrawPixmap()drawImage()AA_UseHighDpiPixmaps
QImageReader
¶
QImageReader(device[, format=QByteArray()])
QImageReader(fileName[, format=QByteArray()])
- param format
QByteArray- param device
QIODevice- param fileName
unicode
构造空
QImageReader
object. Before reading an image, call
setDevice()
or
setFileName()
.
PySide2.QtGui.QImageReader.
ImageReaderError
¶
This enum describes the different types of errors that can occur when reading images with
QImageReader
.
|
常量 |
描述 |
|---|---|
|
QImageReader.FileNotFoundError |
|
|
QImageReader.DeviceError |
|
|
QImageReader.UnsupportedFormatError |
Qt 不支持请求的图像格式。 |
|
QImageReader.InvalidDataError |
The image data was invalid, and
|
|
QImageReader.UnknownError |
An unknown error occurred. If you get this value after calling
|
PySide2.QtGui.QImageReader.
autoDetectImageFormat
(
)
¶
bool
返回
true
if image format autodetection is enabled on this image reader; otherwise returns
false
. By default, autodetection is enabled.
PySide2.QtGui.QImageReader.
autoTransform
(
)
¶
bool
返回
true
if the image handler will apply transformation metadata on
read()
.
PySide2.QtGui.QImageReader.
backgroundColor
(
)
¶
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.
PySide2.QtGui.QImageReader.
canRead
(
)
¶
bool
返回
true
if 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
.
is a lightweight function that only does a quick test to see if the image data is valid.
read()
may still return false after returns
true
, if the image data is corrupt.
注意
A
QMimeDatabase
lookup is normally a better approach than this function for identifying potentially non-image files or data.
For images that support animation, returns
false
when all frames have been read.
PySide2.QtGui.QImageReader.
clipRect
(
)
¶
QRect
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
被返回。
另请参阅
PySide2.QtGui.QImageReader.
currentImageNumber
(
)
¶
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.
此函数返回 -1,若发生错误。
PySide2.QtGui.QImageReader.
currentImageRect
(
)
¶
QRect
For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.
PySide2.QtGui.QImageReader.
decideFormatFromContent
(
)
¶
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.
PySide2.QtGui.QImageReader.
device
(
)
¶
QIODevice
Returns the device currently assigned to
QImageReader
,或
None
若没有设备被赋值。
另请参阅
PySide2.QtGui.QImageReader.
error
(
)
¶
返回最后发生的错误类型。
另请参阅
ImageReaderError
errorString()
PySide2.QtGui.QImageReader.
fileName
(
)
¶
unicode
若目前赋值设备是
QFile
,或者若
setFileName()
has been called, this function returns the name of the file
QImageReader
reads from. Otherwise (i.e., if no device has been assigned or the device is not a
QFile
), an empty
QString
被返回。
另请参阅
PySide2.QtGui.QImageReader.
format
(
)
¶
QByteArray
返回格式
QImageReader
用于读取图像。
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().
PySide2.QtGui.QImageReader.
gamma
(
)
¶
float
注意
此函数被弃用。
使用
colorSpace()
and
gamma()
代替。
Returns the gamma level of the decoded image. If
setGamma()
has been called and gamma correction is supported it will return the gamma set. If gamma level is not supported by the image format,
0.0
被返回。
另请参阅
PySide2.QtGui.QImageReader.
imageCount
(
)
¶
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.
此函数返回 -1,若发生错误。
PySide2.QtGui.QImageReader.
imageFormat
(
)
¶
格式
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.
另请参阅
ImageOption
option()
supportsOption()
PySide2.QtGui.QImageReader.
imageFormat
(
device
)
¶
device
–
QIODevice
QByteArray
If supported, this function returns the image format of the device
device
. Otherwise, an empty string is returned.
PySide2.QtGui.QImageReader.
imageFormat
(
fileName
)
¶
fileName – unicode
QByteArray
PySide2.QtGui.QImageReader.
imageFormatsForMimeType
(
mimeType
)
¶
mimeType
–
QByteArray
Returns the list of image formats corresponding to
mimeType
.
注意:
QGuiApplication
实例必须被创建在调用此函数之前。
PySide2.QtGui.QImageReader.
jumpToImage
(
imageNumber
)
¶
imageNumber
–
int
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.
PySide2.QtGui.QImageReader.
jumpToNextImage
(
)
¶
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.
默认实现调用
read()
, then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.
PySide2.QtGui.QImageReader.
loopCount
(
)
¶
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.
PySide2.QtGui.QImageReader.
nextImageDelay
(
)
¶
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.
此函数返回 -1,若发生错误。
PySide2.QtGui.QImageReader.
quality
(
)
¶
int
返回图像格式的品质设置。
另请参阅
PySide2.QtGui.QImageReader.
read
(
)
¶
Reads an image from the device. On success, the image that was read is returned; otherwise, a null
QImage
is 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 repeatedly will return the next frame. When all frames have been read, a null image will be returned.
PySide2.QtGui.QImageReader.
scaledClipRect
(
)
¶
QRect
Returns the scaled clip rect of the image.
另请参阅
PySide2.QtGui.QImageReader.
scaledSize
(
)
¶
QSize
返回图像的比例缩放尺寸。
另请参阅
PySide2.QtGui.QImageReader.
setAutoDetectImageFormat
(
enabled
)
¶
enabled
–
bool
若
enabled
is true, image format autodetection is enabled; otherwise, it is disabled. By default, autodetection is enabled.
QImageReader
uses 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.
QImageReader
will 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,
QImageReader
will report failure when trying to read the image.
By disabling image format autodetection,
QImageReader
will only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).
另请参阅
autoDetectImageFormat()
canRead()
capabilities()
PySide2.QtGui.QImageReader.
setAutoTransform
(
enabled
)
¶
enabled
–
bool
Determines that images returned by
read()
should have transformation metadata automatically applied if
enabled
is
true
.
PySide2.QtGui.QImageReader.
setBackgroundColor
(
color
)
¶
color
–
QColor
Sets the background color to
color
. Image formats that support this operation are expected to initialize the background to
color
before reading an image.
另请参阅
PySide2.QtGui.QImageReader.
setClipRect
(
rect
)
¶
rect
–
QRect
Sets the image clip rect (also known as the ROI, or Region Of Interest) to
rect
. The coordinates of
rect
are relative to the untransformed image size, as returned by
size()
.
PySide2.QtGui.QImageReader.
setDecideFormatFromContent
(
ignored
)
¶
ignored
–
bool
若
ignored
is 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.
这还禁用自动检测图像格式。
PySide2.QtGui.QImageReader.
setDevice
(
device
)
¶
device
–
QIODevice
集
QImageReader
‘s device to
device
. If a device has already been set, the old device is removed from
QImageReader
and is otherwise left unchanged.
若设备尚未打开,
QImageReader
will 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.
另请参阅
PySide2.QtGui.QImageReader.
setFileName
(
fileName
)
¶
fileName – unicode
Sets the file name of
QImageReader
to
fileName
. Internally,
QImageReader
will create a
QFile
object and open it in
ReadOnly
mode, and use this when reading images.
若
fileName
does not include a file extension (e.g., .png or .bmp),
QImageReader
will cycle through all supported extensions until it finds a matching file.
PySide2.QtGui.QImageReader.
setFormat
(
format
)
¶
format
–
QByteArray
设置格式
QImageReader
will use when reading images, to
format
.
format
is a case insensitive text string. Example:
reader = QImageReader()
reader.setFormat("png") # same as reader.setFormat("PNG")
可以调用
supportedImageFormats()
for the full list of formats
QImageReader
支持。
另请参阅
PySide2.QtGui.QImageReader.
setGamma
(
gamma
)
¶
gamma
–
float
注意
此函数被弃用。
使用
QColorSpace
conversion on the
QImage
代替。
This is an image format specific function that forces images with gamma information to be gamma corrected to
gamma
. For image formats that do not support gamma correction, this value is ignored.
To gamma correct to a standard PC color-space, set gamma to
1/2.2
.
另请参阅
PySide2.QtGui.QImageReader.
setQuality
(
quality
)
¶
quality
–
int
将图像格式的品质设置设为
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
quality
depends on the image format. For example, the “jpeg” format supports a quality range from 0 (low visual quality) to 100 (high visual quality).
另请参阅
PySide2.QtGui.QImageReader.
setScaledClipRect
(
rect
)
¶
rect
–
QRect
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.
PySide2.QtGui.QImageReader.
setScaledSize
(
size
)
¶
size
–
QSize
将图像的比例缩放尺寸设为
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),
QImageReader
will use QImage::scale() with Qt::SmoothScaling.
PySide2.QtGui.QImageReader.
size
(
)
¶
QSize
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.
另请参阅
ImageOption
option()
supportsOption()
PySide2.QtGui.QImageReader.
subType
(
)
¶
QByteArray
返回图像的子类型。
PySide2.QtGui.QImageReader.
supportedImageFormats
(
)
¶
Returns the list of image formats supported by
QImageReader
.
默认情况下,Qt 可以读取以下格式:
|
格式 |
MIME 类型 |
描述 |
|
BMP |
image/bmp |
Windows 位图 |
|
GIF |
image/gif |
Graphic Interchange Format (图形互换格式) 可选 |
|
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 (X11 位图) |
|
XPM |
image/x-xpixmap |
X11 Pixmap (X11 像素图) |
|
SVG |
image/svg+xml |
SVG (可伸缩向量图形) |
Reading and writing SVG files is supported through the Qt SVG module. The Qt Image Formats module provides support for additional image formats.
注意:
QApplication
实例必须被创建在调用此函数之前。
另请参阅
setFormat()
supportedImageFormats()
QImageIOPlugin
PySide2.QtGui.QImageReader.
supportedMimeTypes
(
)
¶
Returns the list of MIME types supported by
QImageReader
.
注意:
QApplication
实例必须被创建在调用此函数之前。
PySide2.QtGui.QImageReader.
supportedSubTypes
(
)
¶
返回由图像所支持的子类型列表。
PySide2.QtGui.QImageReader.
supportsAnimation
(
)
¶
bool
返回
true
若图像格式支持动画;否则,返回 false。
另请参阅
PySide2.QtGui.QImageReader.
supportsOption
(
option
)
¶
option
–
ImageOption
bool
返回
true
若读取器支持
option
;否则返回 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:", str(reader.size())
另请参阅
PySide2.QtGui.QImageReader.
text
(
key
)
¶
key – unicode
unicode
返回的图像文本关联
key
.
支持此选项的实现是透过
描述
.
另请参阅
PySide2.QtGui.QImageReader.
textKeys
(
)
¶
字符串列表
Returns the text keys for this image. You can use these keys with
text()
to list the image text for a certain key.
支持此选项的实现是透过
描述
.
另请参阅
PySide2.QtGui.QImageReader.
transformation
(
)
¶
变换
Returns the transformation metadata of the image, including image orientation. If the format does not support transformation metadata,
TransformationNone
被返回。