We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Notes on Using PIL With PythonWin

April 30, 2003 | Fredrik Lundh

“Does anybody know how to display a JPG file in pythonwin and draw lines over it?”

Here’s a sketch:

When you load the image, do:

    from PIL import Image, ImageWin

    try:
        im = Image.open(filename)
    except IOError:
        # deal with the error
    else:
        # check image size; resize if necessary
        # check the image mode
        if im.mode not in ("1", "L", "RGB"):
            im = im.convert(Image.getmodebase(im.mode))
        dib = ImageWin.Dib(im)
        # store the "dib" variable somewhere

In the OnDraw handler, just do:

    dib.expose(dc.GetHandleAttribute())
    # ... use the dc to draw lines ...

The expose method takes a Windows device context handle, cast to an integer.

Alternatively, you can also use:

    dib.draw(dc.GetHandleAttribute(), (x0, y0, x1, y1))

where (x0, y0, x1, y1) is the coordinates for where you want the image to appear.

(expose is the same thing as draw(dc, (0, 0, width, height))