'Get an image from frame idx'

def frame(self, idx):

    header = self.entry[idx]
    self.buf.seek(header['offset'])
    data = self.buf.read(8)
    self.buf.seek(header['offset'])
    if (data[:8] == PngImagePlugin._MAGIC):
        im = PngImagePlugin.PngImageFile(self.buf)
    else:
        im = BmpImagePlugin.DibImageFile(self.buf)
        im.size = (im.size[0], int((im.size[1] / 2)))
        (d, e, o, a) = im.tile[0]
        im.tile[0] = (d, ((0, 0) + im.size), o, a)
        mode = a[0]
        bpp = 8
        for (k, v) in BmpImagePlugin.BIT2MODE.items():
            if (mode == v[1]):
                bpp = k
                break
        if (32 == bpp):
            self.buf.seek(o)
            alpha_bytes = self.buf.read(((im.size[0] * im.size[1]) * 4))[3::4]
            mask = Image.frombuffer('L', im.size, alpha_bytes, 'raw', ('L', 0, (-1)))
        else:
            w = im.size[0]
            if ((w % 32) > 0):
                w += (32 - (im.size[0] % 32))
            and_mask_offset = (o + int(((im.size[0] * im.size[1]) * (bpp / 8.0))))
            total_bytes = int(((w * im.size[1]) / 8))
            self.buf.seek(and_mask_offset)
            mask_data = self.buf.read(total_bytes)
            mask = Image.frombuffer('1', im.size, mask_data, 'raw', ('1;I', int((w / 8)), (-1)))
        im = im.convert('RGBA')
        im.putalpha(mask)
    return im
