import io


def image_data(img, format, **save_options):
    """Save a PIL Image instance and return its byte contents"""
    fp = io.BytesIO()
    img.save(fp, format, **save_options)
    fp.seek(0)
    return fp.read()