Python change data to bytes without saving to disk

1
2
3
4
5
6
7
8
9
import io
from PIL import Image

im = Image.open('test.jpg')
im_resize = im.resize((500, 500))
buf = io.BytesIO()
im_resize.save(buf, format='JPEG')
byte_im = buf.getvalue()

JSON data to binary type

1
2
3
4

# change json to binary string
data = json.dumps(route_coords).encode("ascii")