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

        im =
    except IOError:
        # deal with the error
        # 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:

    # ... 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))


A Django site. rendered by a django application. hosted by webfaction.