def generate_gif(frames, gif_name="demo.gif", is_pygame_surface=False, duration=30):
    """

    Args:
        frames: a list of images or pygame surfaces
        gif_name: name of the file
        is_pygame_surface: convert pygame surface to PIL.image
        duration: controlling the duration of each frame, unit: ms

    Returns:

    """
    assert gif_name.endswith("gif"), "File name should end with .gif"
    imgs = [pygame.surfarray.array3d(frame) if is_pygame_surface else frame for frame in frames]
    imgs = [Image.fromarray(img) for img in imgs]
    imgs[0].save(gif_name, save_all=True, append_images=imgs[1:], duration=duration, loop=0)


    Args:
        frames: a list of images or pygame surfaces
        gif_name: name of the file
        is_pygame_surface: convert pygame surface to PIL.image
        duration: controlling the duration of each frame, unit: ms

    Returns:

    
metadrive/utils/doc_utils.py