'Convert palette to bytes.
.. warning:: This method is experimental.'

def tobytes(self):

    if self.rawmode:
        raise ValueError('palette    contains    raw    palette    data')
    if isinstance(self.palette, bytes):
        return self.palette
    arr = array.array('B', self.palette)
    if hasattr(arr, 'tobytes'):
        return arr.tobytes()
    return arr.tostring()
