# wx.ImageHandler¶

This is the base class for implementing image file loading/saving, and image creation from data.

It is used within wx.Image and is not normally seen by the application.

If you wish to extend the capabilities of wx.Image, derive a class from wx.ImageHandler and add the handler using wx.Image.AddHandler in your application initialization.

Note that all ImageHandlers provided by wxWidgets are part of the Core library. For details about the default handlers, please see the section Available image handlers in the wx.Image class documentation.

## Class Hierarchy¶

Inheritance diagram for class ImageHandler:

## Methods Summary¶

 __init__ Default constructor. CanRead Returns True if this handler supports the image format contained in the given stream. DoCanRead Called to test if this handler can read an image from the given stream. DoGetImageCount Called to get the number of images available in a multi-image file type, if supported. GetAltExtensions Returns the other file extensions associated with this handler. GetExtension Gets the preferred file extension associated with this handler. GetImageCount If the image file contains more than one image and the image handler is capable of retrieving these individually, this function will return the number of available images. GetMimeType Gets the MIME type associated with this handler. GetName Gets the name of this handler. GetType Gets the image type associated with this handler. LoadFile Loads an image from a stream, putting the resulting data into image. SaveFile Saves an image in the output stream. SetAltExtensions Sets the alternative file extensions associated with this handler. SetExtension Sets the preferred file extension associated with this handler. SetMimeType Sets the handler MIME type. SetName Sets the handler name. SetType Sets the bitmap type for the handler.

## Class API¶

class wx.ImageHandler(Object)

Possible constructors:

ImageHandler()


This is the base class for implementing image file loading/saving, and image creation from data.

### Methods¶

__init__(self)

Default constructor.

In your own default constructor, initialise the members m_name, m_extension and m_type.

CanRead(self, *args, **kw)

Returns True if this handler supports the image format contained in the given stream.

This function doesn’t modify the current stream position (because it restores the original position before returning; this however requires the stream to be seekable; see wx.StreamBase.IsSeekable ).

Parameters

stream (wx.InputStream) –

Return type

bool

Returns True if this handler supports the image format contained in the file with the given name.

This function doesn’t modify the current stream position (because it restores the original position before returning; this however requires the stream to be seekable; see wx.StreamBase.IsSeekable ).

Parameters

filename (string) –

Return type

bool

DoCanRead(self, stream)

Called to test if this handler can read an image from the given stream.

NOTE: this function is allowed to change the current stream position since CallDoCanRead() will take care of restoring it later

Parameters

stream (wx.InputStream) –

Return type

bool

DoGetImageCount(self, stream)

Called to get the number of images available in a multi-image file type, if supported.

NOTE: this function is allowed to change the current stream position since GetImageCount will take care of restoring it later

Parameters

stream (wx.InputStream) –

Return type

int

GetAltExtensions(self)

Returns the other file extensions associated with this handler.

The preferred extension for this handler is returned by GetExtension .

Return type

list of strings

New in version 2.9.0.

GetExtension(self)

Gets the preferred file extension associated with this handler.

Return type

string

GetImageCount(self, stream)

If the image file contains more than one image and the image handler is capable of retrieving these individually, this function will return the number of available images.

Parameters

stream (wx.InputStream) – Opened input stream for reading image data. This function doesn’t modify the current stream position (because it restores the original position before returning; this however requires the stream to be seekable; see wx.StreamBase.IsSeekable ).

Return type

int

Returns

Number of available images. For most image handlers, this is 1 (exceptions are TIFF and ICO formats as well as animated GIFs for which this function returns the number of frames in the animation).

GetMimeType(self)

Gets the MIME type associated with this handler.

Return type

string

GetName(self)

Gets the name of this handler.

Return type

string

GetType(self)

Gets the image type associated with this handler.

Return type

wx.BitmapType

LoadFile(self, image, stream, verbose=True, index=-1)

Loads an image from a stream, putting the resulting data into image.

If the image file contains more than one image and the image handler is capable of retrieving these individually, index indicates which image to read from the stream.

Parameters
• image (wx.Image) – The image object which is to be affected by this operation.

• stream (wx.InputStream) – Opened input stream for reading image data.

• verbose (bool) – If set to True, errors reported by the image handler will produce LogMessages.

• index (int) – The index of the image in the file (starting from zero).

Return type

bool

Returns

True if the operation succeeded, False otherwise.

SaveFile(self, image, stream, verbose=True)

Saves an image in the output stream.

Parameters
• image (wx.Image) – The image object which is to be affected by this operation.

• stream (wx.OutputStream) – Opened output stream for writing the data.

• verbose (bool) – If set to True, errors reported by the image handler will produce LogMessages.

Return type

bool

Returns

True if the operation succeeded, False otherwise.

SetAltExtensions(self, extensions)

Sets the alternative file extensions associated with this handler.

Parameters

extensions (list of strings) – Array of file extensions.

New in version 2.9.0.

SetExtension(self, extension)

Sets the preferred file extension associated with this handler.

Parameters

extension (string) – File extension without leading dot.

SetMimeType(self, mimetype)

Sets the handler MIME type.

Parameters

mimetype (string) – Handler MIME type.

SetName(self, name)

Sets the handler name.

Parameters

name (string) – Handler name.

SetType(self, type)

Sets the bitmap type for the handler.

Parameters

type (BitmapType) – The bitmap type.

### Properties¶

AltExtensions

Extension

MimeType

Name

Type