Inheritance diagram for wx.Image:
This class encapsulates a platform-independent image. An image can be created from data, or using wx.Bitmap.ConvertToImage. An image can be loaded from a file in a variety of formats, and is extensible to new formats via image format handlers. Functions are available to set and get image bits, so it can be used for basic image manipulation.
A wx.Image cannot (currently) be drawn directly to a wx.DC. Instead, a platform-specific wx.Bitmap object must be created from it using the ConvertToBitmap method. This bitmap can then be drawn in a device context, using wx.DC.DrawBitmap.
One colour value of the image may be used as a mask colour which will lead to the automatic creation of a wx.Mask object associated to the bitmap object.
wx.Image supports alpha channel data, that is in addition to a byte for the red, green and blue colour components for each pixel it also stores a byte representing the pixel opacity. An alpha value of 0 corresponds to a transparent pixel (null opacity) while a value of 255 means that the pixel is 100% opaque.
Unlike RGB data, not all images have an alpha channel and before using GetAlpha you should check if this image contains an alpha channel with HasAlpha.
Note
Note that currently only images loaded from PNG files with transparency information will have an alpha channel.
See also
Loads an image from a file.
Parameters:
name (string): Name of the file from which to load the image.
type (int): One of the following values:
Bitmap Type Constant |
Description |
---|---|
wx.BITMAP_TYPE_BMP |
Load a Windows image file. |
wx.BITMAP_TYPE_GIF |
Load a GIF image file. |
wx.BITMAP_TYPE_JPEG |
Load a JPEG image file. |
wx.BITMAP_TYPE_PCX |
Load a PCX image file. |
wx.BITMAP_TYPE_PNG |
Load a PNG image file. |
wx.BITMAP_TYPE_PNM |
Load a PNM image file. |
wx.BITMAP_TYPE_TIF |
Load a TIFF image file. |
wx.BITMAP_TYPE_XPM |
Load a XPM image file. |
wx.BITMAP_TYPE_ICO |
Load a Windows icon file (ICO). |
wx.BITMAP_TYPE_CUR |
Load a Windows cursor file (CUR). |
wx.BITMAP_TYPE_ANI |
Load a Windows animated cursor file (ANI). |
wx.BITMAP_TYPE_ANY |
Will try to autodetect the format. |
index (int): Index of the image to load in the case that the image file contains multiple images. This is only used by GIF, ICO and TIFF handlers. The default value (-1) means to choose the default image and is interpreted as the first image (the one with index = 0) by the GIF and TIFF handler and as the largest and most colourful one by the ICO handler.
Returns:
Adds a handler to the end of the static list of format handlers.
Parameters:
This function muliplies all 4 channels (red, green, blue, alpha) with a factor (around 1.0). Useful for gamma correction, colour correction and to add a certain amount of transparency to a image (fade in fade out effects). If factor_alpha is given but the original image has no alpha channel then a alpha channel will be added.
Parameters:
Returns:
Blurs the image in both horizontal and vertical directions by the specified pixel blurRadius. This should not be used when using a single mask colour for transparency.
Parameters:
Returns:
See also
Blurs the image in the horizontal direction only. This should not be used when using a single mask colour for transparency.
Parameters:
Returns:
See also
Blurs the image in the vertical direction only. This should not be used when using a single mask colour for transparency.
Parameters:
Returns:
See also
Returns True if the image handlers can read this file.
Parameters:
Returns:
bool
Returns True if the image handlers can read an image file from the data currently on the input stream, or a readable Python file-like object.
Parameters:
Returns:
bool
Computes the histogram of the image. histogram is a reference to wx.ImageHistogram object. wx.ImageHistogram is a specialization of wx.HashMap “template”.
Parameters:
Returns:
unsigned long
If the image has alpha channel, this method converts it to mask. All pixels with alpha value less than threshold are replaced with mask colour and the alpha channel is removed. Mask colour is chosen automatically using FindFirstUnusedColour.
If the image image doesn’t have alpha channel, ConvertAlphaToMask does nothing.
Parameters:
Returns:
bool
This method converts an image where the original alpha information is only available as a shades of a colour (actually shades of grey) typically when you draw anti-aliased text into a bitmap. The DC drawing routines draw grey values on the black background although they actually mean to draw white with differnt alpha values. This method reverses it, assuming a black (!) background and white text. The method will then fill up the whole image with the colour given.
Parameters:
Returns:
bool
Returns a greyscale version of the image. The returned image uses the luminance component of the original to calculate the greyscale.
Defaults to using ITU-T BT.601 when converting to YUV, where every pixel equals (R * lr) + (G * lg) + (B * lb).
Parameters:
Returns:
Returns monochromatic version of the image. The returned image has white colour where the original has (r, g, b) colour and black colour everywhere else.
Parameters:
Returns:
Returns a monochromatic wx.Bitmap version of the image. The returned image has white colour where the original has (r, g, b) colour and black colour everywhere else.
Parameters:
Returns:
Creates a fresh image. If clear is True, the new image will be initialized to black. Otherwise, the image data will be uninitialized.
Parameters:
Returns:
bool
Finds the first colour that is never used in the image. The search begins at given initial colour and continues by increasing R, G and B components (in this order) by 1 until an unused colour is found or the colour space exhausted.
Returns a tuple consisting of a success flag and rgb values.
Parameters:
Returns:
(success, r, g, b)
Note
Note that this method involves computing the histogram, which is computationally intensive operation.
Returns the alpha value for the given pixel. This function may only be called for the images with alpha channel, use HasAlpha to check for this.
The returned value is the opacity of the image, i.e. the value of 0 corresponds to the fully transparent pixels while the value of 255 to the fully opaque pixels.
Parameters:
Returns:
byte
Returns a writable Python buffer object that is pointing at the Alpha data buffer inside the wx.Image. You need to ensure that you do not use this buffer object after the image has been destroyed.
Returns:
PyObject
Returns a string containing a copy of the alpha bytes of the image.
Returns:
PyObject
Returns the blue intensity at the given coordinate.
Parameters:
Returns:
byte
Returns the image data as an array. This is most often used when doing direct image manipulation. The return value points to an array of characters in RGBRGBRGB... format in the top-to-bottom, left-to-right order, that is the first RGB triplet corresponds to the pixel first pixel of the first row, the second one — to the second pixel of the first row and so on until the end of the first row, with second row following after it and so on.
You should not delete the returned pointer nor pass it to SetData.
Returns:
unsigned string
Returns a writable Python buffer object that is pointing at the RGB image data buffer inside the wx.Image. You need to ensure that you do not use this buffer object after the image has been destroyed.
Returns:
PyObject
Returns the green intensity at the given coordinate.
Parameters:
Returns:
byte
Returns the static list of image format handlers.
Returns:
list of objects
See also
Gets the height of the image in pixels.
Returns:
int
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.
Returns:
int
Iterates all registered wx.ImageHandler objects, and returns a string containing file extension masks suitable for passing to file open/save dialog boxes.
Returns:
string
See also
Gets the blue value of the mask colour.
Returns:
int
Gets the green value of the mask colour.
Returns:
int
Gets the red value of the mask colour.
Returns:
int
Gets a user-defined option. The function is case-insensitive to name.
For example, when saving as a JPEG file, the option quality is used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
Parameters:
Returns:
string
See also
Gets a user-defined option as an integer. The function is case-insensitive to name. If the given option is not present, the function returns 0. Use HasOption is 0 is a possibly valid value for the option.
Options for wx.PNGHandler:
Option Constant | Description |
---|---|
wx.IMAGE_OPTION_PNG_FORMAT | Format for saving a PNG file. |
wx.IMAGE_OPTION_PNG_BITDEPTH | Bit depth for every channel (R/G/B/A). |
Supported values for wx.IMAGE_OPTION_PNG_FORMAT:
wx.PNG_TYPE_COLOUR | Stores RGB image. |
wx.PNG_TYPE_GREY | Stores grey image, converts from RGB. |
wx.PNG_TYPE_GREY_RED | Stores grey image, uses red value as grey. |
Parameters:
Returns:
int
Get the current mask colour or find a suitable unused colour that could be used as a mask colour.
Returns True if the image currently has a mask.
Returns:
bool
Returns the red intensity at the given coordinate.
Parameters:
Returns:
byte
Returns a sub image of the current one as long as the rect belongs entirely to the image.
Parameters:
Returns:
Gets the width of the image in pixels.
Returns:
int
See also
Converts a color in HSV color space to RGB color space.
Parameters:
Returns:
Returns True if this image has alpha channel, False otherwise.
Returns:
bool
Returns True if there is a mask active, False otherwise.
Returns:
bool
Returns True if the given option is present. The function is case-insensitive to name.
Parameters:
Returns:
bool
See also
Adds a handler at the start of the static list of format handlers.
Parameters:
See also
Returns True if image data is present.
Returns:
bool
Returns True if the given pixel is transparent, i.e. either has the mask colour if this image has a mask or if this image has alpha channel and alpha value of this pixel is strictly less than threshold.
Parameters:
Returns:
bool
Loads an image from an input stream.
Parameters:
name (string): Name of the file from which to load the image.
type (int): One of the following values:
Bitmap Type Constant |
Description |
---|---|
wx.BITMAP_TYPE_BMP |
Load a Windows image file. |
wx.BITMAP_TYPE_GIF |
Load a GIF image file. |
wx.BITMAP_TYPE_JPEG |
Load a JPEG image file. |
wx.BITMAP_TYPE_PCX |
Load a PCX image file. |
wx.BITMAP_TYPE_PNG |
Load a PNG image file. |
wx.BITMAP_TYPE_PNM |
Load a PNM image file. |
wx.BITMAP_TYPE_TIF |
Load a TIFF image file. |
wx.BITMAP_TYPE_XPM |
Load a XPM image file. |
wx.BITMAP_TYPE_ICO |
Load a Windows icon file (ICO). |
wx.BITMAP_TYPE_CUR |
Load a Windows cursor file (CUR). |
wx.BITMAP_TYPE_ANI |
Load a Windows animated cursor file (ANI). |
wx.BITMAP_TYPE_ANY |
Will try to autodetect the format. |
index (int): Index of the image to load in the case that the image file contains multiple images. This is only used by GIF, ICO and TIFF handlers. The default value (-1) means to choose the default image and is interpreted as the first image (the one with index = 0) by the GIF and TIFF handler and as the largest and most colourful one by the ICO handler
Returns:
bool
Note
Depending on how wxWidgets has been configured, not all formats may be available.
Note
you can use GetOptionInt to set the hotspot before saving an image into a cursor file (default hotspot is in the centre of the image):
hotspot_x = image.GetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X)
hotspot_y = image.GetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y)
See also
Loads an image from a file, specifying the image type with a MIME type string.
Parameters:
Returns:
bool
Loads an image from an input stream or a readable Python file-like object, using a MIME type string to specify the image file format.
Parameters:
Returns:
bool
Loads an image from an input stream or a readable Python file-like object. If no handler type is provided, the library will try to autodetect the format.
Parameters:
Returns:
bool
Returns a mirrored copy of the image. The parameter horizontally indicates the orientation.
Parameters:
Returns:
Pastes image into this instance and takes care of the mask colour and any out of bounds problems.
Parameters:
Converts a color in RGB color space to HSV color space.
Parameters:
Returns:
Finds the handler with the given name, and removes it. The handler is not deleted.
Parameters:
Returns:
bool
See also
Replaces the colour specified by r1, g1, b1 by the colour r2, g2, b2.
Parameters:
Changes the size of the image in-place by scaling it: after a call to this function, the image will have the given width and height. For a description of the quality parameter, see the Scale function.
Returns the (modified) image itself.
Parameters:
Returns:
See also
Changes the size of the image in-place without scaling it by adding either a border with the given colour or cropping as necessary. The image is pasted into a new image with the given size and background colour at the position pos relative to the upper left of the new image. If red = green = blue = -1 then use either the current mask colour if set or find, use, and set a suitable mask colour for any newly exposed areas.
Returns the (modified) image itself.
Parameters:
Returns:
See also
Rotates the image about the given point, by angle radians. Passing True to interpolating results in better image quality, but is slower. If the image has a mask, then the mask colour is used for the uncovered pixels in the rotated image background. Else, black RGB(0, 0, 0) will be used.
Returns the rotated image, leaving this image intact.
Parameters:
Returns:
Returns a copy of the image rotated 90 degrees in the direction indicated by clockwise.
Parameters:
Returns:
Rotates the hue of each pixel in the image by angle, which is a double in the range of -1.0 to +1.0, where -1.0 corresponds to -360 degrees and +1.0 corresponds to +360 degrees.
Parameters:
Saves an image in the given stream.
Parameters:
name (string): Name of the file to save the image to.
type (int): Currently these types can be used:
Bitmap Type Constant |
Description |
---|---|
wx.BITMAP_TYPE_BMP |
Save a Windows image file. |
wx.BITMAP_TYPE_JPEG |
Save a JPEG image file. |
wx.BITMAP_TYPE_PCX |
Save a PCX image file. |
wx.BITMAP_TYPE_PNG |
Save a PNG image file. |
wx.BITMAP_TYPE_PNM |
Save a PNM image file. |
wx.BITMAP_TYPE_TIFF |
Save a TIFF image file. |
wx.BITMAP_TYPE_XPM |
Save a XPM image file. |
wx.BITMAP_TYPE_ICO |
Save a Windows icon file (ICO). |
wx.BITMAP_TYPE_CUR |
Save a Windows cursor file (CUR). |
Returns:
bool
Note
Depending on how wxWidgets has been configured, not all formats may be available.
Note
you can use GetOptionInt to set the hotspot before saving an image into a cursor file (default hotspot is in the centre of the image):
hotspot_x = image.GetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X)
hotspot_y = image.GetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y)
See also
Saves an image in the named file.
Parameters:
Returns:
bool
Saves an image in the named file.
Parameters:
Returns:
bool
Saves an image in the named file.
Parameters:
Returns:
bool
Returns a scaled version of the image. This is also useful for scaling bitmaps in general as the only other way to scale bitmaps is to blit a wx.MemoryDC into another wx.MemoryDC.
It should be noted that although using wx.IMAGE_QUALITY_HIGH produces much nicer looking results it is a slower method. Downsampling will use the box averaging method which seems to operate very fast. If you are upsampling larger images using this method you will most likely notice that it is a bit slower and in extreme cases it will be quite substantially slower as the bicubic algorithm has to process a lot of data.
It should also be noted that the high quality scaling may not work as expected when using a single mask colour for transparency, as the scaling will blur the image and will therefore remove the mask partially. Using the alpha channel will work.
Example:
# Get the bitmap from somewhere
bmp = wx.Bitmap("MyBitmap.png", wx.BITMAP_TYPE_PNG)
# rescale it to have size of 32*32
if bmp.GetWidth() != 32 or bmp.GetHeight() != 32:
image = bmp.ConvertToImage()
bmp = wx.BitmapFromImage(image.Scale(32, 32))
# another possibility:
image.Rescale(32, 32)
bmp = image
Parameters:
width (int): The desired width
height (int): The desired height
quality (int): Determines what method to use for resampling the image. Can be one of the following:
Quality Flag |
Description |
---|---|
wx.IMAGE_QUALITY_NORMAL |
Uses the normal default scaling method of pixel replication |
wx.IMAGE_QUALITY_HIGH |
Uses bicubic and box averaging resampling methods for upsampling and downsampling respectively |
Returns:
See also
Sets the alpha value for the given pixel. This function should only be called if the image has alpha channel data, use HasAlpha to check for this.
Parameters:
Sets the internal image alpha pointer to point at a Python buffer object. This can save making an extra copy of the data but you must ensure that the buffer object lives as long as the wx.Image does.
Parameters:
Resets the Image’s alpha data from a buffer of bytes. Accepts either a string or a buffer object holding the data and the length of the data must be width*height.
Parameters:
Sets the image data without performing checks. The data given must have the size (width*height*3) or results will be unexpected. Don’t use this method if you aren’t sure you know what you are doing.
After this call the pointer to the data is owned by the wx.Image object, that will be responsible for deleting it. Do not pass to this function a pointer obtained through GetData.
Parameters:
Sets the internal image data pointer to point at a Python buffer object. This can save making an extra copy of the data but you must ensure that the buffer object lives longer than the wx.Image does.
Parameters:
Specifies whether there is a mask or not. The area of the mask is determined by the current mask colour.
Parameters:
Sets the mask colour for this image (and tells the image to use the mask).
Parameters:
Sets the image’s mask so that the pixels that have RGB value of (mr, mg, mb) in mask will be masked in this image.
This is done by first finding an unused colour in the image, setting this colour as the mask colour and then using this colour to draw all pixels in the image who corresponding pixel in mask has given RGB value.
Returns False if mask does not have same dimensions as the image or if there is no unused colour left. Returns True if the mask was successfully applied.
Parameters:
Returns:
bool
Note
Note that this method involves computing the histogram, which is computationally intensive operation.
Sets a user-defined option. The function is case-insensitive to name.
For example, when saving as a JPEG file, the option quality is used, which is a number between 0 and 100 (0 is terrible, 100 is very good).
Parameters:
See also
Sets an image option as an integer.
Parameters:
Sets the colour of the pixels within the given rectangle. This routine performs bounds-checks for the coordinate so it can be considered a safe way to manipulate the data.
Parameters:
Sets the colour of the pixels within the given rectangle. This routine performs bounds-checks for the rectangle so it can be considered a safe way to manipulate the data.
Parameters:
Return a version of the image scaled smaller by the given factors.
Parameters:
Returns:
Returns a resized version of this image without scaling it by adding either a border with the given colour or cropping as necessary.
The image is pasted into a new image with the given size and background colour at the position pos relative to the upper left of the new image. If red = green = blue = -1 then use either the current mask colour if set or find, use, and set a suitable mask colour for any newly exposed areas.
Returns the (modified) image itself.
Parameters:
Returns:
See also