ImageWin Module (Windows-only)
The ImageWin
module contains support to create and display images onWindows.
ImageWin can be used with PythonWin and other user interface toolkits thatprovide access to Windows device contexts or window handles. For example,Tkinter makes the window handle available via the winfo_id method:
- from PIL import ImageWin
- dib = ImageWin.Dib(...)
- hwnd = ImageWin.HWND(widget.winfo_id())
- dib.draw(hwnd, xy)
- class
PIL.ImageWin.
Dib
(image, size=None)[源代码] - A Windows bitmap with the given mode and size. The mode can be one of “1”,“L”, “P”, or “RGB”.
If the display requires a palette, this constructor creates a suitablepalette and associates it with the image. For an “L” image, 128 greylevelsare allocated. For an “RGB” image, a 6x6x6 colour cube is used, togetherwith 20 greylevels.
To make sure that palettes work properly under Windows, you must call thepalette method upon certain events from Windows.
参数:
- image – Either a PIL image, or a mode string. If a mode string isused, a size must also be given. The mode can be one of “1”,“L”, “P”, or “RGB”.
- size – If the first argument is a mode string, thisdefines the size of the image.
draw
(handle, dst, src=None)[源代码]- Same as expose, but allows you to specify where to draw the image, andwhat part of it to draw.
The destination and source areas are given as 4-tuple rectangles. Ifthe source is omitted, the entire image is copied. If the source andthe destination have different sizes, the image is resized asnecessary.
expose
(handle)[源代码]- Copy the bitmap contents to a device context.
参数:handle – Device context (HDC), cast to a Python integer, or anHDC or HWND instance. In PythonWin, you can use theCDC.GetHandleAttrib()
to get a suitable handle.
frombytes
(buffer)[源代码]- Load display memory contents from byte data.
参数:buffer – A buffer containing display data (usuallydata returned from tobytes)
paste
(im, box=None)[源代码]- Paste a PIL image into the bitmap image.
参数:
- **im** – A PIL image. The size must match the target region.If the mode does not match, the image is converted to themode of the bitmap image.
- **box** – A 4-tuple defining the left, upper, right, andlower pixel coordinate. See [坐标系统]($679a6bf86e66f272.md#coordinate-system). IfNone is given instead of a tuple, all of the image isassumed.
querypalette
(_handle)[源代码]- Installs the palette associated with the image in the given devicecontext.
This method should be called upon QUERYNEWPALETTE andPALETTECHANGED events from Windows. If this method returns anon-zero value, one or more display palette entries were changed, andthe image should be redrawn.
参数:handle – Device context (HDC), cast to a Python integer, or anHDC or HWND instance.返回:A true value if one or more entries were changed (thisindicates that the image should be redrawn).
tobytes
()[源代码]- Copy display memory contents to bytes object.
返回:A bytes object containing display data.