def to_jpeg_bytes(arr, quality=100):
    """Given a numpy array image `arr`, return the image encoded as a
  jpeg buffer."""
    import io
    import imageio
    buf = io.BytesIO()
    imageio.imwrite(buf, arr, 'jpg', quality=quality)
    return buf.getvalue()
