home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / python / Lib / site-packages / PIL / Image.py < prev    next >
Encoding:
Python Source  |  2010-03-06  |  66.3 KB  |  2,094 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: Image.py 2933 2006-12-03 12:08:22Z fredrik $
  4. #
  5. # the Image class wrapper
  6. #
  7. # partial release history:
  8. # 1995-09-09 fl   Created
  9. # 1996-03-11 fl   PIL release 0.0 (proof of concept)
  10. # 1996-04-30 fl   PIL release 0.1b1
  11. # 1999-07-28 fl   PIL release 1.0 final
  12. # 2000-06-07 fl   PIL release 1.1
  13. # 2000-10-20 fl   PIL release 1.1.1
  14. # 2001-05-07 fl   PIL release 1.1.2
  15. # 2002-03-15 fl   PIL release 1.1.3
  16. # 2003-05-10 fl   PIL release 1.1.4
  17. # 2005-03-28 fl   PIL release 1.1.5
  18. # 2006-12-02 fl   PIL release 1.1.6
  19. #
  20. # Copyright (c) 1997-2006 by Secret Labs AB.  All rights reserved.
  21. # Copyright (c) 1995-2006 by Fredrik Lundh.
  22. #
  23. # See the README file for information on usage and redistribution.
  24. #
  25.  
  26. VERSION = "1.1.6"
  27.  
  28. try:
  29.     import warnings
  30. except ImportError:
  31.     warnings = None
  32.  
  33. class _imaging_not_installed:
  34.     # module placeholder
  35.     def __getattr__(self, id):
  36.         raise ImportError("The _imaging C module is not installed")
  37.  
  38. try:
  39.     # give Tk a chance to set up the environment, in case we're
  40.     # using an _imaging module linked against libtcl/libtk (use
  41.     # __import__ to hide this from naive packagers; we don't really
  42.     # depend on Tk unless ImageTk is used, and that module already
  43.     # imports Tkinter)
  44.     __import__("FixTk")
  45. except ImportError:
  46.     pass
  47.  
  48. try:
  49.     # If the _imaging C module is not present, you can still use
  50.     # the "open" function to identify files, but you cannot load
  51.     # them.  Note that other modules should not refer to _imaging
  52.     # directly; import Image and use the Image.core variable instead.
  53.     import _imaging
  54.     core = _imaging
  55.     del _imaging
  56. except ImportError, v:
  57.     core = _imaging_not_installed()
  58.     if str(v)[:20] == "Module use of python" and warnings:
  59.         # The _imaging C module is present, but not compiled for
  60.         # the right version (windows only).  Print a warning, if
  61.         # possible.
  62.         warnings.warn(
  63.             "The _imaging extension was built for another version "
  64.             "of Python; most PIL functions will be disabled",
  65.             RuntimeWarning
  66.             )
  67.  
  68. import ImageMode
  69. import ImagePalette
  70.  
  71. import os, string, sys
  72.  
  73. # type stuff
  74. from types import IntType, StringType, TupleType
  75.  
  76. try:
  77.     UnicodeStringType = type(unicode(""))
  78.     ##
  79.     # (Internal) Checks if an object is a string.  If the current
  80.     # Python version supports Unicode, this checks for both 8-bit
  81.     # and Unicode strings.
  82.     def isStringType(t):
  83.         return isinstance(t, StringType) or isinstance(t, UnicodeStringType)
  84. except NameError:
  85.     def isStringType(t):
  86.         return isinstance(t, StringType)
  87.  
  88. ##
  89. # (Internal) Checks if an object is a tuple.
  90.  
  91. def isTupleType(t):
  92.     return isinstance(t, TupleType)
  93.  
  94. ##
  95. # (Internal) Checks if an object is an image object.
  96.  
  97. def isImageType(t):
  98.     return hasattr(t, "im")
  99.  
  100. ##
  101. # (Internal) Checks if an object is a string, and that it points to a
  102. # directory.
  103.  
  104. def isDirectory(f):
  105.     return isStringType(f) and os.path.isdir(f)
  106.  
  107. from operator import isNumberType, isSequenceType
  108.  
  109. #
  110. # Debug level
  111.  
  112. DEBUG = 0
  113.  
  114. #
  115. # Constants (also defined in _imagingmodule.c!)
  116.  
  117. NONE = 0
  118.  
  119. # transpose
  120. FLIP_LEFT_RIGHT = 0
  121. FLIP_TOP_BOTTOM = 1
  122. ROTATE_90 = 2
  123. ROTATE_180 = 3
  124. ROTATE_270 = 4
  125.  
  126. # transforms
  127. AFFINE = 0
  128. EXTENT = 1
  129. PERSPECTIVE = 2
  130. QUAD = 3
  131. MESH = 4
  132.  
  133. # resampling filters
  134. NONE = 0
  135. NEAREST = 0
  136. ANTIALIAS = 1 # 3-lobed lanczos
  137. LINEAR = BILINEAR = 2
  138. CUBIC = BICUBIC = 3
  139.  
  140. # dithers
  141. NONE = 0
  142. NEAREST = 0
  143. ORDERED = 1 # Not yet implemented
  144. RASTERIZE = 2 # Not yet implemented
  145. FLOYDSTEINBERG = 3 # default
  146.  
  147. # palettes/quantizers
  148. WEB = 0
  149. ADAPTIVE = 1
  150.  
  151. # categories
  152. NORMAL = 0
  153. SEQUENCE = 1
  154. CONTAINER = 2
  155.  
  156. # --------------------------------------------------------------------
  157. # Registries
  158.  
  159. ID = []
  160. OPEN = {}
  161. MIME = {}
  162. SAVE = {}
  163. EXTENSION = {}
  164.  
  165. # --------------------------------------------------------------------
  166. # Modes supported by this version
  167.  
  168. _MODEINFO = {
  169.     # NOTE: this table will be removed in future versions.  use
  170.     # getmode* functions or ImageMode descriptors instead.
  171.  
  172.     # official modes
  173.     "1": ("L", "L", ("1",)),
  174.     "L": ("L", "L", ("L",)),
  175.     "I": ("L", "I", ("I",)),
  176.     "F": ("L", "F", ("F",)),
  177.     "P": ("RGB", "L", ("P",)),
  178.     "RGB": ("RGB", "L", ("R", "G", "B")),
  179.     "RGBX": ("RGB", "L", ("R", "G", "B", "X")),
  180.     "RGBA": ("RGB", "L", ("R", "G", "B", "A")),
  181.     "CMYK": ("RGB", "L", ("C", "M", "Y", "K")),
  182.     "YCbCr": ("RGB", "L", ("Y", "Cb", "Cr")),
  183.  
  184.     # Experimental modes include I;16, I;16B, RGBa, BGR;15,
  185.     # and BGR;24.  Use these modes only if you know exactly
  186.     # what you're doing...
  187.  
  188. }
  189.  
  190. if sys.byteorder == 'little':
  191.     _ENDIAN = '<'
  192. else:
  193.     _ENDIAN = '>'
  194.  
  195. _MODE_CONV = {
  196.     # official modes
  197.     "1": ('|b1', None),
  198.     "L": ('|u1', None),
  199.     "I": ('%si4' % _ENDIAN, None), # FIXME: is this correct?
  200.     "F": ('%sf4' % _ENDIAN, None), # FIXME: is this correct?
  201.     "P": ('|u1', None),
  202.     "RGB": ('|u1', 3),
  203.     "RGBX": ('|u1', 4),
  204.     "RGBA": ('|u1', 4),
  205.     "CMYK": ('|u1', 4),
  206.     "YCbCr": ('|u1', 4),
  207. }
  208.  
  209. def _conv_type_shape(im):
  210.     shape = im.size[::-1]
  211.     typ, extra = _MODE_CONV[im.mode]
  212.     if extra is None:
  213.         return shape, typ
  214.     else:
  215.         return shape+(extra,), typ
  216.  
  217.  
  218. MODES = _MODEINFO.keys()
  219. MODES.sort()
  220.  
  221. # raw modes that may be memory mapped.  NOTE: if you change this, you
  222. # may have to modify the stride calculation in map.c too!
  223. _MAPMODES = ("L", "P", "RGBX", "RGBA", "CMYK", "I;16", "I;16B")
  224.  
  225. ##
  226. # Gets the "base" mode for given mode.  This function returns "L" for
  227. # images that contain grayscale data, and "RGB" for images that
  228. # contain color data.
  229. #
  230. # @param mode Input mode.
  231. # @return "L" or "RGB".
  232. # @exception KeyError If the input mode was not a standard mode.
  233.  
  234. def getmodebase(mode):
  235.     return ImageMode.getmode(mode).basemode
  236.  
  237. ##
  238. # Gets the storage type mode.  Given a mode, this function returns a
  239. # single-layer mode suitable for storing individual bands.
  240. #
  241. # @param mode Input mode.
  242. # @return "L", "I", or "F".
  243. # @exception KeyError If the input mode was not a standard mode.
  244.  
  245. def getmodetype(mode):
  246.     return ImageMode.getmode(mode).basetype
  247.  
  248. ##
  249. # Gets a list of individual band names.  Given a mode, this function
  250. # returns a tuple containing the names of individual bands (use
  251. # {@link #getmodetype} to get the mode used to store each individual
  252. # band.
  253. #
  254. # @param mode Input mode.
  255. # @return A tuple containing band names.  The length of the tuple
  256. #     gives the number of bands in an image of the given mode.
  257. # @exception KeyError If the input mode was not a standard mode.
  258.  
  259. def getmodebandnames(mode):
  260.     return ImageMode.getmode(mode).bands
  261.  
  262. ##
  263. # Gets the number of individual bands for this mode.
  264. #
  265. # @param mode Input mode.
  266. # @return The number of bands in this mode.
  267. # @exception KeyError If the input mode was not a standard mode.
  268.  
  269. def getmodebands(mode):
  270.     return len(ImageMode.getmode(mode).bands)
  271.  
  272. # --------------------------------------------------------------------
  273. # Helpers
  274.  
  275. _initialized = 0
  276.  
  277. ##
  278. # Explicitly loads standard file format drivers.
  279.  
  280. def preinit():
  281.     "Load standard file format drivers."
  282.  
  283.     global _initialized
  284.     if _initialized >= 1:
  285.         return
  286.  
  287.     try:
  288.         import BmpImagePlugin
  289.     except ImportError:
  290.         pass
  291.     try:
  292.         import GifImagePlugin
  293.     except ImportError:
  294.         pass
  295.     try:
  296.         import JpegImagePlugin
  297.     except ImportError:
  298.         pass
  299.     try:
  300.         import PpmImagePlugin
  301.     except ImportError:
  302.         pass
  303.     try:
  304.         import PngImagePlugin
  305.     except ImportError:
  306.         pass
  307. #   try:
  308. #       import TiffImagePlugin
  309. #   except ImportError:
  310. #       pass
  311.  
  312.     _initialized = 1
  313.  
  314. ##
  315. # Explicitly initializes the Python Imaging Library.  This function
  316. # loads all available file format drivers.
  317.  
  318. def init():
  319.     "Load all file format drivers."
  320.  
  321.     global _initialized
  322.     if _initialized >= 2:
  323.         return
  324.  
  325.     visited = {}
  326.  
  327.     directories = sys.path
  328.  
  329.     try:
  330.         directories = directories + [os.path.dirname(__file__)]
  331.     except NameError:
  332.         pass
  333.  
  334.     # only check directories (including current, if present in the path)
  335.     for directory in filter(isDirectory, directories):
  336.         fullpath = os.path.abspath(directory)
  337.         if visited.has_key(fullpath):
  338.             continue
  339.         for file in os.listdir(directory):
  340.             if file[-14:] == "ImagePlugin.py":
  341.                 f, e = os.path.splitext(file)
  342.                 try:
  343.                     sys.path.insert(0, directory)
  344.                     try:
  345.                         __import__(f, globals(), locals(), [])
  346.                     finally:
  347.                         del sys.path[0]
  348.                 except ImportError:
  349.                     if DEBUG:
  350.                         print "Image: failed to import",
  351.                         print f, ":", sys.exc_value
  352.         visited[fullpath] = None
  353.  
  354.     if OPEN or SAVE:
  355.         _initialized = 2
  356.  
  357.  
  358. # --------------------------------------------------------------------
  359. # Codec factories (used by tostring/fromstring and ImageFile.load)
  360.  
  361. def _getdecoder(mode, decoder_name, args, extra=()):
  362.  
  363.     # tweak arguments
  364.     if args is None:
  365.         args = ()
  366.     elif not isTupleType(args):
  367.         args = (args,)
  368.  
  369.     try:
  370.         # get decoder
  371.         decoder = getattr(core, decoder_name + "_decoder")
  372.         # print decoder, (mode,) + args + extra
  373.         return apply(decoder, (mode,) + args + extra)
  374.     except AttributeError:
  375.         raise IOError("decoder %s not available" % decoder_name)
  376.  
  377. def _getencoder(mode, encoder_name, args, extra=()):
  378.  
  379.     # tweak arguments
  380.     if args is None:
  381.         args = ()
  382.     elif not isTupleType(args):
  383.         args = (args,)
  384.  
  385.     try:
  386.         # get encoder
  387.         encoder = getattr(core, encoder_name + "_encoder")
  388.         # print encoder, (mode,) + args + extra
  389.         return apply(encoder, (mode,) + args + extra)
  390.     except AttributeError:
  391.         raise IOError("encoder %s not available" % encoder_name)
  392.  
  393.  
  394. # --------------------------------------------------------------------
  395. # Simple expression analyzer
  396.  
  397. class _E:
  398.     def __init__(self, data): self.data = data
  399.     def __coerce__(self, other): return self, _E(other)
  400.     def __add__(self, other): return _E((self.data, "__add__", other.data))
  401.     def __mul__(self, other): return _E((self.data, "__mul__", other.data))
  402.  
  403. def _getscaleoffset(expr):
  404.     stub = ["stub"]
  405.     data = expr(_E(stub)).data
  406.     try:
  407.         (a, b, c) = data # simplified syntax
  408.         if (a is stub and b == "__mul__" and isNumberType(c)):
  409.             return c, 0.0
  410.         if (a is stub and b == "__add__" and isNumberType(c)):
  411.             return 1.0, c
  412.     except TypeError: pass
  413.     try:
  414.         ((a, b, c), d, e) = data # full syntax
  415.         if (a is stub and b == "__mul__" and isNumberType(c) and
  416.             d == "__add__" and isNumberType(e)):
  417.             return c, e
  418.     except TypeError: pass
  419.     raise ValueError("illegal expression")
  420.  
  421.  
  422. # --------------------------------------------------------------------
  423. # Implementation wrapper
  424.  
  425. ##
  426. # This class represents an image object.  To create Image objects, use
  427. # the appropriate factory functions.  There's hardly ever any reason
  428. # to call the Image constructor directly.
  429. #
  430. # @see #open
  431. # @see #new
  432. # @see #fromstring
  433.  
  434. class Image:
  435.  
  436.     format = None
  437.     format_description = None
  438.  
  439.     def __init__(self):
  440.         self.im = None
  441.         self.mode = ""
  442.         self.size = (0, 0)
  443.         self.palette = None
  444.         self.info = {}
  445.         self.category = NORMAL
  446.         self.readonly = 0
  447.  
  448.     def _new(self, im):
  449.         new = Image()
  450.         new.im = im
  451.         new.mode = im.mode
  452.         new.size = im.size
  453.         new.palette = self.palette
  454.         if im.mode == "P":
  455.             new.palette = ImagePalette.ImagePalette()
  456.         try:
  457.             new.info = self.info.copy()
  458.         except AttributeError:
  459.             # fallback (pre-1.5.2)
  460.             new.info = {}
  461.             for k, v in self.info:
  462.                 new.info[k] = v
  463.         return new
  464.  
  465.     _makeself = _new # compatibility
  466.  
  467.     def _copy(self):
  468.         self.load()
  469.         self.im = self.im.copy()
  470.         self.readonly = 0
  471.  
  472.     def _dump(self, file=None, format=None):
  473.         import tempfile
  474.         if not file:
  475.             file = tempfile.mktemp()
  476.         self.load()
  477.         if not format or format == "PPM":
  478.             self.im.save_ppm(file)
  479.         else:
  480.             file = file + "." + format
  481.             self.save(file, format)
  482.         return file
  483.  
  484.     def __getattr__(self, name):
  485.         if name == "__array_interface__":
  486.             # numpy array interface support
  487.             new = {}
  488.             shape, typestr = _conv_type_shape(self)
  489.             new['shape'] = shape
  490.             new['typestr'] = typestr
  491.             new['data'] = self.tostring()
  492.             return new
  493.         raise AttributeError(name)
  494.  
  495.     ##
  496.     # Returns a string containing pixel data.
  497.     #
  498.     # @param encoder_name What encoder to use.  The default is to
  499.     #    use the standard "raw" encoder.
  500.     # @param *args Extra arguments to the encoder.
  501.     # @return An 8-bit string.
  502.  
  503.     def tostring(self, encoder_name="raw", *args):
  504.         "Return image as a binary string"
  505.  
  506.         # may pass tuple instead of argument list
  507.         if len(args) == 1 and isTupleType(args[0]):
  508.             args = args[0]
  509.  
  510.         if encoder_name == "raw" and args == ():
  511.             args = self.mode
  512.  
  513.         self.load()
  514.  
  515.         # unpack data
  516.         e = _getencoder(self.mode, encoder_name, args)
  517.         e.setimage(self.im)
  518.  
  519.         bufsize = max(65536, self.size[0] * 4) # see RawEncode.c
  520.  
  521.         data = []
  522.         while 1:
  523.             l, s, d = e.encode(bufsize)
  524.             data.append(d)
  525.             if s:
  526.                 break
  527.         if s < 0:
  528.             raise RuntimeError("encoder error %d in tostring" % s)
  529.  
  530.         return string.join(data, "")
  531.  
  532.     ##
  533.     # Returns the image converted to an X11 bitmap.  This method
  534.     # only works for mode "1" images.
  535.     #
  536.     # @param name The name prefix to use for the bitmap variables.
  537.     # @return A string containing an X11 bitmap.
  538.     # @exception ValueError If the mode is not "1"
  539.  
  540.     def tobitmap(self, name="image"):
  541.         "Return image as an XBM bitmap"
  542.  
  543.         self.load()
  544.         if self.mode != "1":
  545.             raise ValueError("not a bitmap")
  546.         data = self.tostring("xbm")
  547.         return string.join(["#define %s_width %d\n" % (name, self.size[0]),
  548.                 "#define %s_height %d\n"% (name, self.size[1]),
  549.                 "static char %s_bits[] = {\n" % name, data, "};"], "")
  550.  
  551.     ##
  552.     # Loads this image with pixel data from a string.
  553.     # <p>
  554.     # This method is similar to the {@link #fromstring} function, but
  555.     # loads data into this image instead of creating a new image
  556.     # object.
  557.  
  558.     def fromstring(self, data, decoder_name="raw", *args):
  559.         "Load data to image from binary string"
  560.  
  561.         # may pass tuple instead of argument list
  562.         if len(args) == 1 and isTupleType(args[0]):
  563.             args = args[0]
  564.  
  565.         # default format
  566.         if decoder_name == "raw" and args == ():
  567.             args = self.mode
  568.  
  569.         # unpack data
  570.         d = _getdecoder(self.mode, decoder_name, args)
  571.         d.setimage(self.im)
  572.         s = d.decode(data)
  573.  
  574.         if s[0] >= 0:
  575.             raise ValueError("not enough image data")
  576.         if s[1] != 0:
  577.             raise ValueError("cannot decode image data")
  578.  
  579.     ##
  580.     # Allocates storage for the image and loads the pixel data.  In
  581.     # normal cases, you don't need to call this method, since the
  582.     # Image class automatically loads an opened image when it is
  583.     # accessed for the first time.
  584.     #
  585.     # @return An image access object.
  586.  
  587.     def load(self):
  588.         "Explicitly load pixel data."
  589.         if self.im and self.palette and self.palette.dirty:
  590.             # realize palette
  591.             apply(self.im.putpalette, self.palette.getdata())
  592.             self.palette.dirty = 0
  593.             self.palette.mode = "RGB"
  594.             self.palette.rawmode = None
  595.             if self.info.has_key("transparency"):
  596.                 self.im.putpalettealpha(self.info["transparency"], 0)
  597.                 self.palette.mode = "RGBA"
  598.         if self.im:
  599.             return self.im.pixel_access(self.readonly)
  600.  
  601.     ##
  602.     # Verifies the contents of a file. For data read from a file, this
  603.     # method attempts to determine if the file is broken, without
  604.     # actually decoding the image data.  If this method finds any
  605.     # problems, it raises suitable exceptions.  If you need to load
  606.     # the image after using this method, you must reopen the image
  607.     # file.
  608.  
  609.     def verify(self):
  610.         "Verify file contents."
  611.         pass
  612.  
  613.  
  614.     ##
  615.     # Returns a converted copy of this image. For the "P" mode, this
  616.     # method translates pixels through the palette.  If mode is
  617.     # omitted, a mode is chosen so that all information in the image
  618.     # and the palette can be represented without a palette.
  619.     # <p>
  620.     # The current version supports all possible conversions between
  621.     # "L", "RGB" and "CMYK."
  622.     # <p>
  623.     # When translating a colour image to black and white (mode "L"),
  624.     # the library uses the ITU-R 601-2 luma transform:
  625.     # <p>
  626.     # <b>L = R * 299/1000 + G * 587/1000 + B * 114/1000</b>
  627.     # <p>
  628.     # When translating a greyscale image into a bilevel image (mode
  629.     # "1"), all non-zero values are set to 255 (white). To use other
  630.     # thresholds, use the {@link #Image.point} method.
  631.     #
  632.     # @def convert(mode, matrix=None)
  633.     # @param mode The requested mode.
  634.     # @param matrix An optional conversion matrix.  If given, this
  635.     #    should be 4- or 16-tuple containing floating point values.
  636.     # @return An Image object.
  637.  
  638.     def convert(self, mode=None, data=None, dither=None,
  639.                 palette=WEB, colors=256):
  640.         "Convert to other pixel format"
  641.  
  642.         if not mode:
  643.             # determine default mode
  644.             if self.mode == "P":
  645.                 self.load()
  646.                 if self.palette:
  647.                     mode = self.palette.mode
  648.                 else:
  649.                     mode = "RGB"
  650.             else:
  651.                 return self.copy()
  652.  
  653.         self.load()
  654.  
  655.         if data:
  656.             # matrix conversion
  657.             if mode not in ("L", "RGB"):
  658.                 raise ValueError("illegal conversion")
  659.             im = self.im.convert_matrix(mode, data)
  660.             return self._new(im)
  661.  
  662.         if mode == "P" and palette == ADAPTIVE:
  663.             im = self.im.quantize(colors)
  664.             return self._new(im)
  665.  
  666.         # colourspace conversion
  667.         if dither is None:
  668.             dither = FLOYDSTEINBERG
  669.  
  670.         try:
  671.             im = self.im.convert(mode, dither)
  672.         except ValueError:
  673.             try:
  674.                 # normalize source image and try again
  675.                 im = self.im.convert(getmodebase(self.mode))
  676.                 im = im.convert(mode, dither)
  677.             except KeyError:
  678.                 raise ValueError("illegal conversion")
  679.  
  680.         return self._new(im)
  681.  
  682.     def quantize(self, colors=256, method=0, kmeans=0, palette=None):
  683.  
  684.         # methods:
  685.         #    0 = median cut
  686.         #    1 = maximum coverage
  687.  
  688.         # NOTE: this functionality will be moved to the extended
  689.         # quantizer interface in a later version of PIL.
  690.  
  691.         self.load()
  692.  
  693.         if palette:
  694.             # use palette from reference image
  695.             palette.load()
  696.             if palette.mode != "P":
  697.                 raise ValueError("bad mode for palette image")
  698.             if self.mode != "RGB" and self.mode != "L":
  699.                 raise ValueError(
  700.                     "only RGB or L mode images can be quantized to a palette"
  701.                     )
  702.             im = self.im.convert("P", 1, palette.im)
  703.             return self._makeself(im)
  704.  
  705.         im = self.im.quantize(colors, method, kmeans)
  706.         return self._new(im)
  707.  
  708.     ##
  709.     # Copies this image. Use this method if you wish to paste things
  710.     # into an image, but still retain the original.
  711.     #
  712.     # @return An Image object.
  713.  
  714.     def copy(self):
  715.         "Copy raster data"
  716.  
  717.         self.load()
  718.         im = self.im.copy()
  719.         return self._new(im)
  720.  
  721.     ##
  722.     # Returns a rectangular region from this image. The box is a
  723.     # 4-tuple defining the left, upper, right, and lower pixel
  724.     # coordinate.
  725.     # <p>
  726.     # This is a lazy operation.  Changes to the source image may or
  727.     # may not be reflected in the cropped image.  To break the
  728.     # connection, call the {@link #Image.load} method on the cropped
  729.     # copy.
  730.     #
  731.     # @param The crop rectangle, as a (left, upper, right, lower)-tuple.
  732.     # @return An Image object.
  733.  
  734.     def crop(self, box=None):
  735.         "Crop region from image"
  736.  
  737.         self.load()
  738.         if box is None:
  739.             return self.copy()
  740.  
  741.         # lazy operation
  742.         return _ImageCrop(self, box)
  743.  
  744.     ##
  745.     # Configures the image file loader so it returns a version of the
  746.     # image that as closely as possible matches the given mode and
  747.     # size.  For example, you can use this method to convert a colour
  748.     # JPEG to greyscale while loading it, or to extract a 128x192
  749.     # version from a PCD file.
  750.     # <p>
  751.     # Note that this method modifies the Image object in place.  If
  752.     # the image has already been loaded, this method has no effect.
  753.     #
  754.     # @param mode The requested mode.
  755.     # @param size The requested size.
  756.  
  757.     def draft(self, mode, size):
  758.         "Configure image decoder"
  759.  
  760.         pass
  761.  
  762.     def _expand(self, xmargin, ymargin=None):
  763.         if ymargin is None:
  764.             ymargin = xmargin
  765.         self.load()
  766.         return self._new(self.im.expand(xmargin, ymargin, 0))
  767.  
  768.     ##
  769.     # Filters this image using the given filter.  For a list of
  770.     # available filters, see the <b>ImageFilter</b> module.
  771.     #
  772.     # @param filter Filter kernel.
  773.     # @return An Image object.
  774.     # @see ImageFilter
  775.  
  776.     def filter(self, filter):
  777.         "Apply environment filter to image"
  778.  
  779.         self.load()
  780.  
  781.         from ImageFilter import Filter
  782.         if not isinstance(filter, Filter):
  783.             filter = filter()
  784.  
  785.         if self.im.bands == 1:
  786.             return self._new(filter.filter(self.im))
  787.         # fix to handle multiband images since _imaging doesn't
  788.         ims = []
  789.         for c in range(self.im.bands):
  790.             ims.append(self._new(filter.filter(self.im.getband(c))))
  791.         return merge(self.mode, ims)
  792.  
  793.     ##
  794.     # Returns a tuple containing the name of each band in this image.
  795.     # For example, <b>getbands</b> on an RGB image returns ("R", "G", "B").
  796.     #
  797.     # @return A tuple containing band names.
  798.  
  799.     def getbands(self):
  800.         "Get band names"
  801.  
  802.         return ImageMode.getmode(self.mode).bands
  803.  
  804.     ##
  805.     # Calculates the bounding box of the non-zero regions in the
  806.     # image.
  807.     #
  808.     # @return The bounding box is returned as a 4-tuple defining the
  809.     #    left, upper, right, and lower pixel coordinate. If the image
  810.     #    is completely empty, this method returns None.
  811.  
  812.     def getbbox(self):
  813.         "Get bounding box of actual data (non-zero pixels) in image"
  814.  
  815.         self.load()
  816.         return self.im.getbbox()
  817.  
  818.     ##
  819.     # Returns a list of colors used in this image.
  820.     #
  821.     # @param maxcolors Maximum number of colors.  If this number is
  822.     #    exceeded, this method returns None.  The default limit is
  823.     #    256 colors.
  824.     # @return An unsorted list of (count, pixel) values.
  825.  
  826.     def getcolors(self, maxcolors=256):
  827.         "Get colors from image, up to given limit"
  828.  
  829.         self.load()
  830.         if self.mode in ("1", "L", "P"):
  831.             h = self.im.histogram()
  832.             out = []
  833.             for i in range(256):
  834.                 if h[i]:
  835.                     out.append((h[i], i))
  836.             if len(out) > maxcolors:
  837.                 return None
  838.             return out
  839.         return self.im.getcolors(maxcolors)
  840.  
  841.     ##
  842.     # Returns the contents of this image as a sequence object
  843.     # containing pixel values.  The sequence object is flattened, so
  844.     # that values for line one follow directly after the values of
  845.     # line zero, and so on.
  846.     # <p>
  847.     # Note that the sequence object returned by this method is an
  848.     # internal PIL data type, which only supports certain sequence
  849.     # operations.  To convert it to an ordinary sequence (e.g. for
  850.     # printing), use <b>list(im.getdata())</b>.
  851.     #
  852.     # @param band What band to return.  The default is to return
  853.     #    all bands.  To return a single band, pass in the index
  854.     #    value (e.g. 0 to get the "R" band from an "RGB" image).
  855.     # @return A sequence-like object.
  856.  
  857.     def getdata(self, band = None):
  858.         "Get image data as sequence object."
  859.  
  860.         self.load()
  861.         if band is not None:
  862.             return self.im.getband(band)
  863.         return self.im # could be abused
  864.  
  865.     ##
  866.     # Gets the the minimum and maximum pixel values for each band in
  867.     # the image.
  868.     #
  869.     # @return For a single-band image, a 2-tuple containing the
  870.     #    minimum and maximum pixel value.  For a multi-band image,
  871.     #    a tuple containing one 2-tuple for each band.
  872.  
  873.     def getextrema(self):
  874.         "Get min/max value"
  875.  
  876.         self.load()
  877.         if self.im.bands > 1:
  878.             extrema = []
  879.             for i in range(self.im.bands):
  880.                 extrema.append(self.im.getband(i).getextrema())
  881.             return tuple(extrema)
  882.         return self.im.getextrema()
  883.  
  884.     ##
  885.     # Returns a PyCObject that points to the internal image memory.
  886.     #
  887.     # @return A PyCObject object.
  888.  
  889.     def getim(self):
  890.         "Get PyCObject pointer to internal image memory"
  891.  
  892.         self.load()
  893.         return self.im.ptr
  894.  
  895.  
  896.     ##
  897.     # Returns the image palette as a list.
  898.     #
  899.     # @return A list of color values [r, g, b, ...], or None if the
  900.     #    image has no palette.
  901.  
  902.     def getpalette(self):
  903.         "Get palette contents."
  904.  
  905.         self.load()
  906.         try:
  907.             return map(ord, self.im.getpalette())
  908.         except ValueError:
  909.             return None # no palette
  910.  
  911.  
  912.     ##
  913.     # Returns the pixel value at a given position.
  914.     #
  915.     # @param xy The coordinate, given as (x, y).
  916.     # @return The pixel value.  If the image is a multi-layer image,
  917.     #    this method returns a tuple.
  918.  
  919.     def getpixel(self, xy):
  920.         "Get pixel value"
  921.  
  922.         self.load()
  923.         return self.im.getpixel(xy)
  924.  
  925.     ##
  926.     # Returns the horizontal and vertical projection.
  927.     #
  928.     # @return Two sequences, indicating where there are non-zero
  929.     #     pixels along the X-axis and the Y-axis, respectively.
  930.  
  931.     def getprojection(self):
  932.         "Get projection to x and y axes"
  933.  
  934.         self.load()
  935.         x, y = self.im.getprojection()
  936.         return map(ord, x), map(ord, y)
  937.  
  938.     ##
  939.     # Returns a histogram for the image. The histogram is returned as
  940.     # a list of pixel counts, one for each pixel value in the source
  941.     # image. If the image has more than one band, the histograms for
  942.     # all bands are concatenated (for example, the histogram for an
  943.     # "RGB" image contains 768 values).
  944.     # <p>
  945.     # A bilevel image (mode "1") is treated as a greyscale ("L") image
  946.     # by this method.
  947.     # <p>
  948.     # If a mask is provided, the method returns a histogram for those
  949.     # parts of the image where the mask image is non-zero. The mask
  950.     # image must have the same size as the image, and be either a
  951.     # bi-level image (mode "1") or a greyscale image ("L").
  952.     #
  953.     # @def histogram(mask=None)
  954.     # @param mask An optional mask.
  955.     # @return A list containing pixel counts.
  956.  
  957.     def histogram(self, mask=None, extrema=None):
  958.         "Take histogram of image"
  959.  
  960.         self.load()
  961.         if mask:
  962.             mask.load()
  963.             return self.im.histogram((0, 0), mask.im)
  964.         if self.mode in ("I", "F"):
  965.             if extrema is None:
  966.                 extrema = self.getextrema()
  967.             return self.im.histogram(extrema)
  968.         return self.im.histogram()
  969.  
  970.     ##
  971.     # (Deprecated) Returns a copy of the image where the data has been
  972.     # offset by the given distances. Data wraps around the edges. If
  973.     # yoffset is omitted, it is assumed to be equal to xoffset.
  974.     # <p>
  975.     # This method is deprecated. New code should use the <b>offset</b>
  976.     # function in the <b>ImageChops</b> module.
  977.     #
  978.     # @param xoffset The horizontal distance.
  979.     # @param yoffset The vertical distance.  If omitted, both
  980.     #    distances are set to the same value.
  981.     # @return An Image object.
  982.  
  983.     def offset(self, xoffset, yoffset=None):
  984.         "(deprecated) Offset image in horizontal and/or vertical direction"
  985.         if warnings:
  986.             warnings.warn(
  987.                 "'offset' is deprecated; use 'ImageChops.offset' instead",
  988.                 DeprecationWarning, stacklevel=2
  989.                 )
  990.         import ImageChops
  991.         return ImageChops.offset(self, xoffset, yoffset)
  992.  
  993.     ##
  994.     # Pastes another image into this image. The box argument is either
  995.     # a 2-tuple giving the upper left corner, a 4-tuple defining the
  996.     # left, upper, right, and lower pixel coordinate, or None (same as
  997.     # (0, 0)).  If a 4-tuple is given, the size of the pasted image
  998.     # must match the size of the region.
  999.     # <p>
  1000.     # If the modes don't match, the pasted image is converted to the
  1001.     # mode of this image (see the {@link #Image.convert} method for
  1002.     # details).
  1003.     # <p>
  1004.     # Instead of an image, the source can be a integer or tuple
  1005.     # containing pixel values.  The method then fills the region
  1006.     # with the given colour.  When creating RGB images, you can
  1007.     # also use colour strings as supported by the ImageColor module.
  1008.     # <p>
  1009.     # If a mask is given, this method updates only the regions
  1010.     # indicated by the mask.  You can use either "1", "L" or "RGBA"
  1011.     # images (in the latter case, the alpha band is used as mask).
  1012.     # Where the mask is 255, the given image is copied as is.  Where
  1013.     # the mask is 0, the current value is preserved.  Intermediate
  1014.     # values can be used for transparency effects.
  1015.     # <p>
  1016.     # Note that if you paste an "RGBA" image, the alpha band is
  1017.     # ignored.  You can work around this by using the same image as
  1018.     # both source image and mask.
  1019.     #
  1020.     # @param im Source image or pixel value (integer or tuple).
  1021.     # @param box An optional 4-tuple giving the region to paste into.
  1022.     #    If a 2-tuple is used instead, it's treated as the upper left
  1023.     #    corner.  If omitted or None, the source is pasted into the
  1024.     #    upper left corner.
  1025.     #    <p>
  1026.     #    If an image is given as the second argument and there is no
  1027.     #    third, the box defaults to (0, 0), and the second argument
  1028.     #    is interpreted as a mask image.
  1029.     # @param mask An optional mask image.
  1030.     # @return An Image object.
  1031.  
  1032.     def paste(self, im, box=None, mask=None):
  1033.         "Paste other image into region"
  1034.  
  1035.         if isImageType(box) and mask is None:
  1036.             # abbreviated paste(im, mask) syntax
  1037.             mask = box; box = None
  1038.  
  1039.         if box is None:
  1040.             # cover all of self
  1041.             box = (0, 0) + self.size
  1042.  
  1043.         if len(box) == 2:
  1044.             # lower left corner given; get size from image or mask
  1045.             if isImageType(im):
  1046.                 size = im.size
  1047.             elif isImageType(mask):
  1048.                 size = mask.size
  1049.             else:
  1050.                 # FIXME: use self.size here?
  1051.                 raise ValueError(
  1052.                     "cannot determine region size; use 4-item box"
  1053.                     )
  1054.             box = box + (box[0]+size[0], box[1]+size[1])
  1055.  
  1056.         if isStringType(im):
  1057.             import ImageColor
  1058.             im = ImageColor.getcolor(im, self.mode)
  1059.  
  1060.         elif isImageType(im):
  1061.             im.load()
  1062.             if self.mode != im.mode:
  1063.                 if self.mode != "RGB" or im.mode not in ("RGBA", "RGBa"):
  1064.                     # should use an adapter for this!
  1065.                     im = im.convert(self.mode)
  1066.             im = im.im
  1067.  
  1068.         self.load()
  1069.         if self.readonly:
  1070.             self._copy()
  1071.  
  1072.         if mask:
  1073.             mask.load()
  1074.             self.im.paste(im, box, mask.im)
  1075.         else:
  1076.             self.im.paste(im, box)
  1077.  
  1078.     ##
  1079.     # Maps this image through a lookup table or function.
  1080.     #
  1081.     # @param lut A lookup table, containing 256 values per band in the
  1082.     #    image. A function can be used instead, it should take a single
  1083.     #    argument. The function is called once for each possible pixel
  1084.     #    value, and the resulting table is applied to all bands of the
  1085.     #    image.
  1086.     # @param mode Output mode (default is same as input).  In the
  1087.     #    current version, this can only be used if the source image
  1088.     #    has mode "L" or "P", and the output has mode "1".
  1089.     # @return An Image object.
  1090.  
  1091.     def point(self, lut, mode=None):
  1092.         "Map image through lookup table"
  1093.  
  1094.         self.load()
  1095.  
  1096.         if not isSequenceType(lut):
  1097.             # if it isn't a list, it should be a function
  1098.             if self.mode in ("I", "I;16", "F"):
  1099.                 # check if the function can be used with point_transform
  1100.                 scale, offset = _getscaleoffset(lut)
  1101.                 return self._new(self.im.point_transform(scale, offset))
  1102.             # for other modes, convert the function to a table
  1103.             lut = map(lut, range(256)) * self.im.bands
  1104.  
  1105.         if self.mode == "F":
  1106.             # FIXME: _imaging returns a confusing error message for this case
  1107.             raise ValueError("point operation not supported for this mode")
  1108.  
  1109.         return self._new(self.im.point(lut, mode))
  1110.  
  1111.     ##
  1112.     # Adds or replaces the alpha layer in this image.  If the image
  1113.     # does not have an alpha layer, it's converted to "LA" or "RGBA".
  1114.     # The new layer must be either "L" or "1".
  1115.     #
  1116.     # @param im The new alpha layer.  This can either be an "L" or "1"
  1117.     #    image having the same size as this image, or an integer or
  1118.     #    other color value.
  1119.  
  1120.     def putalpha(self, alpha):
  1121.         "Set alpha layer"
  1122.  
  1123.         self.load()
  1124.         if self.readonly:
  1125.             self._copy()
  1126.  
  1127.         if self.mode not in ("LA", "RGBA"):
  1128.             # attempt to promote self to a matching alpha mode
  1129.             try:
  1130.                 mode = getmodebase(self.mode) + "A"
  1131.                 try:
  1132.                     self.im.setmode(mode)
  1133.                 except (AttributeError, ValueError):
  1134.                     # do things the hard way
  1135.                     im = self.im.convert(mode)
  1136.                     if im.mode not in ("LA", "RGBA"):
  1137.                         raise ValueError # sanity check
  1138.                     self.im = im
  1139.                 self.mode = self.im.mode
  1140.             except (KeyError, ValueError):
  1141.                 raise ValueError("illegal image mode")
  1142.  
  1143.         if self.mode == "LA":
  1144.             band = 1
  1145.         else:
  1146.             band = 3
  1147.  
  1148.         if isImageType(alpha):
  1149.             # alpha layer
  1150.             if alpha.mode not in ("1", "L"):
  1151.                 raise ValueError("illegal image mode")
  1152.             alpha.load()
  1153.             if alpha.mode == "1":
  1154.                 alpha = alpha.convert("L")
  1155.         else:
  1156.             # constant alpha
  1157.             try:
  1158.                 self.im.fillband(band, alpha)
  1159.             except (AttributeError, ValueError):
  1160.                 # do things the hard way
  1161.                 alpha = new("L", self.size, alpha)
  1162.             else:
  1163.                 return
  1164.  
  1165.         self.im.putband(alpha.im, band)
  1166.  
  1167.     ##
  1168.     # Copies pixel data to this image.  This method copies data from a
  1169.     # sequence object into the image, starting at the upper left
  1170.     # corner (0, 0), and continuing until either the image or the
  1171.     # sequence ends.  The scale and offset values are used to adjust
  1172.     # the sequence values: <b>pixel = value*scale + offset</b>.
  1173.     #
  1174.     # @param data A sequence object.
  1175.     # @param scale An optional scale value.  The default is 1.0.
  1176.     # @param offset An optional offset value.  The default is 0.0.
  1177.  
  1178.     def putdata(self, data, scale=1.0, offset=0.0):
  1179.         "Put data from a sequence object into an image."
  1180.  
  1181.         self.load()
  1182.         if self.readonly:
  1183.             self._copy()
  1184.  
  1185.         self.im.putdata(data, scale, offset)
  1186.  
  1187.     ##
  1188.     # Attaches a palette to this image.  The image must be a "P" or
  1189.     # "L" image, and the palette sequence must contain 768 integer
  1190.     # values, where each group of three values represent the red,
  1191.     # green, and blue values for the corresponding pixel
  1192.     # index. Instead of an integer sequence, you can use an 8-bit
  1193.     # string.
  1194.     #
  1195.     # @def putpalette(data)
  1196.     # @param data A palette sequence (either a list or a string).
  1197.  
  1198.     def putpalette(self, data, rawmode="RGB"):
  1199.         "Put palette data into an image."
  1200.  
  1201.         self.load()
  1202.         if self.mode not in ("L", "P"):
  1203.             raise ValueError("illegal image mode")
  1204.         if not isStringType(data):
  1205.             data = string.join(map(chr, data), "")
  1206.         self.mode = "P"
  1207.         self.palette = ImagePalette.raw(rawmode, data)
  1208.         self.palette.mode = "RGB"
  1209.         self.load() # install new palette
  1210.  
  1211.     ##
  1212.     # Modifies the pixel at the given position. The colour is given as
  1213.     # a single numerical value for single-band images, and a tuple for
  1214.     # multi-band images.
  1215.     # <p>
  1216.     # Note that this method is relatively slow.  For more extensive
  1217.     # changes, use {@link #Image.paste} or the <b>ImageDraw</b> module
  1218.     # instead.
  1219.     #
  1220.     # @param xy The pixel coordinate, given as (x, y).
  1221.     # @param value The pixel value.
  1222.     # @see #Image.paste
  1223.     # @see #Image.putdata
  1224.     # @see ImageDraw
  1225.  
  1226.     def putpixel(self, xy, value):
  1227.         "Set pixel value"
  1228.  
  1229.         self.load()
  1230.         if self.readonly:
  1231.             self._copy()
  1232.  
  1233.         return self.im.putpixel(xy, value)
  1234.  
  1235.     ##
  1236.     # Returns a resized copy of this image.
  1237.     #
  1238.     # @def resize(size, filter=NEAREST)
  1239.     # @param size The requested size in pixels, as a 2-tuple:
  1240.     #    (width, height).
  1241.     # @param filter An optional resampling filter.  This can be
  1242.     #    one of <b>NEAREST</b> (use nearest neighbour), <b>BILINEAR</b>
  1243.     #    (linear interpolation in a 2x2 environment), <b>BICUBIC</b>
  1244.     #    (cubic spline interpolation in a 4x4 environment), or
  1245.     #    <b>ANTIALIAS</b> (a high-quality downsampling filter).
  1246.     #    If omitted, or if the image has mode "1" or "P", it is
  1247.     #    set <b>NEAREST</b>.
  1248.     # @return An Image object.
  1249.  
  1250.     def resize(self, size, resample=NEAREST):
  1251.         "Resize image"
  1252.  
  1253.         if resample not in (NEAREST, BILINEAR, BICUBIC, ANTIALIAS):
  1254.             raise ValueError("unknown resampling filter")
  1255.  
  1256.         self.load()
  1257.  
  1258.         if self.mode in ("1", "P"):
  1259.             resample = NEAREST
  1260.  
  1261.         if resample == ANTIALIAS:
  1262.             # requires stretch support (imToolkit & PIL 1.1.3)
  1263.             try:
  1264.                 im = self.im.stretch(size, resample)
  1265.             except AttributeError:
  1266.                 raise ValueError("unsupported resampling filter")
  1267.         else:
  1268.             im = self.im.resize(size, resample)
  1269.  
  1270.         return self._new(im)
  1271.  
  1272.     ##
  1273.     # Returns a rotated copy of this image.  This method returns a
  1274.     # copy of this image, rotated the given number of degrees counter
  1275.     # clockwise around its centre.
  1276.     #
  1277.     # @def rotate(angle, filter=NEAREST)
  1278.     # @param angle In degrees counter clockwise.
  1279.     # @param filter An optional resampling filter.  This can be
  1280.     #    one of <b>NEAREST</b> (use nearest neighbour), <b>BILINEAR</b>
  1281.     #    (linear interpolation in a 2x2 environment), or <b>BICUBIC</b>
  1282.     #    (cubic spline interpolation in a 4x4 environment).
  1283.     #    If omitted, or if the image has mode "1" or "P", it is
  1284.     #    set <b>NEAREST</b>.
  1285.     # @param expand Optional expansion flag.  If true, expands the output
  1286.     #    image to make it large enough to hold the entire rotated image.
  1287.     #    If false or omitted, make the output image the same size as the
  1288.     #    input image.
  1289.     # @return An Image object.
  1290.  
  1291.     def rotate(self, angle, resample=NEAREST, expand=0):
  1292.         "Rotate image.  Angle given as degrees counter-clockwise."
  1293.  
  1294.         if expand:
  1295.             import math
  1296.             angle = -angle * math.pi / 180
  1297.             matrix = [
  1298.                  math.cos(angle), math.sin(angle), 0.0,
  1299.                 -math.sin(angle), math.cos(angle), 0.0
  1300.                  ]
  1301.             def transform(x, y, (a, b, c, d, e, f)=matrix):
  1302.                 return a*x + b*y + c, d*x + e*y + f
  1303.  
  1304.             # calculate output size
  1305.             w, h = self.size
  1306.             xx = []
  1307.             yy = []
  1308.             for x, y in ((0, 0), (w, 0), (w, h), (0, h)):
  1309.                 x, y = transform(x, y)
  1310.                 xx.append(x)
  1311.                 yy.append(y)
  1312.             w = int(math.ceil(max(xx)) - math.floor(min(xx)))
  1313.             h = int(math.ceil(max(yy)) - math.floor(min(yy)))
  1314.  
  1315.             # adjust center
  1316.             x, y = transform(w / 2.0, h / 2.0)
  1317.             matrix[2] = self.size[0] / 2.0 - x
  1318.             matrix[5] = self.size[1] / 2.0 - y
  1319.  
  1320.             return self.transform((w, h), AFFINE, matrix)
  1321.  
  1322.         if resample not in (NEAREST, BILINEAR, BICUBIC):
  1323.             raise ValueError("unknown resampling filter")
  1324.  
  1325.         self.load()
  1326.  
  1327.         if self.mode in ("1", "P"):
  1328.             resample = NEAREST
  1329.  
  1330.         return self._new(self.im.rotate(angle, resample))
  1331.  
  1332.     ##
  1333.     # Saves this image under the given filename.  If no format is
  1334.     # specified, the format to use is determined from the filename
  1335.     # extension, if possible.
  1336.     # <p>
  1337.     # Keyword options can be used to provide additional instructions
  1338.     # to the writer. If a writer doesn't recognise an option, it is
  1339.     # silently ignored. The available options are described later in
  1340.     # this handbook.
  1341.     # <p>
  1342.     # You can use a file object instead of a filename. In this case,
  1343.     # you must always specify the format. The file object must
  1344.     # implement the <b>seek</b>, <b>tell</b>, and <b>write</b>
  1345.     # methods, and be opened in binary mode.
  1346.     #
  1347.     # @def save(file, format=None, **options)
  1348.     # @param file File name or file object.
  1349.     # @param format Optional format override.  If omitted, the
  1350.     #    format to use is determined from the filename extension.
  1351.     #    If a file object was used instead of a filename, this
  1352.     #    parameter should always be used.
  1353.     # @param **options Extra parameters to the image writer.
  1354.     # @return None
  1355.     # @exception KeyError If the output format could not be determined
  1356.     #    from the file name.  Use the format option to solve this.
  1357.     # @exception IOError If the file could not be written.  The file
  1358.     #    may have been created, and may contain partial data.
  1359.  
  1360.     def save(self, fp, format=None, **params):
  1361.         "Save image to file or stream"
  1362.  
  1363.         if isStringType(fp):
  1364.             filename = fp
  1365.         else:
  1366.             if hasattr(fp, "name") and isStringType(fp.name):
  1367.                 filename = fp.name
  1368.             else:
  1369.                 filename = ""
  1370.  
  1371.         # may mutate self!
  1372.         self.load()
  1373.  
  1374.         self.encoderinfo = params
  1375.         self.encoderconfig = ()
  1376.  
  1377.         preinit()
  1378.  
  1379.         ext = string.lower(os.path.splitext(filename)[1])
  1380.  
  1381.         if not format:
  1382.             try:
  1383.                 format = EXTENSION[ext]
  1384.             except KeyError:
  1385.                 init()
  1386.                 try:
  1387.                     format = EXTENSION[ext]
  1388.                 except KeyError:
  1389.                     raise KeyError(ext) # unknown extension
  1390.  
  1391.         try:
  1392.             save_handler = SAVE[string.upper(format)]
  1393.         except KeyError:
  1394.             init()
  1395.             save_handler = SAVE[string.upper(format)] # unknown format
  1396.  
  1397.         if isStringType(fp):
  1398.             import __builtin__
  1399.             fp = __builtin__.open(fp, "wb")
  1400.             close = 1
  1401.         else:
  1402.             close = 0
  1403.  
  1404.         try:
  1405.             save_handler(self, fp, filename)
  1406.         finally:
  1407.             # do what we can to clean up
  1408.             if close:
  1409.                 fp.close()
  1410.  
  1411.     ##
  1412.     # Seeks to the given frame in this sequence file. If you seek
  1413.     # beyond the end of the sequence, the method raises an
  1414.     # <b>EOFError</b> exception. When a sequence file is opened, the
  1415.     # library automatically seeks to frame 0.
  1416.     # <p>
  1417.     # Note that in the current version of the library, most sequence
  1418.     # formats only allows you to seek to the next frame.
  1419.     #
  1420.     # @param frame Frame number, starting at 0.
  1421.     # @exception EOFError If the call attempts to seek beyond the end
  1422.     #     of the sequence.
  1423.     # @see #Image.tell
  1424.  
  1425.     def seek(self, frame):
  1426.         "Seek to given frame in sequence file"
  1427.  
  1428.         # overridden by file handlers
  1429.         if frame != 0:
  1430.             raise EOFError
  1431.  
  1432.     ##
  1433.     # Displays this image. This method is mainly intended for
  1434.     # debugging purposes.
  1435.     # <p>
  1436.     # On Unix platforms, this method saves the image to a temporary
  1437.     # PPM file, and calls the <b>xv</b> utility.
  1438.     # <p>
  1439.     # On Windows, it saves the image to a temporary BMP file, and uses
  1440.     # the standard BMP display utility to show it (usually Paint).
  1441.     #
  1442.     # @def show(title=None)
  1443.     # @param title Optional title to use for the image window,
  1444.     #    where possible.
  1445.  
  1446.     def show(self, title=None, command=None):
  1447.         "Display image (for debug purposes only)"
  1448.  
  1449.         _showxv(self, title, command)
  1450.  
  1451.     ##
  1452.     # Split this image into individual bands. This method returns a
  1453.     # tuple of individual image bands from an image. For example,
  1454.     # splitting an "RGB" image creates three new images each
  1455.     # containing a copy of one of the original bands (red, green,
  1456.     # blue).
  1457.     #
  1458.     # @return A tuple containing bands.
  1459.  
  1460.     def split(self):
  1461.         "Split image into bands"
  1462.  
  1463.         ims = []
  1464.         self.load()
  1465.         for i in range(self.im.bands):
  1466.             ims.append(self._new(self.im.getband(i)))
  1467.         return tuple(ims)
  1468.  
  1469.     ##
  1470.     # Returns the current frame number.
  1471.     #
  1472.     # @return Frame number, starting with 0.
  1473.     # @see #Image.seek
  1474.  
  1475.     def tell(self):
  1476.         "Return current frame number"
  1477.  
  1478.         return 0
  1479.  
  1480.     ##
  1481.     # Make this image into a thumbnail.  This method modifies the
  1482.     # image to contain a thumbnail version of itself, no larger than
  1483.     # the given size.  This method calculates an appropriate thumbnail
  1484.     # size to preserve the aspect of the image, calls the {@link
  1485.     # #Image.draft} method to configure the file reader (where
  1486.     # applicable), and finally resizes the image.
  1487.     # <p>
  1488.     # Note that the bilinear and bicubic filters in the current
  1489.     # version of PIL are not well-suited for thumbnail generation.
  1490.     # You should use <b>ANTIALIAS</b> unless speed is much more
  1491.     # important than quality.
  1492.     # <p>
  1493.     # Also note that this function modifies the Image object in place.
  1494.     # If you need to use the full resolution image as well, apply this
  1495.     # method to a {@link #Image.copy} of the original image.
  1496.     #
  1497.     # @param size Requested size.
  1498.     # @param resample Optional resampling filter.  This can be one
  1499.     #    of <b>NEAREST</b>, <b>BILINEAR</b>, <b>BICUBIC</b>, or
  1500.     #    <b>ANTIALIAS</b> (best quality).  If omitted, it defaults
  1501.     #    to <b>NEAREST</b> (this will be changed to ANTIALIAS in a
  1502.     #    future version).
  1503.     # @return None
  1504.  
  1505.     def thumbnail(self, size, resample=NEAREST):
  1506.         "Create thumbnail representation (modifies image in place)"
  1507.  
  1508.         # FIXME: the default resampling filter will be changed
  1509.         # to ANTIALIAS in future versions
  1510.  
  1511.         # preserve aspect ratio
  1512.         x, y = self.size
  1513.         if x > size[0]: y = max(y * size[0] / x, 1); x = size[0]
  1514.         if y > size[1]: x = max(x * size[1] / y, 1); y = size[1]
  1515.         size = x, y
  1516.  
  1517.         if size == self.size:
  1518.             return
  1519.  
  1520.         self.draft(None, size)
  1521.  
  1522.         self.load()
  1523.  
  1524.         try:
  1525.             im = self.resize(size, resample)
  1526.         except ValueError:
  1527.             if resample != ANTIALIAS:
  1528.                 raise
  1529.             im = self.resize(size, NEAREST) # fallback
  1530.  
  1531.         self.im = im.im
  1532.         self.mode = im.mode
  1533.         self.size = size
  1534.  
  1535.         self.readonly = 0
  1536.  
  1537.     # FIXME: the different tranform methods need further explanation
  1538.     # instead of bloating the method docs, add a separate chapter.
  1539.  
  1540.     ##
  1541.     # Transforms this image.  This method creates a new image with the
  1542.     # given size, and the same mode as the original, and copies data
  1543.     # to the new image using the given transform.
  1544.     # <p>
  1545.     # @def transform(size, method, data, resample=NEAREST)
  1546.     # @param size The output size.
  1547.     # @param method The transformation method.  This is one of
  1548.     #   <b>EXTENT</b> (cut out a rectangular subregion), <b>AFFINE</b>
  1549.     #   (affine transform), <b>PERSPECTIVE</b> (perspective
  1550.     #   transform), <b>QUAD</b> (map a quadrilateral to a
  1551.     #   rectangle), or <b>MESH</b> (map a number of source quadrilaterals
  1552.     #   in one operation).
  1553.     # @param data Extra data to the transformation method.
  1554.     # @param resample Optional resampling filter.  It can be one of
  1555.     #    <b>NEAREST</b> (use nearest neighbour), <b>BILINEAR</b>
  1556.     #    (linear interpolation in a 2x2 environment), or
  1557.     #    <b>BICUBIC</b> (cubic spline interpolation in a 4x4
  1558.     #    environment). If omitted, or if the image has mode
  1559.     #    "1" or "P", it is set to <b>NEAREST</b>.
  1560.     # @return An Image object.
  1561.  
  1562.     def transform(self, size, method, data=None, resample=NEAREST, fill=1):
  1563.         "Transform image"
  1564.  
  1565.         import ImageTransform
  1566.         if isinstance(method, ImageTransform.Transform):
  1567.             method, data = method.getdata()
  1568.         if data is None:
  1569.             raise ValueError("missing method data")
  1570.         im = new(self.mode, size, None)
  1571.         if method == MESH:
  1572.             # list of quads
  1573.             for box, quad in data:
  1574.                 im.__transformer(box, self, QUAD, quad, resample, fill)
  1575.         else:
  1576.             im.__transformer((0, 0)+size, self, method, data, resample, fill)
  1577.  
  1578.         return im
  1579.  
  1580.     def __transformer(self, box, image, method, data,
  1581.                       resample=NEAREST, fill=1):
  1582.  
  1583.         # FIXME: this should be turned into a lazy operation (?)
  1584.  
  1585.         w = box[2]-box[0]
  1586.         h = box[3]-box[1]
  1587.  
  1588.         if method == AFFINE:
  1589.             # change argument order to match implementation
  1590.             data = (data[2], data[0], data[1],
  1591.                     data[5], data[3], data[4])
  1592.         elif method == EXTENT:
  1593.             # convert extent to an affine transform
  1594.             x0, y0, x1, y1 = data
  1595.             xs = float(x1 - x0) / w
  1596.             ys = float(y1 - y0) / h
  1597.             method = AFFINE
  1598.             data = (x0 + xs/2, xs, 0, y0 + ys/2, 0, ys)
  1599.         elif method == PERSPECTIVE:
  1600.             # change argument order to match implementation
  1601.             data = (data[2], data[0], data[1],
  1602.                     data[5], data[3], data[4],
  1603.                     data[6], data[7])
  1604.         elif method == QUAD:
  1605.             # quadrilateral warp.  data specifies the four corners
  1606.             # given as NW, SW, SE, and NE.
  1607.             nw = data[0:2]; sw = data[2:4]; se = data[4:6]; ne = data[6:8]
  1608.             x0, y0 = nw; As = 1.0 / w; At = 1.0 / h
  1609.             data = (x0, (ne[0]-x0)*As, (sw[0]-x0)*At,
  1610.                     (se[0]-sw[0]-ne[0]+x0)*As*At,
  1611.                     y0, (ne[1]-y0)*As, (sw[1]-y0)*At,
  1612.                     (se[1]-sw[1]-ne[1]+y0)*As*At)
  1613.         else:
  1614.             raise ValueError("unknown transformation method")
  1615.  
  1616.         if resample not in (NEAREST, BILINEAR, BICUBIC):
  1617.             raise ValueError("unknown resampling filter")
  1618.  
  1619.         image.load()
  1620.  
  1621.         self.load()
  1622.  
  1623.         if image.mode in ("1", "P"):
  1624.             resample = NEAREST
  1625.  
  1626.         self.im.transform2(box, image.im, method, data, resample, fill)
  1627.  
  1628.     ##
  1629.     # Returns a flipped or rotated copy of this image.
  1630.     #
  1631.     # @param method One of <b>FLIP_LEFT_RIGHT</b>, <b>FLIP_TOP_BOTTOM</b>,
  1632.     # <b>ROTATE_90</b>, <b>ROTATE_180</b>, or <b>ROTATE_270</b>.
  1633.  
  1634.     def transpose(self, method):
  1635.         "Transpose image (flip or rotate in 90 degree steps)"
  1636.  
  1637.         self.load()
  1638.         im = self.im.transpose(method)
  1639.         return self._new(im)
  1640.  
  1641. # --------------------------------------------------------------------
  1642. # Lazy operations
  1643.  
  1644. class _ImageCrop(Image):
  1645.  
  1646.     def __init__(self, im, box):
  1647.  
  1648.         Image.__init__(self)
  1649.  
  1650.         x0, y0, x1, y1 = box
  1651.         if x1 < x0:
  1652.             x1 = x0
  1653.         if y1 < y0:
  1654.             y1 = y0
  1655.  
  1656.         self.mode = im.mode
  1657.         self.size = x1-x0, y1-y0
  1658.  
  1659.         self.__crop = x0, y0, x1, y1
  1660.  
  1661.         self.im = im.im
  1662.  
  1663.     def load(self):
  1664.  
  1665.         # lazy evaluation!
  1666.         if self.__crop:
  1667.             self.im = self.im.crop(self.__crop)
  1668.             self.__crop = None
  1669.  
  1670.         # FIXME: future versions should optimize crop/paste
  1671.         # sequences!
  1672.  
  1673. # --------------------------------------------------------------------
  1674. # Factories
  1675.  
  1676. #
  1677. # Debugging
  1678.  
  1679. def _wedge():
  1680.     "Create greyscale wedge (for debugging only)"
  1681.  
  1682.     return Image()._new(core.wedge("L"))
  1683.  
  1684. ##
  1685. # Creates a new image with the given mode and size.
  1686. #
  1687. # @param mode The mode to use for the new image.
  1688. # @param size A 2-tuple, containing (width, height) in pixels.
  1689. # @param color What colour to use for the image.  Default is black.
  1690. #    If given, this should be a single integer or floating point value
  1691. #    for single-band modes, and a tuple for multi-band modes (one value
  1692. #    per band).  When creating RGB images, you can also use colour
  1693. #    strings as supported by the ImageColor module.  If the colour is
  1694. #    None, the image is not initialised.
  1695. # @return An Image object.
  1696.  
  1697. def new(mode, size, color=0):
  1698.     "Create a new image"
  1699.  
  1700.     if color is None:
  1701.         # don't initialize
  1702.         return Image()._new(core.new(mode, size))
  1703.  
  1704.     if isStringType(color):
  1705.         # css3-style specifier
  1706.  
  1707.         import ImageColor
  1708.         color = ImageColor.getcolor(color, mode)
  1709.  
  1710.     return Image()._new(core.fill(mode, size, color))
  1711.  
  1712. ##
  1713. # Creates an image memory from pixel data in a string.
  1714. # <p>
  1715. # In its simplest form, this function takes three arguments
  1716. # (mode, size, and unpacked pixel data).
  1717. # <p>
  1718. # You can also use any pixel decoder supported by PIL.  For more
  1719. # information on available decoders, see the section <a
  1720. # href="pil-decoder.htm"><i>Writing Your Own File Decoder</i></a>.
  1721. # <p>
  1722. # Note that this function decodes pixel data only, not entire images.
  1723. # If you have an entire image in a string, wrap it in a
  1724. # <b>StringIO</b> object, and use {@link #open} to load it.
  1725. #
  1726. # @param mode The image mode.
  1727. # @param size The image size.
  1728. # @param data An 8-bit string containing raw data for the given mode.
  1729. # @param decoder_name What decoder to use.
  1730. # @param *args Additional parameters for the given decoder.
  1731. # @return An Image object.
  1732.  
  1733. def fromstring(mode, size, data, decoder_name="raw", *args):
  1734.     "Load image from string"
  1735.  
  1736.     # may pass tuple instead of argument list
  1737.     if len(args) == 1 and isTupleType(args[0]):
  1738.         args = args[0]
  1739.  
  1740.     if decoder_name == "raw" and args == ():
  1741.         args = mode
  1742.  
  1743.     im = new(mode, size)
  1744.     im.fromstring(data, decoder_name, args)
  1745.     return im
  1746.  
  1747. ##
  1748. # (New in 1.1.4) Creates an image memory from pixel data in a string
  1749. # or byte buffer.
  1750. # <p>
  1751. # This function is similar to {@link #fromstring}, but uses data in
  1752. # the byte buffer, where possible.  This means that changes to the
  1753. # original buffer object are reflected in this image).  Not all modes
  1754. # can share memory; supported modes include "L", "RGBX", "RGBA", and
  1755. # "CMYK".
  1756. # <p>
  1757. # Note that this function decodes pixel data only, not entire images.
  1758. # If you have an entire image file in a string, wrap it in a
  1759. # <b>StringIO</b> object, and use {@link #open} to load it.
  1760. # <p>
  1761. # In the current version, the default parameters used for the "raw"
  1762. # decoder differs from that used for {@link fromstring}.  This is a
  1763. # bug, and will probably be fixed in a future release.  The current
  1764. # release issues a warning if you do this; to disable the warning,
  1765. # you should provide the full set of parameters.  See below for
  1766. # details.
  1767. #
  1768. # @param mode The image mode.
  1769. # @param size The image size.
  1770. # @param data An 8-bit string or other buffer object containing raw
  1771. #     data for the given mode.
  1772. # @param decoder_name What decoder to use.
  1773. # @param *args Additional parameters for the given decoder.  For the
  1774. #     default encoder ("raw"), it's recommended that you provide the
  1775. #     full set of parameters:
  1776. #     <b>frombuffer(mode, size, data, "raw", mode, 0, 1)</b>.
  1777. # @return An Image object.
  1778. # @since 1.1.4
  1779.  
  1780. def frombuffer(mode, size, data, decoder_name="raw", *args):
  1781.     "Load image from string or buffer"
  1782.  
  1783.     # may pass tuple instead of argument list
  1784.     if len(args) == 1 and isTupleType(args[0]):
  1785.         args = args[0]
  1786.  
  1787.     if decoder_name == "raw":
  1788.         if args == ():
  1789.             if warnings:
  1790.                 warnings.warn(
  1791.                     "the frombuffer defaults may change in a future release; "
  1792.                     "for portability, change the call to read:\n"
  1793.                     "  frombuffer(mode, size, data, 'raw', mode, 0, 1)",
  1794.                     RuntimeWarning, stacklevel=2
  1795.                 )
  1796.             args = mode, 0, -1 # may change to (mode, 0, 1) post-1.1.6
  1797.         if args[0] in _MAPMODES:
  1798.             im = new(mode, (1,1))
  1799.             im = im._new(
  1800.                 core.map_buffer(data, size, decoder_name, None, 0, args)
  1801.                 )
  1802.             im.readonly = 1
  1803.             return im
  1804.  
  1805.     return apply(fromstring, (mode, size, data, decoder_name, args))
  1806.  
  1807.  
  1808. ##
  1809. # (New in 1.1.6) Create an image memory from an object exporting
  1810. # the array interface (using the buffer protocol).
  1811. #
  1812. # If obj is not contiguous, then the tostring method is called
  1813. # and {@link frombuffer} is used.
  1814. #
  1815. # @param obj Object with array interface
  1816. # @param mode Mode to use (will be determined from type if None)
  1817. # @return An image memory.
  1818.  
  1819. def fromarray(obj, mode=None):
  1820.     arr = obj.__array_interface__
  1821.     shape = arr['shape']
  1822.     ndim = len(shape)
  1823.     try:
  1824.         strides = arr['strides']
  1825.     except KeyError:
  1826.         strides = None
  1827.     if mode is None:
  1828.         typestr = arr['typestr']
  1829.         if not (typestr[0] == '|' or typestr[0] == _ENDIAN or
  1830.                 typestr[1:] not in ['u1', 'b1', 'i4', 'f4']):
  1831.             raise TypeError("cannot handle data-type")
  1832.         typestr = typestr[:2]
  1833.         if typestr == 'i4':
  1834.             mode = 'I'
  1835.         elif typestr == 'f4':
  1836.             mode = 'F'
  1837.         elif typestr == 'b1':
  1838.             mode = '1'
  1839.         elif ndim == 2:
  1840.             mode = 'L'
  1841.         elif ndim == 3:
  1842.             mode = 'RGB'
  1843.         elif ndim == 4:
  1844.             mode = 'RGBA'
  1845.         else:
  1846.             raise TypeError("Do not understand data.")
  1847.     ndmax = 4
  1848.     bad_dims=0
  1849.     if mode in ['1','L','I','P','F']:
  1850.         ndmax = 2
  1851.     elif mode == 'RGB':
  1852.         ndmax = 3
  1853.     if ndim > ndmax:
  1854.         raise ValueError("Too many dimensions.")
  1855.  
  1856.     size = shape[:2][::-1]
  1857.     if strides is not None:
  1858.         obj = obj.tostring()
  1859.  
  1860.     return frombuffer(mode, size, obj, "raw", mode, 0, 1)
  1861.  
  1862. ##
  1863. # Opens and identifies the given image file.
  1864. # <p>
  1865. # This is a lazy operation; this function identifies the file, but the
  1866. # actual image data is not read from the file until you try to process
  1867. # the data (or call the {@link #Image.load} method).
  1868. #
  1869. # @def open(file, mode="r")
  1870. # @param file A filename (string) or a file object.  The file object
  1871. #    must implement <b>read</b>, <b>seek</b>, and <b>tell</b> methods,
  1872. #    and be opened in binary mode.
  1873. # @param mode The mode.  If given, this argument must be "r".
  1874. # @return An Image object.
  1875. # @exception IOError If the file cannot be found, or the image cannot be
  1876. #    opened and identified.
  1877. # @see #new
  1878.  
  1879. def open(fp, mode="r"):
  1880.     "Open an image file, without loading the raster data"
  1881.  
  1882.     if mode != "r":
  1883.         raise ValueError("bad mode")
  1884.  
  1885.     if isStringType(fp):
  1886.         import __builtin__
  1887.         filename = fp
  1888.         fp = __builtin__.open(fp, "rb")
  1889.     else:
  1890.         filename = ""
  1891.  
  1892.     prefix = fp.read(16)
  1893.  
  1894.     preinit()
  1895.  
  1896.     for i in ID:
  1897.         try:
  1898.             factory, accept = OPEN[i]
  1899.             if not accept or accept(prefix):
  1900.                 fp.seek(0)
  1901.                 return factory(fp, filename)
  1902.         except (SyntaxError, IndexError, TypeError):
  1903.             pass
  1904.  
  1905.     init()
  1906.  
  1907.     for i in ID:
  1908.         try:
  1909.             factory, accept = OPEN[i]
  1910.             if not accept or accept(prefix):
  1911.                 fp.seek(0)
  1912.                 return factory(fp, filename)
  1913.         except (SyntaxError, IndexError, TypeError):
  1914.             pass
  1915.  
  1916.     raise IOError("cannot identify image file")
  1917.  
  1918. #
  1919. # Image processing.
  1920.  
  1921. ##
  1922. # Creates a new image by interpolating between two input images, using
  1923. # a constant alpha.
  1924. #
  1925. # <pre>
  1926. #    out = image1 * (1.0 - alpha) + image2 * alpha
  1927. # </pre>
  1928. #
  1929. # @param im1 The first image.
  1930. # @param im2 The second image.  Must have the same mode and size as
  1931. #    the first image.
  1932. # @param alpha The interpolation alpha factor.  If alpha is 0.0, a
  1933. #    copy of the first image is returned. If alpha is 1.0, a copy of
  1934. #    the second image is returned. There are no restrictions on the
  1935. #    alpha value. If necessary, the result is clipped to fit into
  1936. #    the allowed output range.
  1937. # @return An Image object.
  1938.  
  1939. def blend(im1, im2, alpha):
  1940.     "Interpolate between images."
  1941.  
  1942.     im1.load()
  1943.     im2.load()
  1944.     return im1._new(core.blend(im1.im, im2.im, alpha))
  1945.  
  1946. ##
  1947. # Creates a new image by interpolating between two input images,
  1948. # using the mask as alpha.
  1949. #
  1950. # @param image1 The first image.
  1951. # @param image2 The second image.  Must have the same mode and
  1952. #    size as the first image.
  1953. # @param mask A mask image.  This image can can have mode
  1954. #    "1", "L", or "RGBA", and must have the same size as the
  1955. #    other two images.
  1956.  
  1957. def composite(image1, image2, mask):
  1958.     "Create composite image by blending images using a transparency mask"
  1959.  
  1960.     image = image2.copy()
  1961.     image.paste(image1, None, mask)
  1962.     return image
  1963.  
  1964. ##
  1965. # Applies the function (which should take one argument) to each pixel
  1966. # in the given image. If the image has more than one band, the same
  1967. # function is applied to each band. Note that the function is
  1968. # evaluated once for each possible pixel value, so you cannot use
  1969. # random components or other generators.
  1970. #
  1971. # @def eval(image, function)
  1972. # @param image The input image.
  1973. # @param function A function object, taking one integer argument.
  1974. # @return An Image object.
  1975.  
  1976. def eval(image, *args):
  1977.     "Evaluate image expression"
  1978.  
  1979.     return image.point(args[0])
  1980.  
  1981. ##
  1982. # Creates a new image from a number of single-band images.
  1983. #
  1984. # @param mode The mode to use for the output image.
  1985. # @param bands A sequence containing one single-band image for
  1986. #     each band in the output image.  All bands must have the
  1987. #     same size.
  1988. # @return An Image object.
  1989.  
  1990. def merge(mode, bands):
  1991.     "Merge a set of single band images into a new multiband image."
  1992.  
  1993.     if getmodebands(mode) != len(bands) or "*" in mode:
  1994.         raise ValueError("wrong number of bands")
  1995.     for im in bands[1:]:
  1996.         if im.mode != getmodetype(mode):
  1997.             raise ValueError("mode mismatch")
  1998.         if im.size != bands[0].size:
  1999.             raise ValueError("size mismatch")
  2000.     im = core.new(mode, bands[0].size)
  2001.     for i in range(getmodebands(mode)):
  2002.         bands[i].load()
  2003.         im.putband(bands[i].im, i)
  2004.     return bands[0]._new(im)
  2005.  
  2006. # --------------------------------------------------------------------
  2007. # Plugin registry
  2008.  
  2009. ##
  2010. # Register an image file plugin.  This function should not be used
  2011. # in application code.
  2012. #
  2013. # @param id An image format identifier.
  2014. # @param factory An image file factory method.
  2015. # @param accept An optional function that can be used to quickly
  2016. #    reject images having another format.
  2017.  
  2018. def register_open(id, factory, accept=None):
  2019.     id = string.upper(id)
  2020.     ID.append(id)
  2021.     OPEN[id] = factory, accept
  2022.  
  2023. ##
  2024. # Registers an image MIME type.  This function should not be used
  2025. # in application code.
  2026. #
  2027. # @param id An image format identifier.
  2028. # @param mimetype The image MIME type for this format.
  2029.  
  2030. def register_mime(id, mimetype):
  2031.     MIME[string.upper(id)] = mimetype
  2032.  
  2033. ##
  2034. # Registers an image save function.  This function should not be
  2035. # used in application code.
  2036. #
  2037. # @param id An image format identifier.
  2038. # @param driver A function to save images in this format.
  2039.  
  2040. def register_save(id, driver):
  2041.     SAVE[string.upper(id)] = driver
  2042.  
  2043. ##
  2044. # Registers an image extension.  This function should not be
  2045. # used in application code.
  2046. #
  2047. # @param id An image format identifier.
  2048. # @param extension An extension used for this format.
  2049.  
  2050. def register_extension(id, extension):
  2051.     EXTENSION[string.lower(extension)] = string.upper(id)
  2052.  
  2053.  
  2054. # --------------------------------------------------------------------
  2055. # Simple display support
  2056.  
  2057. def _showxv(image, title=None, command=None):
  2058.  
  2059.     if os.name == "nt":
  2060.         format = "BMP"
  2061.     elif sys.platform == "darwin":
  2062.         format = "JPEG"
  2063.         if not command:
  2064.             command = "open -a /Applications/Preview.app"
  2065.     else:
  2066.         format = None
  2067.         if not command:
  2068.             command = "xv"
  2069.             if title:
  2070.                 command = command + " -name \"%s\"" % title
  2071.  
  2072.     if image.mode == "I;16":
  2073.         # @PIL88 @PIL101
  2074.         # "I;16" isn't an 'official' mode, but we still want to
  2075.         # provide a simple way to show 16-bit images.
  2076.         base = "L"
  2077.     else:
  2078.         base = getmodebase(image.mode)
  2079.     if base != image.mode and image.mode != "1":
  2080.         file = image.convert(base)._dump(format=format)
  2081.     else:
  2082.         file = image._dump(format=format)
  2083.  
  2084.     if os.name == "nt":
  2085.         command = "start /wait %s && del /f %s" % (file, file)
  2086.     elif sys.platform == "darwin":
  2087.         # on darwin open returns immediately resulting in the temp
  2088.         # file removal while app is opening
  2089.         command = "(%s %s; sleep 20; rm -f %s)&" % (command, file, file)
  2090.     else:
  2091.         command = "(%s %s; rm -f %s)&" % (command, file, file)
  2092.  
  2093.     os.system(command)
  2094.