How do I load images

Image manipulation in wxPython is a dual-headed system where platform-independent image handling is managed by the class wx.Image, while platform-dependent image handling is managed by the class wx.Bitmap. In practice, what this means is that external file formats are loaded and saved by wx.Image, while wx.Bitmap takes care of displaying the image to the screen. Figure 12.1 displays the creation of different images and bitmaps, read in from different file types.

To load an image from a file use the wx.Image constructor:

wx.Image(name, type=wx.BITMAP_TYPE_ANY, index=-1)

Figure 12.1

Big and little images of different types

Figure 12.1

Big and little images of different types

The name parameter is the name of the file to load from and the type is the handler type. The type ID is either wx.BlTMAP_TYPE_ANY, or one of the type flags listed in Table 12.1. If you use wx.bitmap_type_any, then wxPython will attempt to automatically detect the file type. If you use a specific file type, wxPython will convert the file using that type. Listing 12.1 displays how images can be loaded using wx.BITMAP TYPE ANY.

Listing 12.1 Loading and scaling simple images import wx filenames = ["image.bmp", "image.gif", "image.jpg", "image.png" ]

class TestFrame(wx.Frame):

wx.Frame._init_(self, None, title="Loading Images")

fgs = wx.FlexGridSizer(cols=2, hgap=10, vgap=10) for name in filenames:

img1 = wx.Image(name, wx.BITMAP_TYPE_ANY)

Loading images from a file w = img1.GetWidth()

h = img1.GetHeight()

sb1 = wx.StaticBitmap(p, -1, sb2 = wx.StaticBitmap(p, -1, fgs.Add(sb1) fgs.Add(sb2)

C Scaling <-T images wx.BitmapFromImage(img1)) < wx.BitmapFromImage(img2)) Turning images into bitmap widgets d p.SetSizerAndFit(fgs) self.Fit()

app = wx.PySimpleApp() frm = TestFrame() frm.Show() app.MainLoop()

This listing should be rather straightforward. We start with a list of the image names that we want to load in. Looping over them, we first construct an instance of wx.Image, using the any type flag to direct wxPython to figure out the format without bothering us o. Then we scale the image to half size using the image methods ©, and convert the image to a bitmap to be able to add the image to the display ©.

Although this example defers the determination of the image format to wxPython, you can also specify the format explicitly. In the next section we'll show you what image formats are supported by wxPython.

Specifying an image file format

Images are managed with the use of image handlers. An image handler is an instance of wx.ImageHandler that provides a plug-in architecture for managing image formats. Under normal circumstances, you will not need to worry about the details of how image handlers work. All you need to know is that each handler has its own unique wxPython identifier to load a file in the associated format. Supported formats are listed in table 12.1. The type flag is the value used if you want to specify a type when loading an image.

Table 12.1 Supported file formats in wxPython

Handler class

Type flag




Animated cursor format. This handler only loads images, it does not save them.



Windows and OS/2 bitmap format



Windows cursor icon format



Graphic Interchange Format. Due to copyright restrictions, this handler does not save images.



Windows icon format



Interchange file format. This handler only loads images, it does not save them.

Table 12.1 Supported file formats in wxPython (continued)

Handler class

Type flag




Joint Photographic Experts Group format.



PC Paintbrush format. When saving in this format, wxPython counts the number of different colors in the image. If possible, the image is saved as an 8-bit image (that is, if it has 256 or fewer colors). Otherwise, it saves as 24-bit.



Portable Network Graphics format.



Can only load ASCII or raw RGB images. Images saved by this handler are saved as raw RGB.



Tagged Image File Format.



XPixMap format.



Will try to autodetect the format to use and will then invoke the handler for it.

To use a MIME-type to identify the file, rather than a handler type ID, use the function wx.ImageFromMime(name, mimetype, index=-1), where name is the filename, and mimetype is the string with the type of the file. The index parameter denotes the image to load in the case that the image file contains multiple images. This is only used by the GIF, ICO, and TIFF handlers. The default value (-1) means choose the default image, and is interpreted as the first image (index=0) by the GIF and TIFF handler and as the largest and most colorful one by the ICO handler.

Creating image objects wxPython uses other global functions to create different kinds of wx.Image objects. To create an empty image with a specific size, use the function wx.Empty-Image(width, height) —all the pixels in the created image will be black. To create an image from an open stream or Python file-like object, use wx.ImageFrom-Stream(stream, type=wx.BITMAP_TYPE_ANY, index=-1) Sometimes it's useful to create an image from raw RGB data, which can be done using wx.ImageFrom-Data(width, height, data) where data is a string where each set of three consecutive characters represents a pixel's red, green, and blue components. The size of the string should always be width*height*3.

Creating bitmap objects

There are several ways to create a bitmap object. The basic wx.Bitmap constructor is wx.Bitmap(name, type=wx.BITMAP_TYPE_ANY). The name argument is a filename and the type argument can be one of the types specified in the table 12.1. If the bitmap class is able to natively handle the file format then it does so, otherwise the image is automatically loaded via wx.Image and converted to a wx.Bitmap instance.

You can also create an empty bitmap with the method wx.EmptyBitmap(width, height, depth=-1)—the width and height parameters are, of course, the dimensions of the bitmap, and the depth is the color depth of the eventual image. There are two functions that allow you to create a bitmap from raw data. The function wx.BitmapFromBits(bits, width, height, depth=-1) creates a bitmap where the bits parameter is a Python list of bytes. The behavior of this function depends on the platform. On most platforms the bits are either 1 or 0 and the function creates a monochromatic bitmap. On Windows platforms, the data is passed directly to the Windows API function CreateBitmap(). The function wxBitmapFromXPM-Data(listOfStrings) takes as a parameter a list of Python strings, reading the strings as data in XPM format.

You can convert back and forth from an image to a bitmap by using the wx.Bitmap constructor wx.BitmapFromImage(image, depth=-1). The image parameter is the actual wx.Image object, and the depth is the color depth of the resulting bitmap. If the depth is not specified, the current display's color depth is used. You can convert the bitmap back into an image using the function wx.ImageFrom-Bitmap(bitmap), passing an actual wx.Bitmap object. In listing 12.1, bitmap objects are created using the bitmap constructor and then used to construct wx.StaticBitmap widgets, which allows them to be placed into a container widget just like any other wxPython item.

Was this article helpful?

0 -2


  • Basilio
    How to load a picture in wxpython?
    8 years ago
  • georgie
    How to call imagehandler in for loop to display diff image?
    8 years ago
  • Alice
    How to show a gif in wxpython?
    8 years ago
  • leo
    How to scale a bitmap in wx.python?
    8 years ago
  • rose
    How to load .ico image file in wxpython?
    3 years ago
  • cornelia brown
    How to display a speaking picture in "wx python"?
    9 months ago
  • Jesus
    How to display an image with wxpython in python?
    3 months ago
  • smeralda
    How to inset image in wx python?
    2 months ago

Post a comment