Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
Pillow (PIL Fork) 12.0.0 documentation
Light LogoDark Logo
Pillow (PIL Fork) 12.0.0 documentation
Back to top

ImageWin module (Windows-only)

TheImageWin 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:

fromPILimportImageWindib=ImageWin.Dib(...)hwnd=ImageWin.HWND(widget.winfo_id())dib.draw(hwnd,xy)
classPIL.ImageWin.Dib(image:Image|str,size:tuple[int,int]|None=None)[source]

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 graylevelsare allocated. For an “RGB” image, a 6x6x6 colour cube is used, togetherwith 20 graylevels.

To make sure that palettes work properly under Windows, you must call thepalette method upon certain events from Windows.

Parameters:
  • 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:int|HDC|HWND,dst:tuple[int,int,int,int],src:tuple[int,int,int,int]|None=None)None[source]

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:int|HDC|HWND)None[source]

Copy the bitmap contents to a device context.

Parameters:

handle – Device context (HDC), cast to a Python integer, or anHDC or HWND instance. In PythonWin, you can useCDC.GetHandleAttrib() to get a suitable handle.

frombytes(buffer:bytes)None[source]

Load display memory contents from byte data.

Parameters:

buffer – A buffer containing display data (usuallydata returned fromtobytes())

paste(im:Image,box:tuple[int,int,int,int]|None=None)None[source]

Paste a PIL image into the bitmap image.

Parameters:
  • 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. SeeCoordinate system. IfNone is given instead of a tuple, all of the image isassumed.

query_palette(handle:int|HDC|HWND)int[source]

Installs the palette associated with the image in the given devicecontext.

This method should be called uponQUERYNEWPALETTE andPALETTECHANGED events from Windows. If this method returns anon-zero value, one or more display palette entries were changed, andthe image should be redrawn.

Parameters:

handle – Device context (HDC), cast to a Python integer, or anHDC or HWND instance.

Returns:

The number of entries that were changed (if one or more entries,this indicates that the image should be redrawn).

tobytes()bytes[source]

Copy display memory contents to bytes object.

Returns:

A bytes object containing display data.

classPIL.ImageWin.HDC(dc:int)[source]

Wraps an HDC integer. The resulting object can be passed to thedraw() andexpose()methods.

classPIL.ImageWin.HWND(wnd:int)[source]

Wraps an HWND integer. The resulting object can be passed to thedraw() andexpose()methods, instead of a DC.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp