# Path: TileStache/py3_compat.py
# def is_string_type(val):


from struct import unpack, pack
from math import sqrt, ceil, log
from .py3_compat import urlopen, reduce
from operator import add
    from PIL import Image
    import Image
    for offset in range(0, len(pixels), 4):
        r, g, b, a = unpack('!BBBB', pixels[offset:offset+4])

        if a < 0x80 and t_value is not None:
            # Sufficiently transparent
            indexes.append(t_value)
            continue

        try:
            indexes.append(mapping[(r, g, b)])

        except KeyError:
            # Never seen this color
            mapping[(r, g, b)] = pack('!B', palette_color(r, g, b, palette, t_index))

        else:
            continue

        indexes.append(mapping[(r, g, b)])

    if hasattr(Image, 'frombytes'):
        # Image.fromstring is deprecated past Pillow 2.0
        output = Image.frombytes('P', image.size, b''.join(indexes))
    else:
        # PIL still uses Image.fromstring
        output = Image.fromstring('P', image.size, b''.join(indexes))

    bits = int(ceil(log(len(palette)) / log(2)))

    palette += [(0, 0, 0)] * (256 - len(palette))

# Path: TileStache/py3_compat.py
# def is_string_type(val):


# Path: TileStache/Pixels.py
from struct import unpack, pack
from math import sqrt, ceil, log
from .py3_compat import urlopen, reduce
from operator import add
    from PIL import Image
    import Image

    for offset in range(0, len(pixels), 4):
        r, g, b, a = unpack('!BBBB', pixels[offset:offset+4])

        if a < 0x80 and t_value is not None:
            # Sufficiently transparent
            indexes.append(t_value)
            continue

        try:
            indexes.append(mapping[(r, g, b)])

        except KeyError:
            # Never seen this color
            mapping[(r, g, b)] = pack('!B', palette_color(r, g, b, palette, t_index))

        else:
            continue

        indexes.append(mapping[(r, g, b)])

    if hasattr(Image, 'frombytes'):
        # Image.fromstring is deprecated past Pillow 2.0
        output = Image.frombytes('P', image.size, b''.join(indexes))
    else:
        # PIL still uses Image.fromstring
        output = Image.fromstring('P', image.size, b''.join(indexes))

    bits = int(ceil(log(len(palette)) / log(2)))

    palette += [(0, 0, 0)] * (256 - len(palette))

    palette = reduce(add, palette)