IdentifiantMot de passe
Mot de passe oublié ?Je m'inscris ! (gratuit)
The ImageWin Module
Python Imaging Library Handbook

Prev   Next

The ImageWin Module

The ImageWin module contains support to create and display images on Windows.

ImageWin can be used with PythonWin and other user interface toolkits that provide access to Windows device contexts or window handles. For example, Tkinter makes the window handle available via the winfo_id method:

    dib = ImageWin.Dib(...)

    hwnd = ImageWin.HWND(widget.winfo_id())
    dib.draw(hwnd, xy)

The Dib Class


ImageWin.Dib(image) => Dib instance

ImageWin.Dib(mode, size) => Dib instance

This constructor creates a Windows bitmap from a PIL image, or from a given mode and size.

The mode can be one of "1", "L", or "RGB".

If the display requires a palette, this constructor creates a suitable palette and associates it with the image. For an "L" image, 128 greylevels are allocated. For an "RGB" image, a 6x6x6 colour cube is used, together with 20 greylevels.

To make sure that palettes work properly under Windows, you must call the palette method upon certain events from Windows. See the method descriptions below.

Helper Classes


Wraps a HDC integer. The resulting object can be passed to the draw and expose methods.


Wraps a HWND integer. The resulting object can be passed to the draw and expose methods, instead of a DC.




Expose (draw) the image using the given device context handle. The handle is an integer representing a Windows HDC handle, or an HDC or HWND object.

In PythonWin, you can use the GetHandleAttrib method of the CDC class to get a suitable handle.


dib.draw(hdc, destination)

dib.draw(hdc, destination, source)

Same as expose, but allows you to specify where to draw the image, and what part of it to draw.

The destination and source areas are given as 4-tuple rectangles. If the source is omitted, the entire image is copied. If the source and the destination have different sizes, the image is resized as necessary.



Installs the palette associated with the image in the given device context. The handle argument is an integer representing a Windows HDC handle.

This method should be called in response to QUERYNEWPALETTE and PALETTECHANGED events from Windows. If this method returns a non-zero value, one or more display palette entries were changed since the last updated, and the image should be redrawn.


dib.paste(image, bbox)

Paste an image into the bitmap image. The bbox argument is a 4-tuple defining the left, upper, right, and lower pixel coordinate. If None is used instead of a tuple, the entire image is copied. In all cases, the size of the pasted image must match the size of the region. If the image mode does not match the bitmap mode, conversions are automatically applied.