win32 display jpg file

Fredrik Lundh fredrik at pythonware.com
Wed Apr 30 07:49:30 CEST 2003


Mark Hammond wrote:

> > Does anybody knows how to display a JPG file in pythonwin and draw lines
> > over it?
>
> With a fair bit of work :)  You would need to use PIL to decode the jpg
> and convert it to a bitmap.  IIRC, Pythonwin still has a PIL demo in it.

if you cannot find it, here's an outline:

when you load the image:

    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 PAINT handler, just do:

    dib.expose(dc)

where "dc" is a device context for the window, cast to an integer.
according to the PIL docs, "you can use the GetHandleAttrib method
of the CDC class to get a suitable handle."

in 1.1.4, you can pass in ImageWin.HWND(wnd) instead of the dc,
where wnd is a window handle cast to an integer.

in all versions, you can also use

    dib.draw(dc, (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))

</F>








More information about the Python-list mailing list