import io
import base64


def _embed_img(display):
    """
    Parameters
    ----------
    display: obj
        A Nilearn plotting object to display

    Returns
    -------
    embed : str
        Binary image string
    """
    if display is None:  # no image to display
        return None

    else:  # we were passed a matplotlib display
        io_buffer = io.BytesIO()
        display.frame_axes.figure.savefig(io_buffer, format='svg',
                                          facecolor='white',
                                          edgecolor='white')
        display.close()

        io_buffer.seek(0)
        data = base64.b64encode(io_buffer.read())

        return '{}'.format(data.decode())