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

        Given a set of images writes the bytes to the specified stream.

        """

        # Obtain palette for all images and count each occurance
        palettes, occur = [], []
        for im in images:
            palettes.append(getheader(im)[1])
        for palette in palettes:
            occur.append(palettes.count(palette))

        # Select most-used palette as the global one (or first in case no max)
        print palettes, occur
        global_palette = palettes[occur.index(max(occur))]

        # Init
        frames = 0
        first_frame = True

        for im, palette in zip(images, palettes):

            if first_frame:
                # Write header

                # Gather info
                header = self.get_header_anim(im)
                appext = self.get_app_ext(loops)

                # Write
                fp.write(header)
                fp.write(global_palette)
                fp.write(appext)

                # Next frame is not the first
                first_frame = False

            if True:
                # Write palette and image data

                # Gather info
                data = getdata(im)
                imdes, data = data[0], data[1:]

                transparent_flag = 0
                if self.transparency:
                    transparent_flag = 1

                graphext = self.get_graphics_control_ext(
                    durations[frames],
                    disposes[frames],
                    transparent_flag=transparent_flag,
                    transparency_index=255)

                # Make image descriptor suitable for using 256 local color palette
                lid = self.get_image_descriptor(im, xys[frames])

                # Write local header
                if (palette != global_palette) or (disposes[frames] != 2):
                    # Use local color palette
                    fp.write(graphext)
                    fp.write(lid)  # write suitable image descriptor
                    fp.write(palette)  # write local color table
                    fp.write('\x08')  # LZW minimum size code
                else:
                    # Use global color palette
                    fp.write(graphext)
                    fp.write(imdes)  # write suitable image descriptor

                # Write image data
                for d in data:
                    fp.write(d)

            # Prepare for next round
            frames = frames + 1

        fp.write(";")  # end gif
        return frames
write_gif_to_file(fp, images, durations, loops, xys, disposes)

        Given a set of images writes the bytes to the specified stream.