'Returns a converted copy of this image. For the "P" mode, this
method translates pixels through the palette.  If mode is
omitted, a mode is chosen so that all information in the image
and the palette can be represented without a palette.
The current version supports all possible conversions between
"L", "RGB" and "CMYK." The **matrix** argument only supports "L"
and "RGB".
When translating a color image to black and white (mode "L"),
the library uses the ITU-R 601-2 luma transform::
L = R * 299/1000 + G * 587/1000 + B * 114/1000
The default method of converting a greyscale ("L") or "RGB"
image into a bilevel (mode "1") image uses Floyd-Steinberg
dither to approximate the original image luminosity levels. If
dither is NONE, all non-zero values are set to 255 (white). To
use other thresholds, use the :py:meth:`~PIL.Image.Image.point`
method.
:param mode: The requested mode. See: :ref:`concept-modes`.
:param matrix: An optional conversion matrix.  If given, this
should be 4- or 12-tuple containing floating point values.
:param dither: Dithering method, used when converting from
mode "RGB" to "P" or from "RGB" or "L" to "1".
Available methods are NONE or FLOYDSTEINBERG (default).
:param palette: Palette to use when converting from mode "RGB"
to "P".  Available palettes are WEB or ADAPTIVE.
:param colors: Number of colors to use for the ADAPTIVE palette.
Defaults to 256.
:rtype: :py:class:`~PIL.Image.Image`
:returns: An :py:class:`~PIL.Image.Image` object.'

def convert(self, mode=None, matrix=None, dither=None, palette=WEB, colors=256):

    if (not mode):
        if (self.mode == 'P'):
            self.load()
            if self.palette:
                mode = self.palette.mode
            else:
                mode = 'RGB'
        else:
            return self.copy()
    self.load()
    if matrix:
        if (mode not in ('L', 'RGB')):
            raise ValueError('illegal    conversion')
        im = self.im.convert_matrix(mode, matrix)
        return self._new(im)
    if ((mode == 'P') and (self.mode == 'RGBA')):
        return self.quantize(colors)
    trns = None
    delete_trns = False
    if (('transparency' in self.info) and (self.info['transparency'] is not None)):
        if ((self.mode in ('L', 'RGB')) and (mode == 'RGBA')):
            return self._new(self.im.convert_transparent(mode, self.info['transparency']))
        elif ((self.mode in ('L', 'RGB', 'P')) and (mode in ('L', 'RGB', 'P'))):
            t = self.info['transparency']
            if isinstance(t, bytes):
                warnings.warn((('Palette    images    with    Transparency        ' + '    expressed    in    bytes    should    be    converted    ') + 'to    RGBA    images'))
                delete_trns = True
            else:
                trns_im = Image()._new(core.new(self.mode, (1, 1)))
                if (self.mode == 'P'):
                    trns_im.putpalette(self.palette)
                    if isinstance(t, tuple):
                        try:
                            t = trns_im.palette.getcolor(t)
                        except:
                            raise ValueError("Couldn't    allocate    a    palette    color    for    transparency")
                trns_im.putpixel((0, 0), t)
                if (mode in ('L', 'RGB')):
                    trns_im = trns_im.convert(mode)
                else:
                    trns_im = trns_im.convert('RGB')
                trns = trns_im.getpixel((0, 0))
        elif ((self.mode == 'P') and (mode == 'RGBA')):
            t = self.info['transparency']
            delete_trns = True
            if isinstance(t, bytes):
                self.im.putpalettealphas(t)
            elif isinstance(t, int):
                self.im.putpalettealpha(t, 0)
            else:
                raise ValueError(('Transparency    for    P    mode    should' + '    be    bytes    or    int'))
    if ((mode == 'P') and (palette == ADAPTIVE)):
        im = self.im.quantize(colors)
        new = self._new(im)
        from . import ImagePalette
        new.palette = ImagePalette.raw('RGB', new.im.getpalette('RGB'))
        if delete_trns:
            del new.info['transparency']
        if (trns is not None):
            try:
                new.info['transparency'] = new.palette.getcolor(trns)
            except:
                del new.info['transparency']
                warnings.warn(("Couldn't    allocate    palette    entry    " + 'for    transparency'))
        return new
    if (dither is None):
        dither = FLOYDSTEINBERG
    try:
        im = self.im.convert(mode, dither)
    except ValueError:
        try:
            im = self.im.convert(getmodebase(self.mode))
            im = im.convert(mode, dither)
        except KeyError:
            raise ValueError('illegal    conversion')
    new_im = self._new(im)
    if delete_trns:
        del new_im.info['transparency']
    if (trns is not None):
        if (new_im.mode == 'P'):
            try:
                new_im.info['transparency'] = new_im.palette.getcolor(trns)
            except:
                del new_im.info['transparency']
                warnings.warn(("Couldn't    allocate    palette    entry    " + 'for    transparency'))
        else:
            new_im.info['transparency'] = trns
    return new_im
