• PySide 模块
  • PySide.QtGui
  • 内容表

    上一话题

    QImageWriter

    下一话题

    PySide.QtHelp

    QImageReader

    详细描述

    PySide.QtGui.QImageReader class provides a format independent interface for reading images from files or other devices.

    读取图像的最常见方式是透过 PySide.QtGui.QImage and PySide.QtGui.QPixmap ‘s constructors, or by calling QImage.load() and QPixmap.load() . PySide.QtGui.QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling PySide.QtGui.QImageReader.setScaledSize() , and you can select a clip rect, effectively loading only parts of an image, by calling PySide.QtGui.QImageReader.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 PySide.QtGui.QImageReader object. Pass either a file name or a device pointer, and the image format to PySide.QtGui.QImageReader ‘s constructor. You can then set several options, such as the clip rect (by calling PySide.QtGui.QImageReader.setClipRect() ) and scaled size (by calling PySide.QtGui.QImageReader.setScaledSize() ). PySide.QtGui.QImageReader.canRead() returns the image if the PySide.QtGui.QImageReader can read the image (i.e., the image format is supported and the device is open for reading). Call PySide.QtGui.QImageReader.read() to read the image.

    若读取图像时发生任何错误, PySide.QtGui.QImageReader.read() will return a null PySide.QtGui.QImage . You can then call PySide.QtGui.QImageReader.error() to find the type of error that occurred, or PySide.QtGui.QImageReader.errorString() to get a human readable description of what went wrong.

    调用 PySide.QtGui.QImageReader.supportedImageFormats() for a list of formats that PySide.QtGui.QImageReader can read. PySide.QtGui.QImageReader supports all built-in image formats, in addition to any image format plugins that support reading.

    PySide.QtGui.QImageReader autodetects 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 PySide.QtGui.QImageReader.setAutoDetectImageFormat() .

    class PySide.QtGui. QImageReader
    class PySide.QtGui. QImageReader ( device [ , format=QByteArray() ] )
    class PySide.QtGui. QImageReader ( fileName [ , format=QByteArray() ] )
    参数:

    构造空 PySide.QtGui.QImageReader object. Before reading an image, call PySide.QtGui.QImageReader.setDevice() or PySide.QtGui.QImageReader.setFileName() .

    构造 PySide.QtGui.QImageReader object with the device device 和图像格式 format .

    构造 PySide.QtGui.QImageReader object with the file name fileName 和图像格式 format .

    PySide.QtGui.QImageReader. ImageReaderError

    This enum describes the different types of errors that can occur when reading images with PySide.QtGui.QImageReader .

    常量 描述
    QImageReader.FileNotFoundError PySide.QtGui.QImageReader was 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 PySide.QtGui.QImageReader encountered a device error when reading the image. You can consult your particular device for more details on what went wrong.
    QImageReader.UnsupportedFormatError Qt 不支持请求的图像格式。
    QImageReader.InvalidDataError The image data was invalid, and PySide.QtGui.QImageReader was 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 PySide.QtGui.QImageReader.read() , it is most likely caused by a bug in PySide.QtGui.QImageReader .
    PySide.QtGui.QImageReader. autoDetectImageFormat ( )
    返回类型: PySide.QtCore.bool

    Returns true if image format autodetection is enabled on this image reader; otherwise returns false. By default, autodetection is enabled.

    PySide.QtGui.QImageReader. backgroundColor ( )
    返回类型: PySide.QtGui.QColor

    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.

    PySide.QtGui.QImageReader. canRead ( )
    返回类型: PySide.QtCore.bool

    Returns 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.

    PySide.QtGui.QImageReader.canRead() is a lightweight function that only does a quick test to see if the image data is valid. PySide.QtGui.QImageReader.read() may still return false after PySide.QtGui.QImageReader.canRead() returns true, if the image data is corrupt.

    For images that support animation, PySide.QtGui.QImageReader.canRead() returns false when all frames have been read.

    PySide.QtGui.QImageReader. clipRect ( )
    返回类型: PySide.QtCore.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 PySide.QtCore.QRect 被返回。

    PySide.QtGui.QImageReader. currentImageNumber ( )
    返回类型: PySide.QtCore.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,若发生错误。

    PySide.QtGui.QImageReader. currentImageRect ( )
    返回类型: PySide.QtCore.QRect

    For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.

    PySide.QtGui.QImageReader. decideFormatFromContent ( )
    返回类型: PySide.QtCore.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.

    PySide.QtGui.QImageReader. device ( )
    返回类型: PySide.QtCore.QIODevice

    Returns the device currently assigned to PySide.QtGui.QImageReader , or 0 if no device has been assigned.

    PySide.QtGui.QImageReader. error ( )
    返回类型: PySide.QtGui.QImageReader.ImageReaderError

    返回最后发生的错误类型。

    另请参阅

    QImageReader.ImageReaderError PySide.QtGui.QImageReader.errorString()

    PySide.QtGui.QImageReader. errorString ( )
    返回类型: unicode

    返回最近发生错误的人类可读描述。

    PySide.QtGui.QImageReader. fileName ( )
    返回类型: unicode

    若目前赋值设备是 PySide.QtCore.QFile ,或者若 PySide.QtGui.QImageReader.setFileName() has been called, this function returns the name of the file PySide.QtGui.QImageReader reads from. Otherwise (i.e., if no device has been assigned or the device is not a PySide.QtCore.QFile ), an empty PySide.QtCore.QString 被返回。

    PySide.QtGui.QImageReader. format ( )
    返回类型: PySide.QtCore.QByteArray

    返回格式 PySide.QtGui.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() .

    PySide.QtGui.QImageReader. imageCount ( )
    返回类型: PySide.QtCore.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,若发生错误。

    static PySide.QtGui.QImageReader. imageFormat ( fileName )
    参数: fileName – unicode
    返回类型: PySide.QtCore.QByteArray

    If supported, this function returns the image format of the file fileName . Otherwise, an empty string is returned.

    PySide.QtGui.QImageReader. imageFormat ( )
    返回类型: PySide.QtGui.QImage.Format

    Returns the format of the image, without actually reading the image contents. The format describes the image format QImageReader.read() returns, not the format of the actual image.

    If the image format does not support this feature, this function returns an invalid format.

    另请参阅

    QImageIOHandler.ImageOption QImageIOHandler.option() QImageIOHandler.supportsOption()

    static PySide.QtGui.QImageReader. imageFormat ( device )
    参数: device PySide.QtCore.QIODevice
    返回类型: PySide.QtCore.QByteArray

    If supported, this function returns the image format of the device device . Otherwise, an empty string is returned.

    PySide.QtGui.QImageReader. jumpToImage ( imageNumber )
    参数: imageNumber PySide.QtCore.int
    返回类型: PySide.QtCore.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 PySide.QtGui.QImageReader.read() will attempt to read this image.

    PySide.QtGui.QImageReader. jumpToNextImage ( )
    返回类型: PySide.QtCore.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.

    默认实现调用 PySide.QtGui.QImageReader.read() , then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.

    PySide.QtGui.QImageReader. loopCount ( )
    返回类型: PySide.QtCore.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, PySide.QtGui.QImageReader.canRead() will return false.

    PySide.QtGui.QImageReader. nextImageDelay ( )
    返回类型: PySide.QtCore.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,若发生错误。

    PySide.QtGui.QImageReader. quality ( )
    返回类型: PySide.QtCore.int

    Returns the quality level of the image.

    PySide.QtGui.QImageReader. read ( )
    返回类型: PySide.QtGui.QImage

    Reads an image from the device. On success, the image that was read is returned; otherwise, a null PySide.QtGui.QImage is returned. You can then call PySide.QtGui.QImageReader.error() to find the type of error that occurred, or PySide.QtGui.QImageReader.errorString() to get a human readable description of the error.

    For image formats that support animation, calling PySide.QtGui.QImageReader.read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.

    PySide.QtGui.QImageReader. scaledClipRect ( )
    返回类型: PySide.QtCore.QRect

    Returns the scaled clip rect of the image.

    PySide.QtGui.QImageReader. scaledSize ( )
    返回类型: PySide.QtCore.QSize

    返回图像的比例缩放尺寸。

    PySide.QtGui.QImageReader. setAutoDetectImageFormat ( enabled )
    参数: enabled PySide.QtCore.bool

    enabled is true, image format autodetection is enabled; otherwise, it is disabled. By default, autodetection is enabled.

    PySide.QtGui.QImageReader uses an extensive approach to detecting the image format; firstly, if you pass a file name to PySide.QtGui.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. PySide.QtGui.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, PySide.QtGui.QImageReader will report failure when trying to read the image.

    By disabling image format autodetection, PySide.QtGui.QImageReader will only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).

    PySide.QtGui.QImageReader. setBackgroundColor ( color )
    参数: color PySide.QtGui.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.

    PySide.QtGui.QImageReader. setClipRect ( rect )
    参数: rect PySide.QtCore.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 PySide.QtGui.QImageReader.size() .

    PySide.QtGui.QImageReader. setDecideFormatFromContent ( ignored )
    参数: ignored PySide.QtCore.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.

    这还禁用自动检测图像格式。

    PySide.QtGui.QImageReader. setDevice ( device )
    参数: device PySide.QtCore.QIODevice

    PySide.QtGui.QImageReader ‘s device to device . If a device has already been set, the old device is removed from PySide.QtGui.QImageReader and is otherwise left unchanged.

    若设备尚未打开, PySide.QtGui.QImageReader will attempt to open the device in QIODevice.ReadOnly mode by calling open(). Note that this does not work for certain devices, such as PySide.QtCore.QProcess , PySide.QtNetwork.QTcpSocket and PySide.QtNetwork.QUdpSocket , where more logic is required to open the device.

    PySide.QtGui.QImageReader. setFileName ( fileName )
    参数: fileName – unicode

    Sets the file name of PySide.QtGui.QImageReader to fileName . Internally, PySide.QtGui.QImageReader will create a PySide.QtCore.QFile object and open it in QIODevice.ReadOnly mode, and use this when reading images.

    fileName does not include a file extension (e.g., .png or .bmp), PySide.QtGui.QImageReader will cycle through all supported extensions until it finds a matching file.

    PySide.QtGui.QImageReader. setFormat ( format )
    参数: format PySide.QtCore.QByteArray

    设置格式 PySide.QtGui.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")
    											

    可以调用 PySide.QtGui.QImageReader.supportedImageFormats() for the full list of formats PySide.QtGui.QImageReader 支持。

    PySide.QtGui.QImageReader. setQuality ( quality )
    参数: quality PySide.QtCore.int

    This is an image format specific function that sets the quality level of the image to quality . For image formats that do not support setting the quality, this value is ignored.

    The value range of quality depends on the image format. For example, the “jpeg” format supports a quality range from 0 (low quality, high compression) to 100 (high quality, low compression).

    PySide.QtGui.QImageReader. setScaledClipRect ( rect )
    参数: rect PySide.QtCore.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.

    PySide.QtGui.QImageReader. setScaledSize ( size )
    参数: size PySide.QtCore.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), PySide.QtGui.QImageReader will use QImage::scale() with Qt::SmoothScaling.

    PySide.QtGui.QImageReader. size ( )
    返回类型: PySide.QtCore.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.

    另请参阅

    QImageIOHandler.ImageOption QImageIOHandler.option() QImageIOHandler.supportsOption()

    static PySide.QtGui.QImageReader. supportedImageFormats ( )
    返回类型:

    Returns the list of image formats supported by PySide.QtGui.QImageReader .

    默认情况下,Qt 可以读取以下格式:

    格式 描述
    BMP Windows 位图
    GIF Graphic Interchange Format (图形互换格式) 可选
    JPG Joint Photographic Experts Group (联合摄影专家组)
    JPEG Joint Photographic Experts Group (联合摄影专家组)
    MNG 多图像网络图形
    PNG Portable Network Graphics (便携式网络图形)
    PBM Portable Bitmap (便携式位图)
    PGM Portable Graymap (便携式灰度图)
    PPM Portable Pixmap (便携式像素图)
    TIFF 标签化图像文件格式
    XBM X11 Bitmap (X11 位图)
    XPM X11 Pixmap (X11 像素图)
    SVG SVG (可伸缩向量图形)
    TGA Targa Image Format

    Reading and writing SVG files is supported through Qt's SVG Module .

    TGA support only extends to reading non-RLE compressed files. In particular calls to capabilities for the tga plugin returns only QImageIOPlugin.CanRead , not QImageIOPlugin.CanWrite .

    To configure Qt with GIF support, pass -qt-gif configure script or check the appropriate option in the graphical installer.

    注意: PySide.QtGui.QApplication 实例必须被创建在调用此函数之前。

    PySide.QtGui.QImageReader. supportsAnimation ( )
    返回类型: PySide.QtCore.bool

    Returns true if the image format supports animation; otherwise, false is returned.

    PySide.QtGui.QImageReader. supportsOption ( option )
    参数: option PySide.QtGui.QImageIOHandler.ImageOption
    返回类型: PySide.QtCore.bool
    PySide.QtGui.QImageReader. text ( key )
    参数: key – unicode
    返回类型: unicode

    返回的图像文本关联 key .

    支持此选项的实现是透过 QImageIOHandler.Description .

    PySide.QtGui.QImageReader. textKeys ( )
    返回类型: 字符串列表

    Returns the text keys for this image. You can use these keys with PySide.QtGui.QImageReader.text() to list the image text for a certain key.

    支持此选项的实现是透过 QImageIOHandler.Description .