PySide.QtGui.QImageWriter class provides a format independent interface for writing images to files or other devices.
PySide.QtGui.QImageWriter supports setting format specific options, such as the gamma level, compression level and quality, prior to storing the image. If you do not need such options, you can use QImage.save() or QPixmap.save() 代替。
To store an image, you start by constructing a PySide.QtGui.QImageWriter object. Pass either a file name or a device pointer, and the image format to PySide.QtGui.QImageWriter ‘s constructor. You can then set several options, such as the gamma level (by calling PySide.QtGui.QImageWriter.setGamma() ) and quality (by calling PySide.QtGui.QImageWriter.setQuality() ). PySide.QtGui.QImageWriter.canWrite() returns true if PySide.QtGui.QImageWriter can write the image (i.e., the image format is supported and the device is open for writing). Call PySide.QtGui.QImageWriter.write() to write the image to the device.
If any error occurs when writing the image, PySide.QtGui.QImageWriter.write() will return false. You can then call PySide.QtGui.QImageWriter.error() to find the type of error that occurred, or PySide.QtGui.QImageWriter.errorString() to get a human readable description of what went wrong.
调用 PySide.QtGui.QImageWriter.supportedImageFormats() for a list of formats that PySide.QtGui.QImageWriter can write. PySide.QtGui.QImageWriter supports all built-in image formats, in addition to any image format plugins that support writing.
另请参阅
PySide.QtGui.QImageReader PySide.QtGui.QImageIOHandler QImageIOPlugin
| 参数: |
|
|---|
构造空 PySide.QtGui.QImageWriter object. Before writing, you must call PySide.QtGui.QImageWriter.setFormat() to set an image format, then PySide.QtGui.QImageWriter.setDevice() or PySide.QtGui.QImageWriter.setFileName() .
构造 PySide.QtGui.QImageWriter object using the device device and image format format .
构造 PySide.QtGui.QImageWriter objects that will write to a file with the name fileName , using the image format format 。若 format is not provided, PySide.QtGui.QImageWriter will detect the image format by inspecting the extension of fileName .
This enum describes errors that can occur when writing images with PySide.QtGui.QImageWriter .
| 常量 | 描述 |
|---|---|
| QImageWriter.DeviceError | PySide.QtGui.QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong. |
| QImageWriter.UnsupportedFormatError | Qt 不支持请求的图像格式。 |
| QImageWriter.UnknownError | An unknown error occurred. If you get this value after calling PySide.QtGui.QImageWriter.write() , it is most likely caused by a bug in PySide.QtGui.QImageWriter . |
| 返回类型: | PySide.QtCore.bool |
|---|
返回 true 若 PySide.QtGui.QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.
| 返回类型: | PySide.QtCore.int |
|---|
Returns the compression of the image.
| 返回类型: | PySide.QtCore.QIODevice |
|---|
Returns the device currently assigned to PySide.QtGui.QImageWriter , or 0 if no device has been assigned.
| 返回类型: | PySide.QtGui.QImageWriter.ImageWriterError |
|---|
返回最后发生的错误类型。
另请参阅
QImageWriter.ImageWriterError PySide.QtGui.QImageWriter.errorString()
| 返回类型: | unicode |
|---|
返回最近发生错误的人类可读描述。
| 返回类型: | unicode |
|---|
若目前赋值设备是 PySide.QtCore.QFile ,或者若 PySide.QtGui.QImageWriter.setFileName() has been called, this function returns the name of the file PySide.QtGui.QImageWriter writes to. Otherwise (i.e., if no device has been assigned or the device is not a PySide.QtCore.QFile ), an empty PySide.QtCore.QString 被返回。
| 返回类型: | PySide.QtCore.QByteArray |
|---|
返回格式 PySide.QtGui.QImageWriter uses for writing images.
| 返回类型: | PySide.QtCore.float |
|---|
Returns the gamma level of the image.
| 返回类型: | PySide.QtCore.int |
|---|
Returns the quality level of the image.
| 参数: | 压缩 – PySide.QtCore.int |
|---|
This is an image format specific function that set the compression of an image. For image formats that do not support setting the compression, this value is ignored.
The value range of 压缩 depends on the image format. For example, the “tiff” format supports two values, 0(no compression) and 1(LZW-compression).
| 参数: | device – PySide.QtCore.QIODevice |
|---|
集 PySide.QtGui.QImageWriter ‘s device to device . If a device has already been set, the old device is removed from PySide.QtGui.QImageWriter and is otherwise left unchanged.
若设备尚未打开, PySide.QtGui.QImageWriter will attempt to open the device in QIODevice.WriteOnly 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.
| 参数: | fileName – unicode |
|---|
Sets the file name of PySide.QtGui.QImageWriter to fileName . Internally, PySide.QtGui.QImageWriter will create a PySide.QtCore.QFile and open it in QIODevice.WriteOnly mode, and use this file when writing images.
| 参数: | format – PySide.QtCore.QByteArray |
|---|
设置格式 PySide.QtGui.QImageWriter will use when writing images, to format . format is a case insensitive text string. Example:
writer = QImageWriter()
writer.setFormat("png") # same as writer.setFormat("PNG")
可以调用 PySide.QtGui.QImageWriter.supportedImageFormats() for the full list of formats PySide.QtGui.QImageWriter 支持。
| 参数: | gamma – PySide.QtCore.float |
|---|
This is an image format specific function that sets the gamma level of the image to gamma . For image formats that do not support setting the gamma level, this value is ignored.
The value range of gamma depends on the image format. For example, the “png” format supports a gamma range from 0.0 to 1.0.
| 参数: | 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).
| 参数: |
|
|---|
Sets the image text associated with the key key to text . This is useful for storing copyright information or other information about the image. Example:
image = QImage("some/image.jpeg")
writer = QImageWriter("images/outimage.png", "png")
writer.setText("Author", "John Smith")
writer.write(image)
If you want to store a single block of data (e.g., a comment), you can pass an empty key, or use a generic key like “Description”.
The key and text will be embedded into the image data after calling PySide.QtGui.QImageWriter.write() .
支持此选项的实现是透过 QImageIOHandler.Description .
| 返回类型: |
|---|
Returns the list of image formats supported by PySide.QtGui.QImageWriter .
By default, Qt can write the following formats:
| 格式 | 描述 |
| BMP | Windows 位图 |
| JPG | Joint Photographic Experts Group (联合摄影专家组) |
| JPEG | Joint Photographic Experts Group (联合摄影专家组) |
| PNG | Portable Network Graphics (便携式网络图形) |
| PPM | Portable Pixmap (便携式像素图) |
| TIFF | 标签化图像文件格式 |
| XBM | X11 Bitmap (X11 位图) |
| XPM | X11 Pixmap (X11 像素图) |
Reading and writing SVG files is supported through Qt's SVG Module .
注意: PySide.QtGui.QApplication 实例必须被创建在调用此函数之前。
另请参阅
PySide.QtGui.QImageWriter.setFormat() QImageReader.supportedImageFormats() QImageIOPlugin
| 参数: | option – PySide.QtGui.QImageIOHandler.ImageOption |
|---|---|
| 返回类型: | PySide.QtCore.bool |
| 参数: | image – PySide.QtGui.QImage |
|---|---|
| 返回类型: | PySide.QtCore.bool |
写入图像 image to the assigned device or file name. Returns true on success; otherwise returns false. If the operation fails, you can call PySide.QtGui.QImageWriter.error() to find the type of error that occurred, or PySide.QtGui.QImageWriter.errorString() to get a human readable description of the error.