'writeGifToFile(fp, images, durations, loops, xys, disposes)
Given a set of images writes the bytes to the specified stream.'

def writeGifToFile(self, fp, images, durations, loops, xys, disposes):

    (palettes, occur) = ([], [])
    for im in images:
        (header, usedPaletteColors) = getheader(im)
        palettes.append(header[(-1)])
    for palette in palettes:
        occur.append(palettes.count(palette))
    globalPalette = palettes[occur.index(max(occur))]
    frames = 0
    firstFrame = True
    for (im, palette) in zip(images, palettes):
        if firstFrame:
            header = self.getheaderAnim(im)
            appext = self.getAppExt(loops)
            fp.write(header)
            fp.write(globalPalette)
            fp.write(appext)
            firstFrame = False
        if True:
            data = getdata(im)
            (imdes, data) = (''.join(data[:(-2)]), data[(-2):])
            graphext = self.getGraphicsControlExt(durations[frames], disposes[frames])
            lid = self.getImageDescriptor(im, xys[frames])
            if ((palette != globalPalette) or (disposes[frames] != 2)):
                fp.write(graphext)
                fp.write(lid)
                fp.write(palette)
                fp.write('\x08')
            else:
                fp.write(graphext)
                fp.write(imdes)
            for d in data:
                fp.write(d)
        frames = (frames + 1)
    fp.write(';')
    return frames
