Table Of Contents

Previous topic

QMovie

Next topic

QImageReader

QImageWriter

Inheritance diagram of QImageWriter

Synopsis

Functions

Static functions

Detailed Description

The 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() instead.

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.

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

class PySide.QtGui.QImageWriter
class PySide.QtGui.QImageWriter(device, format)
class PySide.QtGui.QImageWriter(fileName[, format=QByteArray()])
Parameters:

Constructs an empty 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() .

Constructs a PySide.QtGui.QImageWriter object using the device device and image format format .

Constructs a PySide.QtGui.QImageWriter objects that will write to a file with the name fileName , using the image format format . If format is not provided, PySide.QtGui.QImageWriter will detect the image format by inspecting the extension of fileName .

PySide.QtGui.QImageWriter.ImageWriterError

This enum describes errors that can occur when writing images with PySide.QtGui.QImageWriter .

Constant Description
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 does not support the requested image format.
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.QtGui.QImageWriter.canWrite()
Return type:PySide.QtCore.bool

Returns true if PySide.QtGui.QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.

PySide.QtGui.QImageWriter.compression()
Return type:PySide.QtCore.int

Returns the compression of the image.

PySide.QtGui.QImageWriter.device()
Return type:PySide.QtCore.QIODevice

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

PySide.QtGui.QImageWriter.error()
Return type:PySide.QtGui.QImageWriter.ImageWriterError

Returns the type of error that last occurred.

See also

QImageWriter.ImageWriterError PySide.QtGui.QImageWriter.errorString()

PySide.QtGui.QImageWriter.errorString()
Return type:unicode

Returns a human readable description of the last error that occurred.

PySide.QtGui.QImageWriter.fileName()
Return type:unicode

If the currently assigned device is a PySide.QtCore.QFile , or if 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 is returned.

PySide.QtGui.QImageWriter.format()
Return type:PySide.QtCore.QByteArray

Returns the format PySide.QtGui.QImageWriter uses for writing images.

PySide.QtGui.QImageWriter.gamma()
Return type:PySide.QtCore.float

Returns the gamma level of the image.

PySide.QtGui.QImageWriter.quality()
Return type:PySide.QtCore.int

Returns the quality level of the image.

PySide.QtGui.QImageWriter.setCompression(compression)
Parameters:compressionPySide.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 compression depends on the image format. For example, the “tiff” format supports two values, 0(no compression) and 1(LZW-compression).

PySide.QtGui.QImageWriter.setDevice(device)
Parameters:devicePySide.QtCore.QIODevice

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

If the device is not already open, 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.

PySide.QtGui.QImageWriter.setFileName(fileName)
Parameters: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.

PySide.QtGui.QImageWriter.setFormat(format)
Parameters:formatPySide.QtCore.QByteArray

Sets the format 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")

You can call PySide.QtGui.QImageWriter.supportedImageFormats() for the full list of formats PySide.QtGui.QImageWriter supports.

PySide.QtGui.QImageWriter.setGamma(gamma)
Parameters:gammaPySide.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.

PySide.QtGui.QImageWriter.setQuality(quality)
Parameters:qualityPySide.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.QImageWriter.setText(key, text)
Parameters:
  • key – unicode
  • text – unicode

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() .

Support for this option is implemented through QImageIOHandler.Description .

static PySide.QtGui.QImageWriter.supportedImageFormats()
Return type:

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

By default, Qt can write the following formats:

Format Description
BMP Windows Bitmap
JPG Joint Photographic Experts Group
JPEG Joint Photographic Experts Group
PNG Portable Network Graphics
PPM Portable Pixmap
TIFF Tagged Image File Format
XBM X11 Bitmap
XPM X11 Pixmap

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

PySide.QtGui.QImageWriter.supportsOption(option)
Parameters:optionPySide.QtGui.QImageIOHandler.ImageOption
Return type:PySide.QtCore.bool
PySide.QtGui.QImageWriter.write(image)
Parameters:imagePySide.QtGui.QImage
Return type:PySide.QtCore.bool

Writes the image 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.