xxxxxxxxxx
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()
In the above code, we save the im_resize Image object into BytesIO object buf.
Note that in this case, you have to specify the saving image format because
PIL does not know the image format in this case. The bytes string can be retrieved
using getvalue() method of buf variable.