'DEPRECATED
Load Windows ICO image.
See http://en.wikipedia.org/w/index.php?oldid=264332061 for file format
description.
Cribbed and modified from http://djangosnippets.org/snippets/1287/'

def load_icon(self, image_file, index=None):

    try:
        image_file.seek(0)
        header = struct.unpack('<3H', image_file.read(6))
    except Exception as e:
        return
    if (header[:2] != (0, 1)):
        return
    directories = []
    for i in xrange(header[2]):
        directory = list(struct.unpack('<4B2H2I', image_file.read(16)))
        for j in xrange(3):
            if (not directory[j]):
                directory[j] = 256
        directories.append(directory)
    if (index is None):
        directory = max(directories, key=operator.itemgetter(slice(0, 3)))
    else:
        directory = directories[index]
    image_file.seek(directory[7])
    prefix = image_file.read(16)
    image_file.seek((-16), 1)
    if PngImagePlugin._accept(prefix):
        try:
            image = PngImagePlugin.PngImageFile(image_file)
        except IOError:
            return
    else:
        try:
            image = BmpImagePlugin.DibImageFile(image_file)
        except IOError:
            return
        if (image.mode == 'RGBA'):
            pass
        else:
            image.size = (image.size[0], (image.size[1] >> 1))
            (d, e, o, a) = image.tile[0]
            image.tile[0] = (d, ((0, 0) + image.size), o, a)
            offset = (o + (a[1] * image.size[1]))
            stride = (((image.size[0] + 31) >> 5) << 2)
            size = (stride * image.size[1])
            image_file.seek(offset)
            string = image_file.read(size)
            mask = Image.frombytes('1', image.size, string, 'raw', ('1;I', stride, (-1)))
            image = image.convert('RGBA')
            image.putalpha(mask)
    return image
