ImageTk Module

The ImageTk module contains support to create and modify TkinterBitmapImage and PhotoImage objects from PIL images.

For examples, see the demo programs in the Scripts directory.

  • class PIL.ImageTk.BitmapImage(image=None, **kw)[源代码]
  • A Tkinter-compatible bitmap image. This can be used everywhere Tkinterexpects an image object.

The given image must have mode “1”. Pixels having value 0 are treated astransparent. Options, if any, are passed on to Tkinter. The most commonlyused option is foreground, which is used to specify the color for thenon-transparent parts. See the Tkinter documentation for information onhow to specify colours.

参数:image – A PIL image.

返回:The height, in pixels.

返回:The width, in pixels.

  • class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)[源代码]
  • A Tkinter-compatible photo image. This can be usedeverywhere Tkinter expects an image object. If the image is an RGBAimage, pixels having alpha 0 are treated as transparent.

The constructor takes either a PIL image, or a mode and a size.Alternatively, you can use the file or data options to initializethe photo image object.

参数:

  • image – Either a PIL image, or a mode string. If a mode string isused, a size must also be given.
  • size – If the first argument is a mode string, this defines the sizeof the image.
  • file – A filename to load the image from (usingImage.open(file)).
  • data – An 8-bit string containing image data (as loaded from animage file).

返回:The height, in pixels.

  • paste(im, box=None)[源代码]
  • Paste a PIL image into the photo image. Note that this canbe very slow if the photo image is displayed.

参数:

  1. - **im** A PIL image. The size must match the target region. If themode does not match, the image is converted to the mode ofthe bitmap image.
  2. - **box** A 4-tuple defining the left, upper, right, and lower pixelcoordinate. See [坐标系统]($679a6bf86e66f272.md#coordinate-system). If None is giveninstead of a tuple, all of the image is assumed.

返回:The width, in pixels.